PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, February 2, 2022

[FIXED] Cakephp 3.x Custom Validation Rules Creation and Using

 February 02, 2022     cakephp, validation     No comments   

Issue

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


Solution

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


Answered By - Ananthaselvam P
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing