Welcome Guest, Not a member yet? Register   Sign In
My required if custom validation rule
#1

[eluser]Ollie Rattue[/eluser]
Hello everyone, I am attempting to right a new validation rule, Required IF. I want to make one field e.g. a select field required if a corresponding field e.g. text
input has a value. I have took a look at the validation class and have tried to combine two rules, matches (because it looks at two fields at once) and required, but without much success.

Code:
// required if another field has a value

function requiredif($str, $field)
{
    if ( isset($_POST[$field]))
    {
        if ( ! is_array($str))
        {
            return (trim($str) == '') ? FALSE : TRUE;
        }
        else
        {
            return ( ! empty($str));
        }
    } else {
    
    return ($str !== $_POST[$field]) ? FALSE : TRUE;
    }
}

The two code igniter rules I was working from for reference purposes.

Code:
/**
* Required
*
* @access    public
* @param    string
* @return    bool
*/
function required($str)
{
    if ( ! is_array($str))
    {
        return (trim($str) == '') ? FALSE : TRUE;
    }
    else
    {
        return ( ! empty($str));
    }
}

/**
* Match one field to another
*
* @access    public
* @param    string
* @param    field
* @return    bool
*/
function matches($str, $field)
{
    if ( ! isset($_POST[$field]))
    {
        return FALSE;
    }
    
    return ($str !== $_POST[$field]) ? FALSE : TRUE;
}



I have then added this error message to the language file.

Code:
$lang['requiredif']        = "The %s field is required";

I have called this like a normal validation rule. For upgradability I should extend the validation class instead of adding this rule in directly, however I wanted
to remove as many possibilities for bugs.

It would be fantastic if anyone could point in in the right direction, or highlight where I have gone wrong.

Please note I am using code igniter CodeIgniter V 1.6.3 as I have yet to have had the time to do an upgrade to 1.70.
#2

[eluser]lmv4321[/eluser]
Try changing your last return line in your code to the following:

Code:
// required if another field has a value
function requiredif($str, $field)
{
    if ( isset($_POST[$field]))
    {
        if ( ! is_array($str))
        {
            return (trim($str) == '') ? FALSE : TRUE;
        }
        else
        {
            return ( ! empty($str));
        }
    } else {
    
    return TRUE;
    }
}

This makes the logic work as follows: If $field is set {check $str as it is required} else return true (always fine).
#3

[eluser]iainco[/eluser]
Sorry, made a mistake in my post and since I can't delete it I'll type this...
#4

[eluser]t'mo[/eluser]
I made a request to get this sort of thing added to the core distribution: http://ellislab.com/forums/viewthread/110793/
#5

[eluser]Ollie Rattue[/eluser]
[quote author="t’mo" date="1238928189"]I made a request to get this sort of thing added to the core distribution: http://ellislab.com/forums/viewthread/110793/[/quote]

I wrote up my process and thoughts when I was playing with this rule at http://toomanytabs.com/requiredif.php. Maybe this page might be of interest.




Theme © iAndrew 2016 - Forum software by © MyBB