1

can we create validation rules, which can work as a core validation rule.

Suppose I want to use a validation function which can be use for input fields.Means I have a function like:

function checkinput($input)
   {
     $arr=array('x','y','z');
     if(in_array($input,$arr))
        return false;
     else
        return true;
   }

Now I want to use this function as the validation in all models. suppose for this function a custom rule has been created, name checkinput. I want to use this validation in any model as:

var $validate = array(
   'name' => array(
        'notempty' => array(
            'rule' => 'notEmpty',
            'message' => 'Please provide a name.',
        ),
        'checkinput' => array(
            'rule' => 'checkinput',
            'message' => "You can't use X,Y,Z as name.",
        ),
    ));

Can this kind of custom validation rule be created in behaviour or by other method..

3 Answers 3

1

You should be able to place the function in your app_model.php (or AppModel.php depending on which version of Cake you are using. This will give all your models access to the function for validation/other purposes.

To quote the manual:

Model/behavior methods are checked first, before looking for a method on the Validation class. This means that you can override existing validation methods (such as alphaNumeric()) at an application level (by adding the method to AppModel), or at model level.

Sign up to request clarification or add additional context in comments.

Comments

0

This should help you: "Custom validation rules" http://www.dereuromark.de/2010/07/19/extended-core-validation-rules/

and http://www.dereuromark.de/2011/10/07/maximum-power-for-your-validation-rules/ with code in github

Note that you need to array_shift first (see debug output for details):

function validateCustom($field) {
    $field = array_shift($field);
    ....
}

Comments

0

We have posted the validation rules. this example best for validation in cakephp.

public $validate = array(
    'name' => array(
            'rule' => array('custom', '/^[a-z0-9]{1,}$/i'),
            'message' => 'Alphabets and numbers only'
        ),
    'user_name' => array(
        'isUnique' => array(
            'rule' => 'isUnique',
            'message' => 'The username has already been taken.',
        ),
        'notEmpty' => array(
            'rule' =>  array('custom','/^[a-z0-9]{1,}$/i'),
            'message' => 'Alphabets and numbers only',
        ),
    ),

    /*'password' => array(
        'rule' => array('minLength', 6),
        'message' => 'Passwords must be at least 6 characters long.',
    ),*/
    'password1' => array(
    'password' => array(
        'rule'    => '/^[a-z0-9]{1,}$/i',
        'message' => 'Only letters and integers'
        ),

        'between' => array(
            'rule'    => array('between', 6, 50),
            'message' => 'Between 6 to 50 characters'
        ),),
    'confirm_password' => array(
        'equaltofield' => array(
        'rule' => array('equaltofield','password1'),
        'message' => 'Password does not match.',
        //'allowEmpty' => false,
        //'required' => false,
        //'last' => false, // Stop validation after this rule
        'on' => 'create', // Limit validation to 'create' or 'update' operations
        ),
        'compare'    => array(
            'rule'      => array('validate_passwords'),
            'message' => 'The passwords you entered do not match.',
        )

    ),
    'email_address' => array(
        'email' => array(
            'rule' => array('custom','/^[A-Za-z0-9._%+-]+@([A-Za-z0-9-]+\.)+([A-Za-z0-9]{2,4}|museum)$/i'),
            'message' => 'Please provide a valid email address.',
        ),
        'isUnique' => array(
            'rule' => 'isUnique',
            'message' => 'Email address already in use.',
        ),
        )

    );

    function equaltofield($check,$otherfield)
    {
        //get name of field
        $fname = '';

        foreach ($check as $key => $value){
            $fname = $key;
            break;
        }



        return $this->data[$this->name][$otherfield] === $this->data[$this->name][$fname];
    } 
    /*public  function beforeValidate(){

        return true;
    }*/
    public function validate_passwords() {
            return $this->data[$this->alias]['password1'] === $this->data[$this->alias]['confirm_password'];
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.