6

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..?

1
  • 2
    Please don't down vote this, I've been trying this for the past day full. But I didn't get any solutions yet..! Commented Oct 20, 2015 at 10:06

5 Answers 5

15

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;
        }
    }
Sign up to request clarification or add additional context in comments.

2 Comments

This isn't inputting the message for me on fail, is there a reason for this?
I don't understand this. There's no class definition and no file name. Could you please complete that information?
7
<?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'];

Comments

1

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'].

Comments

0

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)
{
  ...
}

Comments

-2

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

2 Comments

I'm asking a custom validate global function. I tried this all! Without provider concept!
This code is taken from a project which I am currently working on and this works just fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.