2

我想为我的字段创建自定义验证。表单已从 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;
    }
}
4

2 回答 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;
    }
}
于 2016-08-08T11:55:10.047 回答
1

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

将您的对象添加为提供者。

$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');

然后使用它。

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);
    }

}

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

于 2016-08-08T09:40:30.393 回答