doureng1083 2015-02-10 23:22
浏览 13
已采纳

CakePHP 3 - 创建新的Validator

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'
                ]
            ])
}

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?

  • 写回答

1条回答 默认 最新

  • dr9379 2015-02-11 07:34
    关注

    Validator naming conventions

    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

    Inheriting validation rules

    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!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料