CodeIgniter Forums

Full Version: Form Validation - One number less than another
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[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.

El Forum

[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.';

El Forum

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

El Forum

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