CodeIgniter Forums
Form Validation - One number less than another - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Form Validation - One number less than another (/showthread.php?tid=29953)



Form Validation - One number less than another - El Forum - 04-27-2010

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


Form Validation - One number less than another - El Forum - 04-27-2010

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



Form Validation - One number less than another - El Forum - 04-27-2010

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


Form Validation - One number less than another - El Forum - 04-27-2010

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