I want to create a custom validation for my fields. The Form has been extended from cakephp Form class (Modelless Forms).
Note: Bear in mind this is Modelless Forms so there is no table or database.
The problem is when I create the validation it give me this error.
Method isValidCardNumber does not exist
Here is my code:
<?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;
}
}