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