5
votes

In cakephp3 Custom Validation Rules:

How to Use a global function validation method.

$validator->add('title', 'custom', [
    'rule' => 'validate_title'
]);

Please any one done before? Pls Provide me the some example program.

http://book.cakephp.org/3.0/en/core-libraries/validation.html#custom-validation-rules

I tried the above but it doesn't work..?

5
Please don't down vote this, I've been trying this for the past day full. But I didn't get any solutions yet..!Prassanna D Manikandan

5 Answers

14
votes

here is an Example for validation using global function concept:

namespace App\Model\Table;

use Cake\ORM\Table;
use Cake\Validation\Validator;

    public function validationDefault(Validator $validator) {
        $validator->add('title',[
        'notEmptyCheck'=>[
        'rule'=>'notEmptyCheck',
        'provider'=>'table',
        'message'=>'Please enter the title'
         ]
        ]);
       return $validator;
    }

    public function notEmptyCheck($value,$context){
        if(empty($context['data']['title'])) {
            return false;
        } else {
            return true;
        }
    }
7
votes
<?php

namespace App\Model\Table;

use App\Model\Entity\Member;
use Cake\ORM\Query;
use Cake\ORM\RulesChecker;
use Cake\ORM\Table;
use Cake\Validation\Validator;

class MembersTable extends Table {

    public function initialize(array $config) {
        parent::initialize($config);

        $this->table('members');
    }

    public function validationDefault(Validator $validator) {
        $validator
                ->add("cedula", [
                    "custom" => [
                        "rule" => [$this, "customFunction"], //add the new rule 'customFunction' to cedula field
                        "message" => "Enter the value greater than 1000"
                    ]
                        ]
                )
                ->notEmpty('cedula');
        return $validator;
    }

    public function customFunction($value, $context) {
        return $value > 1000;
    }

}

Use $context variable to comare current value with other fields like $value >= $context['data']['another_field_name'];

?>

Use $context variable to comare current value with other fields like $value >= $context['data']['another_field_name'];

1
votes

This actually work for me (Cakephp 3.x). It's a good way if your condition is simple:

<?php

namespace App\Form;

use Cake\Form\Form;
use Cake\Validation\Validator;

class addPostForm extends Form {

  protected function _buildValidator(Validator $validator) {
    return $validator->allowEmpty('my_input', function ($context) {
                      return (@context['data']['an_other_input'] != "");
                    });
  }

  public function setErrors($errors) {
    $this->_errors = $errors;
  }

}

Here the form input my_input is allow to be empty only if a second input an_other_input is completed. You can get form data with the variable $context['data'].

0
votes

This is what worked for me for CakePHP 3.0. The important parameter here is the 'provider', which is not very clear in the document examples.

$validator->add('title', 'custom', [
    'rule' => 'validate_title',
    'provider' => 'table',
    'message' => 'some error message'
]);

Then define your function. The variable passed to the function is

$check='title'

:

public function validate_title($check)
{
  ...
}
-3
votes

here is an Example for validation.

In your Table.

public function validationDefault(Validator $validator)
{
    $validator = new Validator();
    $validator
        ->notEmpty('username', 'A username is required')
        ->add('username', [
            'emailValid' => [
                'rule' => ['email', true],
                'message' => 'You must provide a valid email'
            ],
            'emailUnique' => [
                'message' => 'The email you provided is already taken. Please provide another one.',
                'rule' => 'validateUnique', 
                'provider' => 'table'
            ]
        ]);
    return $validator;
}