如何在cakephp3中为Modelless Forms创建自定义验证

时间:2016-08-08 09:35:29

标签: cakephp cakephp-3.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;
    }
}

2 个答案:

答案 0 :(得分:1)

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

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

}

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

答案 1 :(得分:1)

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

但我决定为其他开发人员保留答案,以便他们了解Modelless Forms自定义验证的工作原理。

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

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