-1

I have a problem in cakephp validation. Below I describe what I want to do:

<select>
<option> Value 1</option>
<option> Value 2</option>
</select>

Input 1 : <input type="text" name="val1" value="" />
Input 2 : <input type="text" name="val2" value="" />

Suppose if I select "Value 1" from dropdown then "Input 1" textbox will be validated and if I select "Value 2" from dropdown then "Input 2" textbox will be validated.

How would I do this in cakephp validation ? Please help me..this is very urgent.plsssss

user3744754
  • 39
  • 1
  • 1
  • 5
  • did you see this:: http://stackoverflow.com/questions/11309498/cakephp-validating-an-input-field-depending-on-an-option-selected-from-a-dropdo – Sudhir Bastakoti Jul 31 '14 at 06:17

1 Answers1

0

Try to use the Cake FormHelper class to build forms - it does nearly everything for you and is very customisable http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html

Html

<select name='select_val'>
    <option value='val_1'>Value 1</option>
    <option value='val_2'>Value 2</option>
</select>

Input 1: <input type="text" name="val1" value="" />
Input 2: <input type="text" name="val2" value="" />

MyModel.php

public $validate = array(
    'select_val' => array(
        'rule' => 'customValidationForInputs',
        'message' => 'Invalid value'
    )
);

public function customValidationForInputs($check) {
    if ($check['select_val'] == 'val_1') {
        //access the value in 'val1' through $this->data[$this->alias]['val1']
        //perform some checks on the value
        if (the value in 'val1' passes your checks) {
            return TRUE;
        }
        else {
            return FALSE;
        }
    }
    if ($check['select_val'] == 'val_2') {
        //access the value in 'val2' through $this->data[$this->alias]['val2']
        //perform some checks on the value
        if (the value in 'val2' passes your checks) {
            return TRUE;
        }
        else {
            return FALSE;
        }
    }
    //if we reach here then the value of the select was not either of the ones specified so presume this is invalid
    return FALSE;
}

For more on validation have a look at http://book.cakephp.org/2.0/en/models/data-validation.html

Kvothe
  • 1,819
  • 2
  • 23
  • 37