【问题标题】:How to create custom validation for Modelless Forms in cakephp3如何在 cakephp3 中为无模型表单创建自定义验证
【发布时间】:2016-12-14 00:33:36
【问题描述】:

我想为我的字段创建自定义验证。表单已从 cakephp 表单类(无模型表单)扩展而来。

注意:请记住,这是无模型表单,因此没有表或数据库。

问题是当我创建验证时它给了我这个错误。

方法 isValidCardNumber 不存在

这是我的代码:

<?php
namespace App\Form;

use Cake\Core\Configure;
use Cake\Form\Form;
use Cake\Form\Schema;
use Cake\Network\Exception\SocketException;
use Cake\Validation\Validator;
use SagePay\SagePayDirectPayment;

/**
 * Payment Form
 */
class PaymentForm extends Form
{

    /**
     * Define the schema
     *
     * @param  \Cake\Form\Schema $schema The schema to customize.
     * @return \Cake\Form\Schema The schema to use.
     */
    protected function _buildSchema(Schema $schema)
    {
        return $schema
            ->addField('name', 'string')
            ->addField('card_number', 'string')
            ...
            ...;
    }

    /**
     * Define the validator
     * 
     * @param  \Cake\Validation\Validator $validator The validator to customize.
     * @return \Cake\Validation\Validator The validator to use.
     */
    protected function _buildValidator(Validator $validator)
    {
        return $validator   
            ->notEmpty('name', 'Please enter the name on card.')    
            ->notEmpty('card_number', 'string')
            ->add('card_number', 'isValidCardNumber', [
                'rule' =>  ['isValidCardNumber'],
                'message' => 'Card number should be 16 long number.'
            ])
            ...
            ...;

    }

    protected function isValidCardNumber($data, array $context)
    {
        debug($data);
        die;
    }
}

【问题讨论】:

    标签: cakephp cakephp-3.2


    【解决方案1】:

    回答我的问题,我在 Cake Validation http://api.cakephp.org/3.0/class-Cake.Validation.Validation.html 中找到了一种用于支付卡的 CC() 验证方法。

    但我决定将答案留给其他开发人员,以便他们了解无模型表单自定义验证的工作原理。


    我需要创建自己的自定义支付类。

    <?php
    namespace App\Form;
    
    use App\Validation\PaymentCardValidation;  // <= Need to add the custom payment class
    /**
     * Payment Form
     */
    class PaymentForm extends Form
    {
    
        /**
         * Define the validator
         * 
         * @param  \Cake\Validation\Validator $validator The validator to customize.
         * @return \Cake\Validation\Validator The validator to use.
         */
        protected function _buildValidator(Validator $validator)
        {       
            return $validator   
                // assign the custom PaymentCardValidation to the provider   
                ->provider('custom', 'App\Validation\PaymentCardValidation')
    
                ->notEmpty('name', 'Please enter the name on card.')    
                ->notEmpty('card_number', 'string') 
                ->add('card_number', 'cardNumber', [
                    'rule' =>  'cardNumber',
                    'message' => 'Card number should be 16 long number.',
                    'provider' => 'custom', // <= Use the provider
                ])
    ...
    ...
    

    在我的 PaymentCardValidation 类中

    <?php
    namespace App\Validation;
    
    use Cake\Validation\Validation;
    
    /**
     * PaymentCard Validation 
     * 
     * Provide's rules for payment cards
     */
    class PaymentCardValidation extends Validation
    {
    
        /**
         * Check if card number.
         * 
         * @param  string $check The value to check.
         * @return bool
         */
        public static function cardNumber($check)
        {
            debug($check);
            die;
        }
    }
    

    【讨论】:

      【解决方案2】:

      您需要将其设置为提供者。

      Add your object as provider.

      $validator = new Validator();
      
      // Use an object instance.
      $validator->provider('custom', $myObject);
      
      // Use a class name. Methods must be static.
      $validator->provider('custom', 'App\Model\Validation');
      

      Then use it.

      use Cake\ORM\Table;
      use Cake\Validation\Validator;
      
      class UsersTable extends Table
      {
      
          public function validationDefault(Validator $validator)
          {
              $validator
                  ->add('role', 'validRole', [
                      'rule' => 'isValidRole', // <--- method
                      'message' => __('You need to provide a valid role'),
                      'provider' => 'table', // <--- provider
                  ]);
              return $validator;
          }
      
          public function isValidRole($value, array $context)
          {
              return in_array($value, ['admin', 'editor', 'author'], true);
          }
      
      }
      

      代码是从官方文档复制粘贴的,根据您的用例需要进行更改。

      【讨论】:

      • 谢谢。但是我说这个类是从无模型表单扩展而来的,它没有表,也没有数据库。
      • 在文档页面上花费的时间超过一秒钟,并实际阅读了该页面,而不仅仅是查看示例。那里也解释了如何设置其他提供程序。 default 和 table 是内置的,其他需要定义。
      • @Fury $validator-&gt;provider('custom', $myObject); $myObject$this - 如果这是您希望验证器用作提供者的类,则表单对象,从文档复制的示例代码不是表格 -特定类。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多