Re: How to validate multiple fields with the same name?
Thanks, I can't believe the solution was so simple! With saveAll() the data gets validated just as I needed. Thanks!! The only changes I had to do were in the controller /// IN THE CONTROLLER function listProducts() data) ) { $this->Product->set($this->data); if( $this->Product->saveAll($this->data) ) { $this->Session->setFlash('Product Saved.'); $this->redirect(array('action' => 'listProducts')); } else { $this->Session->setFlash('Error saving product!'); $this->redirect(array('action' => 'listProducts')); } } ?> On Mar 10, 10:35 am, Sam Bernard wrote: > You *should* be able to do this with "saveAll". saveAll will validate your > records and then try to save all the records in a single transaction, so you > don't have to validate first and then perform a transaction. > > If for some reason you wanted to validate separately- just do: > $this-> Product->saveAll($this->data['Product'], array('validate' => > 'only')) > to just perform validation on your records > > http://book.cakephp.org/view/1031/Saving-Your-Data -- Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions. To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php
Re: How to validate multiple fields with the same name?
You *should* be able to do this with "saveAll". saveAll will validate your records and then try to save all the records in a single transaction, so you don't have to validate first and then perform a transaction. If for some reason you wanted to validate separately- just do: $this-> Product->saveAll($this->data['Product'], array('validate' => 'only')) to just perform validation on your records http://book.cakephp.org/view/1031/Saving-Your-Data -- Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions. To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php
Re: How to validate multiple fields with the same name?
Sounds like a challenge! The only way I can think of is to write a custom validation rule that checks each of the submitted Product Names - have a read at: http://book.cakephp.org/#!/view/1179/Custom-Validation-Rules Let us know how you get one! Might be handy in the future... On Mar 9, 2:30 am, Eddy wrote: > Hello people. I been trying to validate a form with several product > names but haven't been succesful. The last two days were spent > searching for a way to do this but with no results either. > The thing is I have a view with a form with a single field: > product_name and the necesary code in the controller to recieve $this->data, > and save if the data passed validation, and the model with the > > validation method to check if the name is unique, not too short or too > long and not empty. > > // IN THE VIEW list_products.ctp > > echo $this->Form->create('Product'); > > echo $this->Form->input('Product.product_name', array('label' > => 'Product Name')); > > echo $this->Form->input('Product.provider_id', array('type' => > 'hidden', 'value' => 'whatever number' )); > > echo $this->Form->end('Save Product'); > ?> > > /// IN THE CONTROLLER function listProducts() > function listProducts() > if( !empty($this->data) ) > > { > > $this->Product->set($this->data); > > if( $this->Product->validates($this->data) ) > > { > > $this->Product->begin(); > > if( $this->Product->save($this->data['Product']) ) > > { > > $this->Product->commit(); > > $this->Session->setFlash('Product Saved.'); > > $this->redirect(array('action' => 'listProducts')); > > } > > else > > { > > $this->Product->rollback(); > > $this->Session->setFlash('Error saving product!'); > > $this->redirect(array('action' => 'listProducts')); > > } > > }} > > ?> > > /// IN THE MODEL > > class Product extends AppModel { > > var $name = 'Product'; > > var $useTable = 'providers_product'; > > var $validate = array( > > 'product_name' => array( > > 'minLength'=>array( > > 'rule' => array('minLength', 5), > > 'message' => 'Too short', > > ), > > 'maxLength'=>array( > > 'rule' => array('maxLength', 200), > > 'message' => 'Too long', > > ), > > 'notEmpty' => array( > > 'rule' => 'notEmpty', > > 'message' => 'You can't leave this > empty', > > ), > > 'isUnique' => array( > > 'rule' => 'isUnique', > > 'message' => 'Name not unique', > > ), > > ), > > ); > > } > > ?> > > But now I have been trying to change this view so I can type 1 to 5 > product names at once. > So my view now looks like this (which I found how to do it in the > cookbook): > > echo $this->Form->create('Product'); > > echo $this->Form->input('Product.0.product_name', array('label' => > 'Product Name')); > > echo $this->Form->input('Product.1.product_name', array('label' => > 'Product Name')); > > echo $this->Form->input('Product.2.product_name', array('label' => > 'Product Name')); > > echo $this->Form->input('Product.3.product_name', array('label' => > 'Product Name')); > > echo $this->Form->input('Product.4.product_name', array('label' => > 'Product Name')); > > echo $this->Form->input('Product.provider_id', array('type' => > 'hidden', 'value' => 'whatever number' )); > > echo $this->Form->end('Save'); > > ?> > This way I have $this->data looking like this: > Array > ( > [Product] => Array > ( > [0] => Array > ( > [product_name] => name 1 > ) > > [1] => Array > ( > [product_name] => name 2 > ) > > [2] => Array > ( > [product_name] => name 3 > ) > > [3] => Array > ( > [product_name] => name 4 > ) > > [4] => Array > ( > [product_name] => name 5 > ) > > [provider_id] => whatever number > ) > ) > > Now, the problem is that I can't f
How to validate multiple fields with the same name?
Hello people. I been trying to validate a form with several product names but haven't been succesful. The last two days were spent searching for a way to do this but with no results either. The thing is I have a view with a form with a single field: product_name and the necesary code in the controller to recieve $this- >data, and save if the data passed validation, and the model with the validation method to check if the name is unique, not too short or too long and not empty. // IN THE VIEW list_products.ctp Form->create('Product'); echo $this->Form->input('Product.product_name', array('label' => 'Product Name')); echo $this->Form->input('Product.provider_id', array('type' => 'hidden', 'value' => 'whatever number' )); echo $this->Form->end('Save Product'); ?> /// IN THE CONTROLLER function listProducts() data) ) { $this->Product->set($this->data); if( $this->Product->validates($this->data) ) { $this->Product->begin(); if( $this->Product->save($this->data['Product']) ) { $this->Product->commit(); $this->Session->setFlash('Product Saved.'); $this->redirect(array('action' => 'listProducts')); } else { $this->Product->rollback(); $this->Session->setFlash('Error saving product!'); $this->redirect(array('action' => 'listProducts')); } } } ?> /// IN THE MODEL array( 'minLength'=>array( 'rule' => array('minLength', 5), 'message' => 'Too short', ), 'maxLength'=>array( 'rule' => array('maxLength', 200), 'message' => 'Too long', ), 'notEmpty' => array( 'rule' => 'notEmpty', 'message' => 'You can't leave this empty', ), 'isUnique' => array( 'rule' => 'isUnique', 'message' => 'Name not unique', ), ), ); } ?> But now I have been trying to change this view so I can type 1 to 5 product names at once. So my view now looks like this (which I found how to do it in the cookbook): Form->create('Product'); echo $this->Form->input('Product.0.product_name', array('label' => 'Product Name')); echo $this->Form->input('Product.1.product_name', array('label' => 'Product Name')); echo $this->Form->input('Product.2.product_name', array('label' => 'Product Name')); echo $this->Form->input('Product.3.product_name', array('label' => 'Product Name')); echo $this->Form->input('Product.4.product_name', array('label' => 'Product Name')); echo $this->Form->input('Product.provider_id', array('type' => 'hidden', 'value' => 'whatever number' )); echo $this->Form->end('Save'); ?> This way I have $this->data looking like this: Array ( [Product] => Array ( [0] => Array ( [product_name] => name 1 ) [1] => Array ( [product_name] => name 2 ) [2] => Array ( [product_name] => name 3 ) [3] => Array ( [product_name] => name 4 ) [4] => Array ( [product_name] => name 5 ) [provider_id] => whatever number ) ) Now, the problem is that I can't figure out how to validate $this- >data[Product]. The model always return as if the validation was correct and the controller proceeds to save. I know that I have to change the controller to saveall() instead of saving a single field but still, no matter how I set the data before validation it doesn't validate. Please help!!! -- Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions. To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php