Strawberry
Strawberry

Reputation: 67928

How do I validate the value in a multidimensional array?

$year = $this->Form->input('exp_year', array(
                                'type' => 'date',
                                'maxYear' => date('Y', strtotime('+ 7 years')),
                                'minYear' => date('Y'),
                                'dateFormat' => 'Y',
                                'empty' => '----',
                                'label' => false

                                    )
    ));

So I am trying to get the expiration year, and this is posting as:

            'exp_year' => array(
                    'year' => '2016'
                ),

I tried, and didn't see anything in the documentation for such a situation.

    'exp_year' => array( 'year' => array(
                'required' => array(
                    'rule' => array('numeric'),
                    'message' => 'Must select an expiration year'
        )
           )
     ),

    'exp_year' => array( array(
                'required' => array(
                    'rule' => array('numeric'),
                    'message' => 'Must select an expiration year'
        )
                 )
     ),

Upvotes: 0

Views: 666

Answers (1)

Costa
Costa

Reputation: 4969

It doesn't look like cake's built-in data validation rules currently handle the "Y" dateFormat. (list of built-in validation rules)

And even if it did, there's no easy way to specify dynamic values in class properties so you wouldn't be able to use date("Y") in the Model $validate declaration, you'd have use constants which are specified elsewhere in your app... bottom line: inelegant and difficult to maintain.

So easiest way is to use a custom validation function, which are very simple to implement: Adding your own Validation Methods

Upvotes: 2

Related Questions