Welcome Guest, Not a member yet? Register   Sign In
Form Validation - One number less than another
#1

[eluser]richzilla[/eluser]
Hi All, is there anyway to validate the following scenario in the form_validation library.

Basically, i have a stock form, where the quantity of stock is entered. Another field enables users to reserve a quantity of that stock, so that it doesn't appear available in the db. Obviously the amount reserved has to be less than the total amount in stock. How would i go about validating that?

Any help would be appreciated.
#2

[eluser]WanWizard[/eluser]
I have extended the form validation library, and added these methods to compare two posted values:
Code:
// --------------------------------------------------------------------
/**
* Match one field value to another
*
* @access    public
* @param    string
* @param    field
* @return    bool
*/
function matches_value($str, $field)
{
    return ($str !== $field) ? FALSE : TRUE;
}

// --------------------------------------------------------------------

/**
* checks if the value is less than the value of another field
*
* @access    public
* @param    string
* @param    field
* @return    bool
*/
function less_than($str, $field)
{
    if ( ! $field = $this->input->post($field) )
    {
        return FALSE;
    }
    else
    {
        return ($str >= $field) ? FALSE : TRUE;
    }
}

// --------------------------------------------------------------------

/**
* checks if the value is more than the value of another field
*
* @access    public
* @param    string
* @param    field
* @return    bool
*/
function more_than($str, $field)
{
    if ( ! $field = $this->input->post($field) )
    {
        return FALSE;
    }
    else
    {
        return ($str <= $field) ? FALSE : TRUE;
    }
}

Don't forget to add the appropriate language strings:
Code:
$lang['matches_value']            = 'The value of the field %s must be equal to %s.';
$lang['less_than']                = 'The field %s must have a lower value than %s.';
$lang['more_than']                = 'The field %s must have a higher value than %s.';
#3

[eluser]richzilla[/eluser]
Spot on. Thanks for the help.
#4

[eluser]WanWizard[/eluser]
Small update, I posted an old version of the code (and forgot the language strings).




Theme © iAndrew 2016 - Forum software by © MyBB