PatternableValidator plugin for CakePHP 3
Installation
You can install this plugin into your CakePHP application using composer., (*1)
The recommended way to install composer packages is:, (*2)
composer require k1low/patternable-validator
Usage
Before
<?php
namespace App\Model\Table;
class UsersTable extends AppTable
{
public function validationDefault(Validator $validator)
{
$validator
->integer('id')
->allowEmpty('id', 'create');
$validator
->allowEmpty('username')
->add('username', 'minLength4', [
'rule' => ['minLength', 4],
'message' => __('Validation Error: minLength4'),
])
->add('username', 'maxLength10', [
'rule' => ['maxLength', 10],
'message' => __('Validation Error: maxLength10'),
]);
$validator
->allowEmpty('password');
}
}
After
<?php
namespace App\Model\Table;
class UsersTable extends AppTable
{
public function validationDefault(Validator $validator)
{
$validator
->addPattern('id', ['integer', 'allowEmptyWhenCreate']);
$validator
->addPattern('username', ['allowEmpty', 'username_length']);
$validator
->appPattern('password', ['allowEmpty']);
}
}
AppTable settings:, (*3)
<?php
namespace App\Model\Table;
class AppTable extends Table
{
public function initialize(array $config)
{
parent::initialize($config);
$this->_validatorClass = '\PatternedValidator\Validation\Validator';
\PatternedValidator\Validation\Validator::$validationPatterns = [
'username_length' => [
'minLength4' => [
'rule' => ['minLength', 4],
'message' => __('Validation Error: minLength4'),
],
'maxLength10' => [
'rule' => ['maxLength', 10],
'message' => __('Validation Error: maxLength10'),
]
],
];
}
}
for CakePHP 2
see cakeplus/ValidationPatternsBehavior.php, (*4)