Want to show a custom message in form's error list, if the two fields did not match.
the from is as follows,
'old_password' =>'Old Password*',
'new_password' =>'New Password*',
'confirm_password' =>'Confirm Password*',
I want that the old password should match the value from the database, the value in new password and confirm password should also match.
please help me.
In Symfony 1.1 and later, to compare if the two form fields match you need to set up a post validator, like:
$this->validatorSchema->setPostValidator(
new sfValidatorSchemaCompare(
'new_password',
sfValidatorSchemaCompare::EQUAL,
'confirm_password',
array(),
array('invalid' => 'Your custom error message here!!')
)
);
Try reading Symfony forms in Action, it should solve most of your problems about form creation and validation within the Symfony framework
Related
I'm playing around with Cakephp 3.0 and wondering how I go about validating data which is not being saved to the database.
For example I know in the model (which appears to be now known as a "table") you add a validationDefault method to the model which is called automatically when data is being saved to the database i.e a new user is being added to the database. But how would I go about validating data from a login form which is not saving to the database and then show those errors?
For example on a user login, I would want to check whether fields have been entered, doesn't exceed a certain size etc
Taken from the official docs, you can instantiate a validator in your controller and validate your data by passing it $this->request->data()
use Cake\Validation\Validator;
...
$validator = new Validator();
$validator
->validatePresence('email')
->add('email', 'validFormat', [
'rule' => 'email',
'message' => 'E-mail must be valid'
])
->validatePresence('name')
->notEmpty('name', 'We need your name.')
->validatePresence('comment')
->notEmpty('comment', 'You need to give a comment.');
$errors = $validator->errors($this->request->data());
if (!empty($errors)) {
// Send an email.
}
http://book.cakephp.org/3.0/en/core-libraries/validation.html
I am using CakePHP for my application, where I have a User model. This User has a password, which has a regex to validate.
The regex forces the user to use a password at least 6 characters long, containing at least 1 number and special char.
The validation looks like this:
'password' => array(
'ruleName' => array(
'rule' => array('custom', '/^.*(?=.{6,})(?=.*\d)(?=.*[a-zA-Z])(?=.*[##$%^&+=]).*$/i'),
'message' => 'Password not legit',
'allowEmpty' => true
)
)
When I want to edit my password, this validation works great. But when I want to edit the user (no option to change password there), the $this->User->save() fails.
If I debug my $this->User->validationErrors, the only thing shown is:
array(
'password' => '*****'
)
The password field is not set in my post data, so the validation should not happen at all.
When I comment this block of validation code, the user can be saved.
Anyone knows what I am doing wrong?
I solved it myself already. Before saving the User object, I already did a $this->User->Read(null, $userid) for other purposes.
This resulted in remembering the values of the read (including password) in the $this->User object.
Since the save method is called on the $this->User object, the password value is trying to get saved too. But since *** isn't valid according to the regex, the save fails.
Thanks for the help anyway!
I was investigating CakePHP (2.3.4) Model::save method to insert new records and ran into a little snag:
//Two fields provided, email field intended to fail validation
$data = array('Member' => array(
'username' => 'hello',
'email' => 'world'
));
$this->Member->create();
var_dump($this->Member->save($data, true));
The above save() will return false and no data will be written to the database. However if I change the data to:
//One field provided, intended to pass validation
$data = array('Member' => array(
'username' => 'hello'
));
then save() will attempt to write a new record to database with a blank email field. I realize that skipping validation for unspecified fields might be a useful behavior during updates, but is there a CakePHP recommended way to handle partially empty data sets when creating new records? Thanks a lot.
Edit:
Thanks to Sam Delaney for the tip. In case anybody else gets stumped, this did the trick: CakePHP Data Validation: field 'required' key
This key accepts either a boolean, or create or update. Setting this key to true will make the field always required. While setting it to create or update will make the field required only for update or create operations. If ‘required’ is evaluated to true, the field must be present in the data array. For example, if the validation rule has been defined as follows:
I had originally baked the model and forgotten that required => true was left out of the validation rule. Setting it to true or 'create' would've avoided me blank records getting inserted due to gibberish data array.
IMHO what you've experienced is the desired behavior. Consider that you have the same two fields within a form and the user provides a value for only username. Both username and email field are submitted even though email is empty. Then on the server, you try to save the record only to find out that it has failed validation which you feedback to the user. On the other hand, perhaps in your system it is perfectly possible to create a user without requiring an email (for example root access account), CakePHP's validation implementation allows both of these scenarios.
If this flexibility isn't what you desire, just set the required attribute for your validation rule as true:
public $validate = array(
'email' => array(
'rule' => 'email',
'required' => true
)
);
This will satisfy your all or nothing requirement.
How do I run validation checks on a password field in CakePHP, seeing that the password is hashed before I get a chance to run any checks on it?
If you only have a single password field in your form, you will need to create a custom hash function that either does nothing, or, better, preserves the original password somewhere.
Most likely though you have two password fields in your form where the user is required to confirm the password. In this case, you perform your password validation rules on the second password field. This can automatically happen in a custom validation rule, remember that you have access to all other fields inside a validation function via $this->data. You can then confirm that the two passwords are identical as described here.
It works this way for me (in the model):
public $validate = array(
'password' => array(
'minLength' => array(
'rule' => array('minLength', '8')
)
)
);
If you want to do more validations then create a custom validation method in the appropriate model. In the custom validation method hash password this way: Security::hash($this->data['User']['password'], null, true)
I’m using Zend_Filter_Input to validate form data and want to customize the error messages, if a user does not enter a value. It is important that each field gets a different error message.
With Zend Framework 1.8.0 I used the following array for the “validator” parameter of Zend_Filter_Input:
$validators = array(
'salutation' => array(
new Zend_Validate_NotEmpty(),
Zend_Filter_Input::MESSAGES => array(
Zend_Validate_NotEmpty::IS_EMPTY => "Please enter a salutation"
)
),
/* ... */
);
Since I’ve upgraded to ZF 1.8.4, I always get the default message for empty fields (“You must give a non-empty value for field '%field%'”). Obviously Zend_Filter_Input does not call the Zend_Validate_NotEmpty validator anymore, if the field is empty.
Is there a way to change this behavior or another way to get customized “empty” messages for each field?
It seems that Zend_Filter_Input changed its behaviour when handling empty fields. Empty fields are never processed by rule validators. If a field is empty and allowEmpty is set to true, none of your validators are used. If the field is empty and allowEmpty is set to false, then the default message for empty values is set. Currently there is no way to customize this message for a specific field.
The behavior has not changed. This is a bug (http://framework.zend.com/issues/browse/ZF-7394)
try this:
$validators = array(
'salutation' => array('NotEmpty', Zend_Filter_Input::MESSAGES => 'Please enter a salutation')
);
I don't know why, but seems they changed the constant "isEmpty" with "NotEmpty" (without including it in the Zend_Validate_NotEmpty class).
Sometimes I just go nuts with Zend. :)