Question

I am using 'MultivalidatableBehavior' in cakephp. Though it worked fine with previous version. But its not working with Cakephp 2.4.0 version. Is their any changes with this behaviour. Because When I use this code.

var $validationSets = array( 'ruleset mentioned in it. ');

Though my request go through the Behaviour class but its not putting validation on the desired field. I have also checked $this->request->data Its also valid and for same Model where ruleset has been written.

I tried to debug using die in my MultivalidatableBehavior class. Though during valiation my request falls in the function of setValidation(&$model, $rules = array()) {. Please suggest if it is compatible with greater version of cakephp2.3.

My tried code..

Model Code :

 var $actsAs = array('Multivalidatable');
 var $validationSets = array(
        'login' => array(
            'username' => array('required' => array('rule' => array('notEmpty'), 'message' => 'Username is required !!')),
            'password' => array('required' => array('rule' => array('notEmpty'), 'message' => 'Password is required !!')),
        ),
    ); 

Controller Call.

 $this->User->setValidation('login'); Fields are also with same name. 

Its not validating If I put if($this->User->setValidation('login')) it returns false.

Was it helpful?

Solution

Thanks to Piotr Beschel for helping me out. I shouldn't use any sort of behaviour. It can also be achieved without behaviour. My Model code would be

public $validationAdmin = array(
        'adminLogin' => array(
            'username' => array('required' => array('rule' => array('notEmpty'), 'message' => 'Please enter your username !!')),
            'password' => array('required' => array('rule' => array('notEmpty'), 'message' => 'Please enter your password !!')),
        ),
        'adminForgotPassword' => array(
            'email' => array('required' => array('rule' => array('notEmpty'), 'message' => 'Please enter your email address !!'),
                'email' => array('rule' => array('email'), 'message' => 'Please enter valid mail address')),
        ),
    );

That is simply creating variable named $valdiationAdmin I have named it as admin to know following rule set is for admin.

My sample controller code would be.

$this->Admin->validate = $this->Admin->validationAdmin['adminLogin']; 

Just this way. If form has data to save it would move like butter. In case data is just validating I have to force form to validate it(In case login where data is not saved).

if ($this->Admin->validates()) { 
} else {
      $errors = $this->Admin->validationErrors;
      $this->set('validationErrors', $errors);
}

It would validate my form. Even add error message to form field in ctp file. I need not to write $this->form->error('fieldname');

Thanks again to all the viewer. Suggest me something if more better. I am thinking to validate data of other form if associated with one form, with same implementation. Help if you can.

OTHER TIPS

The method

$this->User->setValidation('login')

only chooses which validation Set will be used.

Use:

if ($this->User->validates()) {
    ...

to get the result of the validation.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top