CakePHP - Validating multiple fields with same rule

LogixMaster

Let's say I have a dozen radio button fields which I want to validate against two common rules.

   'valid'=> array(
        'rule' => array('inList', array('yes','no')),
        'message' => 'Illegal Choice Detected'

   ),
   'required'=> array(
       'rule' => array('notEmpty'),
       'message' => 'Field is required.'
   ),

How can I do this without having to assign each validation rule to each field ?

[EDIT]

For those how prefer some spoon feeding as I myself like sometimes, Here is how I did it based on Burzum's answer!

   public function beforeValidate($options = []) {
        $fields = ['field_1','field_2','field_3','etc'];
        foreach ($fields as $field) {            
            $this->validate[$field]['required'] = array(
                    'rule' => array('notEmpty'),
                    'message' => 'Field is required.'
            );
            $this->validate[$field]['legal'] = array(
                    'rule' => array('inList', array('yes', 'no')),
                    'message' => 'An illegal choice has been detected, please contact the website administrator.'
            );
        }
        return true;
    }
burzum

Add them in a foreach loop in beforeValidate()

public function beforeValidate($options = []) {
    $fields = ['field1', '...'];
    foreach ($fields as $field) {
        // Add your rule(s) here to the field
        $this->validate[$field]['myRule'] = ['...'];
    }
    return true;
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Add Same Rule Multiple Fields

From Dev

Add Same Rule Multiple Fields

From Dev

CakePHP 2 - Validating password fields

From Dev

Javascript validating multiple fields

From Dev

Cakephp validating file upload "multiple"

From Dev

Cakephp validating file upload "multiple"

From Dev

Validating two fields at the same time

From Dev

Validating two fields at the same time

From Dev

Validating multiple forms on the same page

From Dev

validating multiple inputs at the same time

From Dev

Error message issue with validating multiple fields of a bean

From Dev

Makefile: Same Rule for multiple Targets

From Dev

Validating multiple instances of same partial view

From Dev

Validating multiple instances of same partial view

From Dev

rule for validating $ amount?

From Dev

Cross reference to multiple names in the same grammar rule

From Dev

Multiple Foreign Keys for the same business rule

From Dev

Foolproof multiple validators on the same fields

From Dev

Set same value on multiple fields

From Dev

post multiple fields with same name

From Dev

SQL Multiple Joining with same fields

From Dev

Validating multiple check boxes in a HTML form that can't be the same name

From Dev

Multiple sessions for different instances of Cakephp in the same domain

From Dev

Saving Multiple record related to the same Model in Cakephp

From Dev

Multiple Associations to the Same Model in CakePHP 3

From Dev

how to validate multiple entries for same field in cakephp

From Dev

Cakephp 3.0 - Multiple relations to the same model

From Dev

Multiple sessions for different instances of Cakephp in the same domain

From Dev

Saving Multiple record related to the same Model in Cakephp

Related Related

HotTag

Archive