Pra*_*dan 5 validation cakephp
在cakephp3自定义验证规则中:
如何使用全局函数验证方法.
$validator->add('title', 'custom', [
'rule' => 'validate_title'
]);
Run Code Online (Sandbox Code Playgroud)
请以前做过的人吗?请给我一些示例程序.
http://book.cakephp.org/3.0/en/core-libraries/validation.html#custom-validation-rules
我尝试了以上但它不起作用..?
Ana*_*m P 13
这是使用全局函数概念进行验证的示例:
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;
}
}
Run Code Online (Sandbox Code Playgroud)
小智 5
<?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'];
?>
Run Code Online (Sandbox Code Playgroud)
使用$ context变量将当前值与其他字段(如$ value> = $ context ['data'] ['another_field_name'];
归档时间: |
|
查看次数: |
13081 次 |
最近记录: |