Validating Multiple sets of POST data in Cakephp -


i've got cakephp project 'addresses' table following structure:

create table `addresses` ( `id` int(11) not null auto_increment, `user_id` int(11) default null, `name` varchar(50) not null, `company` varchar(50) not null, `address1` varchar(50) not null, `address2` varchar(50) default null, `city` varchar(40) not null, `state` varchar(2) not null, `country` varchar(2) not null, `zip` varchar(5) not null, primary key (`id`) ) 

there page in project asks user both shipping address , billing address, , im not how structure names of form inputs allow multiple instances of same database fields on 1 page

in view, i've attempted use alias seperate 2 instances of address fields

i.e.-

<?=$this->form->input('shipaddress.zip', array('label' => 'zip code'));?> ... <?=$this->form->input('billaddress.zip', array('label' => 'zip code'));?> 

then in view, tried seperate 2 instances, validate both, , set appropriate $this->validationerror values display errors correct field views

// place in arrays proper model name ['address'] $ship_array['address'] = $this->request->data['shipaddress']; $bill_array['address'] = $this->request->data['billaddress'];  //set data model, validate against model, change model name in validationerrors  match aliased fields, , remove validationerrors ['address'] $this->address->set($ship_array); $shipping_valid = $this->address->validates(array('fieldlist' => array('name', 'company', 'address1', 'address2', 'city', 'state', 'country', 'zip'))); $this->validationerrors['shipaddress'] = $this->validationerrors['address']; $this->validationerrors['address'] = array();  //do again billing address fields $this->address->set($bill_array); $billing_valid = $this->address->validates(array('fieldlist' => array('name', 'company', 'address1', 'address2', 'city', 'state', 'country', 'zip'))); $this->validationerrors['billaddress'] = $this->validationerrors['address']; $this->validationerrors['address'] = array(); 

unfortunately, doesnt appear work, , i'm afraid i've gone far trying make work...

can give kick in right direction on how can done properly?

figured out how on own...

in /app/model created 'shippingaddress.php' , 'billingaddress.php', both extend "address"

//shippingaddress.php <?php   app::uses('address', 'model'); class shippingaddress extends address {  }   //billingaddress.php <?php   app::uses('address', 'model'); class billingaddress extends address {  }  

to prevent new models using tables named after them, edit parent address.php , set $usetable both extended models use addresses table

//address.php ... public $usetable = 'addresses'; ... 

then matter of inserting 2 instances of input fields view... no renaming models, no modifying validationerrors, works :)


Comments