In the user registration form, I have password and password2 so that user has to enter password 2x and they have to equal each other before he/she is registered. The validation rules work fine in this case. In the update user info form. I have both password and password2 set to blank so that if the user does not enter a value in the first password field or password2 field, then the system simply saves the current password which I have stored in a temporary variable in my user model. So far so good. What I want is to trigger all the same validation rules as when registering but ONLY if the user enters a value in the first password field, but leave blank and ignore otherwise. So I guess this is some kind of conditional validation.
In the user model:
$validate = array(
'password' => array(
'password_notempty'=>array(
'rule' => 'notEmpty',
'message' => 'Required field',
'on' => 'create')
,
'password_between'=>array(
'rule' => array('between', 5, 15),
'message' => 'Between 5 to 15 characters',
'on' => 'create')
,
'password_alphanumeric'=>array(
'rule' => 'alphaNumeric',
'message' => 'Characters and numbers only',
'on' => 'create')
),
'password2' => array(
'password2_isequal' =>array(
'rule' => array('comparePasswords','password'),
'message' => 'Must be same value as password.')
)
);
Anyone have ideas on how to accomplish this?
this should help getting you started:
http://www.dereuromark.de/2011/08/25/working-with-passwords-in-cakephp/
you can also look at the code for examples on how to create the validation needed.
basically you always use a different field and only map it to the real field if something is actually entered. also dont forget to include a confirmation field (to retype and confirm the correctly typed password)
the article describes how you can achieve all that will a behavior in 3-4 lines of code.
read below url :-
http://book.cakephp.org/1.3/view/1143/Data-Validation
http://book.cakephp.org/1.3/view/1152/Core-Validation-Rules
//Or Try this:-
$validate = array(
'new_password' => array(
'between' => array(
'rule' => array('between', 5, 20),
'allowEmpty' => false,
'message' => 'You password must be between 7 and 20 characters long')),
'password' => array(
'between' => array(
'rule' => array('between', 5, 20),
'allowEmpty' => false,
'message' => 'You password must be between 7 and 20 characters long')),
'retype_password'=>array(
'rule'=>array('equalTo','password'),
'message'=>'Password does not match'),
'repeat_password'=>array(
'rule'=>array('equalTo','new_password'),
'message'=>'Password does not match'),
);
Related
I have following code in my model .
'uniqueNameRule' => array(
'rule' => 'isUnique',
'required' => true,
'allowEmpty' => false,
'on' => 'create', // here
'last' => false,
'message' => 'name already exists'
)
The above code validates the field name but there is an issue that when i try to insert new name it does not save it , means validation message is shown for each time .
I tried to figure out this and find that 'on' => 'create', is creating issue . Please help me on this thanks.
Make sure that you set the validation on the right way:
public $validate = array(
'column_name' => array(
'uniqueNameRule' => array(
'rule' => 'isUnique',
'required' => true,
'allowEmpty' => false,
'on' => 'create', // here
'last' => false,
'message' => 'name already exists'
)
)
);
The 'on' => 'create', line says to cakePhp that the validation must be executed only when you a creating a new register. If you are updating an existing register, then validation won't be executed at all.
Also, check if the validation error is not running on another validation block, as you set 'last' => false, which means that validation will continue in spite of this rule failing.
If you've issue in isUnique rule in validation. You can use Multivalidatable Behavior. below is the complete example how to use please visit below link.
http://bakery.cakephp.org/articles/dardosordi/2008/07/29/multivalidatablebehavior-using-many-validation-rulesets-per-model
Thanks
I can successfully put new messages in view files only if they are not present in model. I mean validations have to be in model, but messages attached to those validations should not be there.
Example model
public $validate = array(
'username' => array(
'required' => array(
'rule' => array('notEmpty'),
'required' => true, 'allowEmpty' => false,
'message' => 'Please enter a username.'),
'alpha' => array(
'rule' => array('alphaNumeric'),
'message' => 'The username must be alphanumeric.'),
'unique_username' => array(
'rule'=>array('isUnique', 'username'),
'message' => 'This username is already in use.'),
'username_min' => array(
'rule' => array('minLength', '3'),
'message' => 'The username must have at least 3 characters.')),
'email' => array(
...................
...................
and here's an example of view
echo $this->Form->input('username',array(
'label' => __d('app_users','Username'),
'error' => array(
'required' => __d('app_users', 'Please enter a username'),
.....................
.....................
));
So, my question is how can one make it into a work without removing 'message' => 'Please enter a username.' in a model but to TRULY OVERRIDE IT.
I mean validations have to be in model, but messages attached to those validations should not be there.
And how do you then have messages for a JSON API service? The text contained in validation has nothing to do with how your site looks so should not be in a view.
You can change the validation message from the model or from custom validation methods without much hassle and its all in the book
I have a model ex. posts that acts as translate and I want to validate the title which is a translated field.
How is this possible. The below does not work.
'title' => array(
'notempty' => array(
'rule' => array('notempty'),
),
));
Change rule name to camelCase: notEmpty and add message
you translate your message in /Locale default.po
'title' => array(
'notEmpty' => array(
'rule' => array('notEmpty'),
'message' => 'your translated message',
),
));
whats the big deal?
How can I check in my controller which one of the validation rules failed? I want to know if 'rule2' failed, based on the validation rule key.
public $validate = array(
'email' => array(
'rule1' => array(
'rule' => array('email', true),
'message' => 'Please enter a valid email address'
),
'rule2' => array(
'rule' => 'isUnique',
'message' => 'Email address already registered'
)
),
);
I know I can do debug($this->User->validationErrors) but all that shows is the message, and I don't want to check the message because I might change that in the future.
Is there any way to see that 'email.rule2' failed, based on the validation rule key?
for me it never made sense that the default param for last is false
IMO the correct approach is:
public $validate = array(
'email' => array(
'rule1' => array(
'rule' => array('email', true),
'message' => 'Please enter a valid email address',
'last' => true
),
'rule2' => array(
'rule' => 'isUnique',
'message' => 'Email address already registered'
)
),
);
ONLY check on unique if the email is valid in the first place.
and no, the message is the only thing returned. why is it important what failed?
I'm not sure if it does exactly what you need but try this:
$errors = $this->ModelName->invalidFields();
I'm using Tank Auth (it's working fine), but I can't get the form validation function to work when I include any value in the variable array along with set_value. I need these values as a label for form fields--they tells the user what to enter in the field.
How can I keep Tank Auth's form validation functionality AND use a value in the value field?
$login = array(
'name' => 'login',
'id' => 'login',
'value' => set_value('login', 'Enter username'),
);
this populates field values : set_value('field_name');
this shows individual errors: form_errors('field_name');
If you use html5, you can simply add a placeholder!
{ placeholders should not be used as an alternative to labels!}
$login = array(
'name' => 'login',
'id' => 'login',
'value' => set_value('login'),
'placeholder' => 'Enter Username',
'required' => 'required'
);