I'm using cake 2.3.8 version. I have a registration form where users can enter in a username and password in the form.
My model validation looks like:
public $validate = array(
'username' => array(
'required' => array(
'rule' => array('notEmpty'),
'message' => 'A username is required'
),
'alphanumeric' => array(
'rule' => 'alphaNumeric',
'message' => 'Usernames must only contain letters and numbers.'
)
),
'password' => array(
'required' => array(
'rule' => array('notEmpty'),
'message' => 'A password is required'
)
) );
Now the weird thing is in my site when I enter in a username with space in it, the validation is displayed twice. But when I use the Family registration form and enter a username with space in it, the validation error only displays once. Does anyone know what could be the issue?
Generally this is because validation is triggered twice. What exactly causes it to be triggered twice is pretty hard to tell without seeing more code (especially involved controllers, components, behaviours and models).
Check whether you are maybe calling Model::validates() manually, additionally to validation that is triggered by the models save operation, or maybe you are even calling it twice manually.
It could also be triggered by a third party component or a behaviour or whatever... you'll need to do some debugging.
In my case it happened because in the controller:
I did a $this->Model->save() which returned false (first
validation)
Then, to show validation error I did
$this->Model->invalidFields() which validates again the fields
(second time) and returned the message
To fix it I changed $this->Model->invalidFields() to $this->Model->validationErrors to get the error message
I also got this issue once. But, in my case I found out that, though I had written validate code in the Model, I validated it again in the controller because of which it was showing double validation errors. If you have done the same, then remove
$this->Model->validates()
from controller.
Related
I use standalone Symfony components in my app (and without Twig).
My HTML form contains two fields ('new_password' & 'confirm_new_password').
The following validation works fine:
$validator = Validation::createValidator();
$violations = $validator->validate($new_password, [
new Length(['min' => 4]),
new Regex([
'pattern' => '/\d/',
'match' => true,
'message' => 'Password must contain at least one number'
])
]);
if (0 !== count($violations)) {
...
}
I would like to add validation of password confirmation fields as well
The 'Form' component by Symfony allows to create, process and reuse forms, but this is far beyond of what I want to do. I found that 'RepeatedType' field by Symfony can do this but seems to be using the 'Form' component.
How can I simply add password confirmation to my validation script?
there is a constraint call identicalTo that looks quite the same as explained by RiggsFolly. So you call this constraint and give it both fields' values.
I need to validate that a customer is using a valid subdomain name as well as making sure some other required fields are present. I have created the following rule in my model class:
public function rules()
{
return array(
array(',c_customerSubdomain, c_custAdminUser,c_adminPassword,c_adminContact', 'required', 'message' => "{attribute} is TEST required."),
array('c_customerSubdomain', 'match', 'pattern' => '/^[a-z0-9_]*$/', 'message' => "Customer Subdomain should contain only alphanumeric and underscore.", "allowEmpty" => false),
);
}
I am testing whether or not the rule is firing by changing the message for required values to: {attribute} is TEST required.
When I submit the form with all of the field blank, the result is as expected.
However, when I populate the field for subdomain with an illegal value, such as "ASD# a", I am expecting to get a validation error. However, instead, the rules bypass all of the other validation errors and attempt to save the model.
I have gone through the validations in Laravel. I have taken so many validation rules from Laravel Validations Rules
I want to user required_unless rule for following conditions.
$rules = array(
'facebookID' => 'alpha_num',
'googleID' => 'alpha_num',
'email' => 'required_unless:facebookID,null|required_unless:facebookID,null|email|max:32',
'password' => 'required_unless:facebookID,""|required_unless:googleID,""|max:20',
);
I want to add validation rule of email and password is only required if signup with facebook or google is not attempted.
I believe we can use required_unless or required_if
Please help me to solve this problem.
The laravel validation rule only accept value, that's mean required_unless:facebookID,null is evaluated as: the field is required unless facebookID='null'. So it's not a case you would need.
My suggest solution is using require_without_all:
'email' => 'required_without_all:facebookID,googleID',
'password' => 'required_without_all:facebookID,googleID'
The reference link for you: https://laravel.com/docs/5.1/validation#rule-required-without-all
Regards
I used the Symfony 2 documentation to create a simple registration.
But now i have two little problems. The password fields, which I created with the Form Builder, be time in plain text.
The second problem is that the action of the form is not used the correct route. When I press the submit button, I get the standard page.
Has anyone of you an idea, which may be related?
If you still parts of the code needed, I like to add these.
Greetings
did you set it to a password type? eg
$builder->add('password', 'password)
Or can do repeated field
$builder->add('password', 'repeated', array(
'type' => 'password',
));
When you build the form do you set action?
$form = $this->createForm(new Type(), $type, array(
'action' => $this->generateUrl('your_route'),
));
I creating a custom validation rule in my cakephp model.
'article' => array(
'rule' => '/^[a-z0-9#.,&; ]{2,255}$/i',
'required' => true,
'allowEmpty' => false,
'message' => 'Alphabets and numbers only(3,255).'
),
This works fine. But It stops working, throws a error in model, when I add forward slash [/]. I can't understand why forward causes a problem.
I appreciate any help.
Thanks.
As stated in your other question, read about preg_match() patterns in the php manual. This function is used internally in the framework.