Setting Validation Rules at Runtime in CakePHP -


my problem right model has set of validation rules so:

var $validate = array(     'title' => array(         'rule' => 'notempty'     ),     'uri' => array(         'slugged' => array(             'rule' => '/^[a-z0-9-_]+$/i',             'message' => 'this field should contain characters, numbers, dashes , underscores'         ),         'uniqueurl' => array(             'rule' => array('uniqueurl'),             'message' => 'a page has acquired url'         )     ),     'meta_keywords' => array(         'rule' => 'notempty'     ),     'meta_description' => array(         'rule' => 'notempty'     ),     'layout' => array(         'rule' => 'notempty'     ) ); 

the problem in model has hasone relationship controller inserts data it. want not require title, uri , layout page. how do it?

i have post model , set page values there.

array (     [post] => array         (             [title] => data[post][title]             [body] =>   post body          )      [category] => array         (             [category] => array                 (                     [0] => 1                     [1] => 2                     [2] => 3                     [3] => 4                 )          )      [page] => array         (             [meta_keywords] => data[page][meta_keywords]             [meta_description] => data[page][meta_description]         )  ) 

i controller set info page model

$this->data['page']['title'] = $this->data['post']['title']; 

it turns this:

array (     [post] => array         (             [title] => data[post][title]             [body] =>   post body          )      [category] => array         (             [category] => array                 (                     [0] => 1                     [1] => 2                     [2] => 3                     [3] => 4                 )          )      [page] => array         (             [meta_keywords] => data[page][meta_keywords]             [meta_description] => data[page][meta_description]             [title] => data[post][title]         )  ) 

my problem not require page field when saving. post belongsto page.

i don't require [page][layout] when saving post post uses default view of method in post controller. page uses static pages , require them when creating page, not when creating post.

you're preprocessing data before goes validation, you're taking control away validation. seem making decision (in code) whether or not fields need artificially populated rendering parts of validation redundant , should remove them. if have multiple validations in php, you're going end confused.

where or how more cleanly? wellbeforevalidate might 'correct' place this, wherever best fits logic of application. function should come before elegance.


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -