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 wegame打不开英雄联盟
  • ¥15 公司的电脑,win10系统自带远程协助,访问家里个人电脑,提示出现内部错误,各种常规的设置都已经尝试,感觉公司对此功能进行了限制(我们是集团公司)
  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多