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 <[email protected]> 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 > <?php > > 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() > <?php > 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 > <?php > > 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): > <?php > > 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 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 [email protected] For more options, visit this group at http://groups.google.com/group/cake-php
