public function rule($fieldName, $checks, $humanName = false)
    {
        $checkFailures = 0;

        // Default human readable name of form field to the field name.
        if ($humanName == false) {
            $humanName = ucfirst($fieldName);
        }

        // Grab data from array or leave as false if no data exists.
        $fieldData = $this->getValue($fieldName);

        if (!is_array($checks)) {
            $checks = explode('|', $checks);
        }

        // Run checks
        foreach ($checks as $check) {

            $checkName = $check;

            if (isset($this->customChecks->$check)) {

                // Grab this custom check's definition.
                $customCheckDef = $this->customChecks->$check;

                // Grab custom check type. Ex. "call"
                //$checkName = $check;
                $customType = $customCheckDef[0];

                // Define the arguments that will be passed to the custom check.
                $arguments = array($fieldData, $customCheckDef[1], $customCheckDef[2], $customCheckDef[3], $customCheckDef[4]);

                // Call the custom check.
                $checkResult = call_user_func_array(array($this, $customType), $arguments);
            }
            else {

                // See if validation check needs additional argument. E.g "matches[password]"
                $checkArgs = explode('[', $check, 2);
                if (count($checkArgs)>1) {

                    // Set corrected method check name to call. E.g. "matches[password]" calls "matches"
                    $check = $checkArgs[0];
                    $checkName = $checkArgs[0];

                    // explode strips out the leading '[' from '[value]' so we are just restoring it.
                    $checkArgs[1] = '['.$checkArgs[1];

                    // Grab arguments that need to be passed to Check. E.g. "matches[password][title]" would
                    // return "array('password', 'title')"
                    $args = array();
                    preg_match_all("/\[([^\]]*)\]/", $checkArgs[1], $args);
                    $this->matchedArg = $args[1][0];

                    // Append underscore to the check method's name.
                    $check = '_'.$checkName;

                    // Call a built-in check that's part of this Validation class.
                    $checkResult = $this->$check($fieldData, $args[1]);
                }
                else {
                    // Append underscore to the check method's name.
                    $check = '_'.$checkName;

                    // Call a built-in check that's part of this Validation class.
                    $checkResult = $this->$check($fieldData);
                }
            }

            // If the result of the called check is anything other than true, set a validation error.
            if ($checkResult == false) {
                $this->errors[] =  sprintf($this->lang->$checkName, $humanName, $this->matchedArg);
                $this->matchedArg = false;
                $checkFailures++;
            }
        }

        // After all checks for this data field have been run, return if Validation passed (TRUE) or failed (FALSE);
        return $checkFailures == 0 ? true : false;
    }
public function rule($fieldName, $checks, $humanName = false)
    {
        $checkFailures = 0;

        // Default human readable name of form field to the field name.
        if ($humanName == false) {
            $humanName = ucfirst($fieldName);
        }

        // Grab data from array or leave as false if no data exists.
        $fieldData = $this->getValue($fieldName);

        if (!is_array($checks)) {
            $checks = explode('|', $checks);
        }

        // Run checks
        foreach ($checks as $check) {

            $checkName = $check;

            if (isset($this->customChecks->$check)) {

                // Grab this custom check's definition.
                $customCheckDef = $this->customChecks->$check;

                // Grab custom check type. Ex. "call"
                //$checkName = $check;
                $customType = $customCheckDef[0];

                // Define the arguments that will be passed to the custom check.
                $arguments = array($fieldData, $customCheckDef[1], $customCheckDef[2], $customCheckDef[3], $customCheckDef[4]);

                // Call the custom check.
                $checkResult = call_user_func_array(array($this, $customType), $arguments);
            }
            else {

                // See if validation check needs additional argument. E.g "matches[password]"
                $checkArgs = explode('[', $check, 2);
                if (count($checkArgs)>1) {

                    // Set corrected method check name to call. E.g. "matches[password]" calls "matches"
                    $check = $checkArgs[0];
                    $checkName = $checkArgs[0];

                    // explode strips out the leading '[' from '[value]' so we are just restoring it.
                    $checkArgs[1] = '['.$checkArgs[1];

                    // Grab arguments that need to be passed to Check. E.g. "matches[password][title]" would
                    // return "array('password', 'title')"
                    $args = array();
                    preg_match_all("/\[([^\]]*)\]/", $checkArgs[1], $args);
                    $this->matchedArg = $args[1][0];

                    // Append underscore to the check method's name.
                    $check = '_'.$checkName;

                    // Call a built-in check that's part of this Validation class.
                    $checkResult = $this->$check($fieldData, $args[1]);
                }
                else {
                    // Append underscore to the check method's name.
                    $check = '_'.$checkName;

                    // Call a built-in check that's part of this Validation class.
                    $checkResult = $this->$check($fieldData);
                }
            }

            // If the result of the called check is anything other than true, set a validation error.
            if ($checkResult == false) {
                $this->errors[] =  sprintf($this->lang->$checkName, $humanName, $this->matchedArg);
                $this->matchedArg = false;
                $checkFailures++;
            }
        }

        // After all checks for this data field have been run, return if Validation passed (TRUE) or failed (FALSE);
        return $checkFailures == 0 ? true : false;
    }
