小编Ega*_*ega的帖子

Cakephp-3 条件 notEmpty 验证行为

我需要对一个字段进行条件验证: if other_field = 1then this_field = notBlank。我找不到办法做到这一点。我在表类中的验证器:

    public function validationDefault(Validator $validator) {
       $validator->allowEmpty('inst_name');
       $validator->add('inst_name', [
        'notEmpty' => [
            'rule' => 'checkInstName',
            'provider' => 'table',
            'message' => 'Please entar a name.'
        ],
        'maxLength' => [
            'rule' => ['maxLength', 120],
            'message' => 'Name must not exceed 120 character length.'
        ]
    ]);
    return $validator;
}

public function checkInstName($value, array $context) {
    if ($context['data']['named_inst'] == 1) {
        if ($value !== '' && $value !== null) {
            return true;
        } else { …
Run Code Online (Sandbox Code Playgroud)

validation cakephp cakephp-3.0

5
推荐指数
1
解决办法
1484
查看次数

标签 统计

cakephp ×1

cakephp-3.0 ×1

validation ×1