Reputation: 645
In cakephp3 Custom Validation Rules:
How to Use a global function validation method.
$validator->add('title', 'custom', [
'rule' => 'validate_title'
]);
Please any one done before? Pls Provide me the some example program.
http://book.cakephp.org/3.0/en/core-libraries/validation.html#custom-validation-rules
I tried the above but it doesn't work..?
Upvotes: 6
Views: 16334
Reputation: 5057
This is what worked for me for CakePHP 3.0. The important parameter here is the 'provider', which is not very clear in the document examples.
$validator->add('title', 'custom', [
'rule' => 'validate_title',
'provider' => 'table',
'message' => 'some error message'
]);
Then define your function. The variable passed to the function is
$check='title'
:
public function validate_title($check)
{
...
}
Upvotes: 0
Reputation: 71
This actually work for me (Cakephp 3.x). It's a good way if your condition is simple:
<?php
namespace App\Form;
use Cake\Form\Form;
use Cake\Validation\Validator;
class addPostForm extends Form {
protected function _buildValidator(Validator $validator) {
return $validator->allowEmpty('my_input', function ($context) {
return (@context['data']['an_other_input'] != "");
});
}
public function setErrors($errors) {
$this->_errors = $errors;
}
}
Here the form input my_input
is allow to be empty only if a second input an_other_input
is completed.
You can get form data with the variable $context['data']
.
Upvotes: 1
Reputation: 161
<?php
namespace App\Model\Table;
use App\Model\Entity\Member;
use Cake\ORM\Query;
use Cake\ORM\RulesChecker;
use Cake\ORM\Table;
use Cake\Validation\Validator;
class MembersTable extends Table {
public function initialize(array $config) {
parent::initialize($config);
$this->table('members');
}
public function validationDefault(Validator $validator) {
$validator
->add("cedula", [
"custom" => [
"rule" => [$this, "customFunction"], //add the new rule 'customFunction' to cedula field
"message" => "Enter the value greater than 1000"
]
]
)
->notEmpty('cedula');
return $validator;
}
public function customFunction($value, $context) {
return $value > 1000;
}
}
Use $context variable to comare current value with other fields like $value >= $context['data']['another_field_name'];
?>
Use $context variable to comare current value with other fields like $value >= $context['data']['another_field_name'];
Upvotes: 7
Reputation: 382
here is an Example for validation using global function concept:
namespace App\Model\Table;
use Cake\ORM\Table;
use Cake\Validation\Validator;
public function validationDefault(Validator $validator) {
$validator->add('title',[
'notEmptyCheck'=>[
'rule'=>'notEmptyCheck',
'provider'=>'table',
'message'=>'Please enter the title'
]
]);
return $validator;
}
public function notEmptyCheck($value,$context){
if(empty($context['data']['title'])) {
return false;
} else {
return true;
}
}
Upvotes: 15
Reputation: 785
here is an Example for validation.
In your Table.
public function validationDefault(Validator $validator)
{
$validator = new Validator();
$validator
->notEmpty('username', 'A username is required')
->add('username', [
'emailValid' => [
'rule' => ['email', true],
'message' => 'You must provide a valid email'
],
'emailUnique' => [
'message' => 'The email you provided is already taken. Please provide another one.',
'rule' => 'validateUnique',
'provider' => 'table'
]
]);
return $validator;
}
Upvotes: -2