public function rule ( $ fieldName , $ checks , $ humanName = false ) { $ checkFailures = 0 ; // Default human readable name of form field to the field name. if ( $ humanName == false ) { $ humanName = ucfirst ( $ fieldName ) ; } // Grab data from array or leave as false if no data exists. $ fieldData = $ this -> getValue ( $ fieldName ) ; if ( ! is_array ( $ checks ) ) { $ checks = explode ( '|' , $ checks ) ; } // Run checks foreach ( $ checks as $ check ) { $ checkName = $ check ; if ( isset ( $ this -> customChecks -> $ check ) ) { // Grab this custom check's definition. $ customCheckDef = $ this -> customChecks -> $ check ; // Grab custom check type. Ex. "call" //$checkName = $check; $ customType = $ customCheckDef [ 0 ] ; // Define the arguments that will be passed to the custom check. $ arguments = array ( $ fieldData , $ customCheckDef [ 1 ] , $ customCheckDef [ 2 ] , $ customCheckDef [ 3 ] , $ customCheckDef [ 4 ] ) ; // Call the custom check. $ checkResult = call_user_func_array ( array ( $ this , $ customType ) , $ arguments ) ; } else { // See if validation check needs additional argument. E.g "matches[password]" $ checkArgs = explode ( '[' , $ check , 2 ) ; if ( count ( $ checkArgs ) > 1 ) { // Set corrected method check name to call. E.g. "matches[password]" calls "matches" $ check = $ checkArgs [ 0 ] ; $ checkName = $ checkArgs [ 0 ] ; // explode strips out the leading '[' from '[value]' so we are just restoring it. $ checkArgs [ 1 ] = '[' . $ checkArgs [ 1 ] ; // Grab arguments that need to be passed to Check. E.g. "matches[password][title]" would // return "array('password', 'title')" $ args = array ( ) ; preg_match_all ( "/\[([^\]]*)\]/" , $ checkArgs [ 1 ] , $ args ) ; $ this -> matchedArg = $ args [ 1 ] [ 0 ] ; // Append underscore to the check method's name. $ check = '_' . $ checkName ; // Call a built-in check that's part of this Validation class. $ checkResult = $ this -> $ check ( $ fieldData , $ args [ 1 ] ) ; } else { // Append underscore to the check method's name. $ check = '_' . $ checkName ; // Call a built-in check that's part of this Validation class. $ checkResult = $ this -> $ check ( $ fieldData ) ; } } // If the result of the called check is anything other than true, set a validation error. if ( $ checkResult == false ) { $ this -> errors [ ] = sprintf ( $ this -> lang -> $ checkName , $ humanName , $ this -> matchedArg ) ; $ this -> matchedArg = false ; $ checkFailures ++ ; } } // After all checks for this data field have been run, return if Validation passed (TRUE) or failed (FALSE); return $ checkFailures == 0 ? true : false ; }
Checks a data field for validness by running all the specified checks
against it.
Checks a data field for validness by running all the specified checks against it .