0

I've had a working registration/update model, I wanted to expand on my models so I added in a regex to the password field. I have checked the regex works online and even my client side validation shows it works, but the model refuses to save now. I'm not really sure why, could anyone help me out please?

        return array(
        array('firstName, lastName, email, password', 'required'),
        array('firstName', 'length', 'max'=>45),
        array('lastName', 'length', 'max'=>80),
        array('email', 'length', 'max'=>120),
        // email must be valid email
        array('email', 'email'),    
        // email must be unique
        array('email', 'unique'),
        // Regex for password
        array('password','match', 'pattern'=>'/^[a-z0-9_-]{7,20}$/i', 
            'message'=>'The password must be between 7 and 20 characters
             long'),


        array('password', 'length', 'min'=>7, 'max'=>64),
        array('date_modified', 'safe'),
        array('active, date_modified', 'default', 'setOnEmpty' => true, 'value' => null),
        array('id, first_name, last_name, email, pass, active, date_created, date_modified, live', 'safe', 'on'=>'search'),

    );

Thanks

Jonny

Jonnny
  • 4,939
  • 11
  • 63
  • 93
  • Did you take a look at yii logs for errrors ? Did you try to `var_dump` your model errors : `var_dump($model->errors);` ? – soju May 06 '13 at 06:41
  • After a closer look the problem was in my beforeSave() method. Thanks for all help as always. – Jonnny Jul 25 '13 at 15:16

1 Answers1

2

You can create your own validation rule.

http://www.yiiframework.com/wiki/168/create-your-own-validation-rule/

Or else you can define validation rule in YII Model, something like this:

return array(
    array('password', 'length', 'min'=>7, 'max'=>64),
    array('password','pattern'=>'/^[A-Za-z0-9_!@#$%^&*()+=?.,]+$/u', 'message'=>'Spaces or given characters are not allowed'),
);

There are more validation you can specify in your model.

Mohit Bhansali
  • 1,725
  • 4
  • 20
  • 43