In this blog post I'm going to explain how to add different validations for same field in different function in
This is doing for the same database table same column.
In CakePHP (1.3) we can call modle using
controller.
For example
in one view(greaterthanadd.ctp) i wanted to add number which is grater than 18.
and in other view(lesserthanadd.ctp) i wanted to add number which is less than 18.This is doing for the same database table same column.
In CakePHP (1.3) we can call modle using
$this->Model_name;So we can access the validation array in controller
$this->Model_name->validate['fieldName']['ruleName'];In this example I am using table called numbers and field call number.
function greaterthanadd() { if (!empty($this->data)) { //************ //validation strat //************ $this->Number->validate['number']['greaterthan'] = array( 'rule' => array('comparison', '>=', 18), 'message' => 'Must be at least 18.'); //************ //validation end //************ if ($this->Number->save($this->data)) { $this->Session->setFlash(__('The number has been saved', true)); $this->redirect(array('action' => 'index')); } else { $this->Session->setFlash(__('The number could not be saved. Please, try again.', true)); } } } function lesserthanadd() { if (!empty($this->data)) { //************ //validation strat //************ $this->Number->validate['number']['greaterthan'] = array( 'rule' => array('comparison', '<=', 18), 'message' => 'Must be at least 18.'); //************ //validation end //************ if ($this->Number->save($this->data)) { $this->Session->setFlash(__('The number has been saved', true)); $this->redirect(array('action' => 'index')); } else { $this->Session->setFlash(__('The number could not be saved. Please, try again.', true)); } } }
you are missing one crucial information for readers (especially beginners): what cake version is this for.
ReplyDeleteThank you for the comment I'll do it.
DeleteTo be honest, I really do not see the point of changing the model validation, which is a data structure within the model, in a controller.
ReplyDeleteA major point of OO is surely to encapsulate data for a class within that class. If the validate array becomes protected instead of public in a future version of Cake, then your code will break.
Surely it would be preferable to place this logic in a model class member so that if things change, then your code only needs to change in one place.
Thank you very much for your comment.
DeleteThe problem was I cloud not be able to validate same filed in different views in different way.
for example
in one view(greaterthanadd.ctp) i wanted to add number which is grater than 18.
and in other view(lesserthanadd.ctp) i wanted to add number which is less than 18.
This is doing for the same database table same column.
Is there any different way of doing this than accessing the model validation in controller ?
Please reply for this comment.
This comment has been removed by the author.
ReplyDeleteCake PHP development is based on MVC framework.If we look at the advantages of Cake PHP Web Development, then most probably is this would be the most preferred option compare to others.
ReplyDelete