public function validate($rules, $form = null)
    {
        $regExpEmailAddress = '/\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/i';
        $tests = [
            'fail' => [
                'message' => 'Will always fail.',
                'test' => 'return false;'
            ],

            'pass' => [
                'message' => 'Will always pass.',
                'test' => 'return true;'
            ],

            'not_empty' => [
                'message' => 'Can not be empty.',
                'test' => 'return $value != "";'
            ],

            'not_equal' => [
                'message' => 'Value not valid.',
                'test' => 'return $value != $arg;'
            ],

            'number' => [
                'message' => 'Must be a number.',
                'test' => 'return is_numeric($value);'
            ],

            'email' => [
                'message' => 'Must be an email adress.',
                'test' => function ($value) {
                    return preg_match('/\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/i', $value) === 1;
                }
            ],

            'match' => [
                'message' => 'The field does not match.',
                'test' => 'return $value == $form[$arg]["value"] ;'
            ],

            'must_accept' => [
                'message' => 'You must accept this.',
                'test' => 'return $checked;'
            ],

            'custom_test' => true,
        ];

        // max tecken, min tecken, datum, tid, datetime, mysql datetime

        $pass = true;
        $messages = array();
        $value = $this['value'];
        $checked = $this['checked'];

        foreach ($rules as $key => $val) {
            $rule = is_numeric($key) ? $val : $key;
            if (!isset($tests[$rule])) {
                throw new Exception("Validation of form element failed, no such validation rule exists: $rule");
            }
            $arg = is_numeric($key) ? null : $val;

            $test = ($rule == 'custom_test') ? $arg : $tests[$rule];
            $status = null;
            if (is_callable($test['test'])) {
                $status = $test['test']($value);
            } else {
                $status = eval($test['test']);
            }

            if ($status === false) {
                $messages[] = $test['message'];
                $pass = false;
            }
        }

        if (!empty($messages)) {
            $this['validation-messages'] = $messages;
        }
        return $pass;
    }
public function validate($rules, $form = null)
    {
        $regExpEmailAddress = '/\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/i';
        $tests = [
            'fail' => [
                'message' => 'Will always fail.',
                'test' => 'return false;'
            ],

            'pass' => [
                'message' => 'Will always pass.',
                'test' => 'return true;'
            ],

            'not_empty' => [
                'message' => 'Can not be empty.',
                'test' => 'return $value != "";'
            ],

            'not_equal' => [
                'message' => 'Value not valid.',
                'test' => 'return $value != $arg;'
            ],

            'number' => [
                'message' => 'Must be a number.',
                'test' => 'return is_numeric($value);'
            ],

            'email' => [
                'message' => 'Must be an email adress.',
                'test' => function ($value) {
                    return preg_match('/\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/i', $value) === 1;
                }
            ],

            'match' => [
                'message' => 'The field does not match.',
                'test' => 'return $value == $form[$arg]["value"] ;'
            ],

            'must_accept' => [
                'message' => 'You must accept this.',
                'test' => 'return $checked;'
            ],

            'custom_test' => true,
        ];

        // max tecken, min tecken, datum, tid, datetime, mysql datetime

        $pass = true;
        $messages = array();
        $value = $this['value'];
        $checked = $this['checked'];

        foreach ($rules as $key => $val) {
            $rule = is_numeric($key) ? $val : $key;
            if (!isset($tests[$rule])) {
                throw new Exception("Validation of form element failed, no such validation rule exists: $rule");
            }
            $arg = is_numeric($key) ? null : $val;

            $test = ($rule == 'custom_test') ? $arg : $tests[$rule];
            $status = null;
            if (is_callable($test['test'])) {
                $status = $test['test']($value);
            } else {
                $status = eval($test['test']);
            }

            if ($status === false) {
                $messages[] = $test['message'];
                $pass = false;
            }
        }

        if (!empty($messages)) {
            $this['validation-messages'] = $messages;
        }
        return $pass;
    }
public function validate ( $ rules , $ form = null ) { $ regExpEmailAddress = '/\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/i' ; $ tests = [ 'fail' => [ 'message' => 'Will always fail.' , 'test' => 'return false;' ] , 'pass' => [ 'message' => 'Will always pass.' , 'test' => 'return true;' ] , 'not_empty' => [ 'message' => 'Can not be empty.' , 'test' => 'return $value != "";' ] , 'not_equal' => [ 'message' => 'Value not valid.' , 'test' => 'return $value != $arg;' ] , 'number' => [ 'message' => 'Must be a number.' , 'test' => 'return is_numeric($value);' ] , 'email' => [ 'message' => 'Must be an email adress.' , 'test' => function ( $ value ) { return preg_match ( '/\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/i' , $ value ) === 1 ; } ] , 'match' => [ 'message' => 'The field does not match.' , 'test' => 'return $value == $form[$arg]["value"] ;' ] , 'must_accept' => [ 'message' => 'You must accept this.' , 'test' => 'return $checked;' ] , 'custom_test' => true , ] ; // max tecken, min tecken, datum, tid, datetime, mysql datetime $ pass = true ; $ messages = array ( ) ; $ value = $ this [ 'value' ] ; $ checked = $ this [ 'checked' ] ; foreach ( $ rules as $ key => $ val ) { $ rule = is_numeric ( $ key ) ? $ val : $ key ; if ( ! isset ( $ tests [ $ rule ] ) ) { throw new Exception ( "Validation of form element failed, no such validation rule exists: $rule" ) ; } $ arg = is_numeric ( $ key ) ? null : $ val ; $ test = ( $ rule == 'custom_test' ) ? $ arg : $ tests [ $ rule ] ; $ status = null ; if ( is_callable ( $ test [ 'test' ] ) ) { $ status = $ test [ 'test' ] ( $ value ) ; } else { $ status = eval ( $ test [ 'test' ] ) ; } if ( $ status === false ) { $ messages [ ] = $ test [ 'message' ] ; $ pass = false ; } } if ( ! empty ( $ messages ) ) { $ this [ 'validation-messages' ] = $ messages ; } return $ pass ; }
Validate the form element value according a ruleset.

@param array     $rules validation rules.
@param Form|null $form  the parent form.

@return boolean true if all rules pass, else false.
Validate the form element value according a ruleset .