Reputation: 47
I have a simple create new account page with username, email, confirm email, password, and confirm passwords fields. All of the validations goes under the User model and currently all the input fields are required.
Once the user logs in, there's a setting page where they can update their email and password. The input fields are email, confirm email, password, and confirm password (same input fields as the create an account page sans username field). However, I want the validations to be different under settings with none of the fields being required. How would I approach this problem without affecting the validation rules for the create new account page? Your help is much appreciated.
Upvotes: 0
Views: 478
Reputation: 29121
It doesn't seem like you really need ANY of the fields "required" as CakePHP thinks of it.
"Required" as far as CakePHP is concerned means that that field MUST be submitted any and every time that model is saved. It has nothing to do with whether or not there is content in the field (which is 'notEmpty').
So - for your case, you could probably just set up the normal data validation rules for each field (ie minLength, notEmpty, valid email...etc etc, etc, and be just fine for both pages. Any data that's submitted must pass the validation - and if it's not submitted, no big deal.
You could always set:
'required' => 'create' //or update
if you need to verify that that field exists in the data for a save or update... but I've personally never found that necessary and have created many pages like you've described.
Per the book [here]:
required => true does not mean the same as the validation rule notEmpty(). required => true indicates that the array key must be present - it does not mean it must have a value. Therefore validation will fail if the field is not present in the dataset, but may (depending on the rule) succeed if the value submitted is empty (‘’).
Upvotes: 2
Reputation: 708
You can use 'on'
, like this:
array(
'rule' => 'required',
'on' => 'create' // or 'update'
)
Or you can try unseting validations in the controller, but the first is cleaner.
Upvotes: 0