Reputation: 1542
Hi again :) I'd like to create a new Validating Data. When I bake my model, It generates a validation
UsersTable.php -I'm posting only part for example..
public function validationDefault(Validator $validator)
{
$validator
->add('id', 'valid', ['rule' => 'numeric'])
->allowEmpty('id', 'create');
return $validator;
}
I would create another.. In it I write validations to another form.. Edit for example
public function validationEditUser(Validator $validator){
...
}
That's how I call validation?
<?= $this->Form->create($user,['context' => ['valitador' => 'validationEditUsers']]); ?>
Is there a process of inheritance among the validations that I created??
UPDATED
I will demonstrate what is currently happening.. This Validation I'm using for my Form Users/Add
public function validationDefault(Validator $validator)
{
$validator
->requirePresence('password', 'create')
->notEmpty('password',"Field's empty")
->add('password',[
'minLength' => [
'rule' => ['minLength', 5],
'last' => true,
'message' => 'Password is low, add more characters'
]
]);
return $validator;
}
Now, This valdation, I'm using for my form Users/Edit
public function validationEditUsers(Validator $validator){
//Its empty
}
That's the way I'm telling to the form context which validator to use
<?= $this->Form->create($user,['context' => ['valitador' => 'editUsers']]) ?>
What is happening is that message "Password is low.." (belong to "ValidationDefault") is triggered in my form Edit.. I thought that using context would make just validation using ValidationEditUsers.. Why doesn't it happen?
Upvotes: 2
Views: 5147
Reputation: 60453
You do not "call" validation in a form, you just tell the form context which validator to use (for checking required fields). The passed name should omit the validation
part of the method name, and start lowercased, so in your case editUser
.
$this->Form->create($user, [
'context' => [
'validator' => 'editUser'
]
]);
http://book.cakephp.org/3.0/en/views/helpers/form.html#using-custom-validators
The same rules apply when specifying which validator to use when marshalling request data.
$Table->newEntity($this->reqeust->data, [
'validate' => 'editUser'
]);
http://book.cakephp.org/3.0/en/orm/saving-data.html#validating-data-before-building-entities
And no, there is no inhertiance involved, if need/want this, you have to take care of this yourself, by invoking the necessary validation methods, something along the lines of
public function validationDefault(Validator $validator)
{
$validator
->add(/* ... */);
return $validator;
}
public function validationEditUser(Validator $validator)
{
$this
->validationDefault($validator)
->add(/* ... */);
return $validator;
}
This would make the editUser
validator "inherit" the defaults rules before applying additional ones.
Update
As mentioned initially, the form helper doesn't do any validation whatsoever, all it uses the validator for is to check which fields are required, and then set the appropriate HTML element attributes. Actual validation happens when marshalling request data, the form helper only displays errors stored on the entity.
Please refer to the newEntity()
example and the link above to figure how to define the validator to use for actual validation. On a side note, you have a typo in your code, it must be validator
, not valitador
!
Upvotes: 4