Re: How to build form with multiple records?
Hey guys, I just had a look at the core files and patched something up this afternoon... Now I can build multi-record forms as Travis suggested by using some kind of loop and $form-input(Model..$id..field); works pretty well, only validation and saving of course still needs to be handled by my controllers... I posted an enhancement on trac: https://trac.cakephp.org/ticket/3044, diff-files are there... -- Ben --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Cake PHP group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~--~~~~--~~--~--~---
Re: How to build form with multiple records?
Hi Geoff did you have any luck with getting this feature to work? Can I also thank you for your posts in this group on multiple saves as they've been very helpful. Coincidentally I am trying to get many RoomsTasks submitted from a form at once, and I have managed to loop through POSTed data and successfuly save it, from a form with inputs in form - data[RoomsTask][213][quantity_of_unit] data[RoomsTask][218][quantity_of_unit] data[RoomsTask][224][quantity_of_unit] etc(where I use the 224 to specify the task_id and I manipulate this in the controller before saving: foreach($this-data['RoomsTask'] as $taskIdNow = $roomsTask){ // only if user checked the task, obviously: if ($roomsTask['task_id'] == 1) { // perform save code $this-RoomsTask-create(); // assign all the fields with correct names // Todo: taken from a hidden field - take from session? $roomsTask['room_id'] = $this-data['Meta']['room_id']; $roomsTask['task_id'] = $taskIdNow; // we need to swap this value over, as it's a unique task we are adding! // priority? $this-RoomsTask-set($roomsTask); // this adds the quantity_of_unit also. //pr($this-RoomsTask-data); // Validation - tricky as need to remember the values too :/ // if (!$this-RoomsTask-validates()) { // if it is Quantity - we must inform user of error, otherwise, we just don't Save it as a checkbox/hidden should validate! if (isset($this-RoomsTask-invalidFields['RoomsTask'] ['quantity_of_unit'])) { $this-RoomsTask-invalidFields['RoomsTask'][$taskIdNow] ['quantity_of_unit'] = $this-RoomsTask-invalidFields['RoomsTask'] ['quantity_of_unit']; unset($this-RoomsTask-invalidFields['RoomsTask'] ['quantity_of_unit']); } } else { $this-RoomsTask-save(); $flashMsg .= 'saved '; //pr($this-RoomsTask-data); } } But I cannot get the validation and re-display of values working, since setting the $this-RoomsTask-invalidFields['RoomsTask'] [$taskIdNow]['quantity_of_unit'] doesnt work. any ideas on how to re-display in the view values and error messages for appropriate fields? many thanks Luke On Jul 26, 12:27 am, Geoff Ford [EMAIL PROTECTED] wrote: Hmm.. Very interesting. I had not seen that before. I might have a play later on and see if I can get it to work. I'll let you know how I go. Geoff --http://lemoncake.wordpress.com On Jul 26, 8:49 am, chewie124 [EMAIL PROTECTED] wrote: Ahhh... ok. Thanks for the input. I was grepping thru the helpers.php library in the cake core, and it looks like in the setFormTag function , if there's a 3rd level branch,ie FormerEmployer. 2.name, the '2' will get parsed and tagged as the 'modelId'. Seems like though, the propigation hasn't made it back to the form helper, that can use the modelId to generate the input tags as you described above. Thanks for the input! --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Cake PHP group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~--~~~~--~~--~--~---
Re: How to build form with multiple records?
sorry I was calling invalidFields wrong way there - think I have fixed it! On Aug 8, 12:45 pm, luke BAKING barker [EMAIL PROTECTED] wrote: Hi Geoff did you have any luck with getting this feature to work? Can I also thank you for your posts in this group on multiple saves as they've been very helpful. Coincidentally I am trying to get many RoomsTasks submitted from a form at once, and I have managed to loop through POSTed data and successfuly save it, from a form with inputs in form - data[RoomsTask][213][quantity_of_unit] data[RoomsTask][218][quantity_of_unit] data[RoomsTask][224][quantity_of_unit] etc(where I use the 224 to specify the task_id and I manipulate this in the controller before saving: foreach($this-data['RoomsTask'] as $taskIdNow = $roomsTask){ // only if user checked the task, obviously: if ($roomsTask['task_id'] == 1) { // perform save code $this-RoomsTask-create(); // assign all the fields with correct names // Todo: taken from a hidden field - take from session? $roomsTask['room_id'] = $this-data['Meta']['room_id']; $roomsTask['task_id'] = $taskIdNow; // we need to swap this value over, as it's a unique task we are adding! // priority? $this-RoomsTask-set($roomsTask); // this adds the quantity_of_unit also. //pr($this-RoomsTask-data); // Validation - tricky as need to remember the values too :/ // if (!$this-RoomsTask-validates()) { // if it is Quantity - we must inform user of error, otherwise, we just don't Save it as a checkbox/hidden should validate! if (isset($this-RoomsTask-invalidFields['RoomsTask'] ['quantity_of_unit'])) { $this-RoomsTask-invalidFields['RoomsTask'][$taskIdNow] ['quantity_of_unit'] = $this-RoomsTask-invalidFields['RoomsTask'] ['quantity_of_unit']; unset($this-RoomsTask-invalidFields['RoomsTask'] ['quantity_of_unit']); } } else { $this-RoomsTask-save(); $flashMsg .= 'saved '; //pr($this-RoomsTask-data); } } But I cannot get the validation and re-display of values working, since setting the $this-RoomsTask-invalidFields['RoomsTask'] [$taskIdNow]['quantity_of_unit'] doesnt work. any ideas on how to re-display in the view values and error messages for appropriate fields? many thanks Luke On Jul 26, 12:27 am, Geoff Ford [EMAIL PROTECTED] wrote: Hmm.. Very interesting. I had not seen that before. I might have a play later on and see if I can get it to work. I'll let you know how I go. Geoff --http://lemoncake.wordpress.com On Jul 26, 8:49 am, chewie124 [EMAIL PROTECTED] wrote: Ahhh... ok. Thanks for the input. I was grepping thru the helpers.php library in the cake core, and it looks like in the setFormTag function , if there's a 3rd level branch,ie FormerEmployer. 2.name, the '2' will get parsed and tagged as the 'modelId'. Seems like though, the propigation hasn't made it back to the form helper, that can use the modelId to generate the input tags as you described above. Thanks for the input! --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Cake PHP group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~--~~~~--~~--~--~---
Re: How to build form with multiple records?
On Jul 25, 6:27 pm, Geoff Ford [EMAIL PROTECTED] wrote: Hmm.. Very interesting. I had not seen that before. I might have a play later on and see if I can get it to work. I'll let you know how I go. Geoff --http://lemoncake.wordpress.com I've also been interested in seeing what it would mean to have the core support for fields in such a format: data[Model][?= $i; ?][id] I don't think it's trivial. I wonder if the devs consider this too edge case, it would surely be slick. -- Travis --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Cake PHP group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~--~~~~--~~--~--~---
Re: How to build form with multiple records?
Luke, I did find what you described but could not get it to work for the input name, only the input value. I recently posted a blog entry on multi-record forms, including redisplaying the errors, although it sounds like you got it sorted, using a similar approach to what I have used in my blog post. Travis, this seems to be coming up again and again on the mailing list and I think it would be a great addition to the core FormHelper. As FormHelper already supports this format for the input value, it would seem that it is doable for the input name and id. When I get time I will try to do this and submit it as an enhancement to the trac. Geoff On 8/9/07, Travis Cline [EMAIL PROTECTED] wrote: On Jul 25, 6:27 pm, Geoff Ford [EMAIL PROTECTED] wrote: Hmm.. Very interesting. I had not seen that before. I might have a play later on and see if I can get it to work. I'll let you know how I go. Geoff --http://lemoncake.wordpress.com I've also been interested in seeing what it would mean to have the core support for fields in such a format: data[Model][?= $i; ?][id] I don't think it's trivial. I wonder if the devs consider this too edge case, it would surely be slick. -- Travis -- http://lemoncake.wordpress.com --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Cake PHP group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~--~~~~--~~--~--~---
How to build form with multiple records?
Hi All - I'm have a job-board app that has a Resume model which hasMany FormerEmployer models (like for employment history). There's plenty of discussion on how to save the data on the controller side (at least, that I've been able to find), but my question is, is how do you build up the form, so when I hit submit, it will send the Resume and the associated FormerEmployer (more than one) data fields that I can handle back in the controller? Can somebody fill in the missing pieces for me? ?php echo $form-input('first_name'); ? ?php echo $form-input('last_name'); ? h2Former Employers/h2 ?php foreach ($this-data['FormerEmployer'] as $employer): // THIS ISN'T RIGHT... ? ?php echo $form-input('FormerEmployer.name'); ? ?php endforeach; ? I'm using cake 1.2... Thanks, Rich --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Cake PHP group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~--~~~~--~~--~--~---
Re: How to build form with multiple records?
Ahhh... ok. Thanks for the input. I was grepping thru the helpers.php library in the cake core, and it looks like in the setFormTag function , if there's a 3rd level branch,ie FormerEmployer. 2.name, the '2' will get parsed and tagged as the 'modelId'. Seems like though, the propigation hasn't made it back to the form helper, that can use the modelId to generate the input tags as you described above. Thanks for the input! --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Cake PHP group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~--~~~~--~~--~--~---
Re: How to build form with multiple records?
You will have to create the inputs the old fashioned way, and create the empyer records as an array that you can loop over when posted back. In the controller function use $this-set('formerEmployers', $this- FormerEmployer-findAllByApplicantId($applicant_id)); Then in the view, make sure you include FormerEmplyee id if it is an edit and do something like ?php for ($i=0; $i count($formerEmployers); $i++): ? input type='text' name='data[FormerEmployers][?= $i; ?][name]' value='?= $formerEmplyers[$i]['FormerEmployer']['name']; ? / input type='hidden' name='data[FormerEmployers][?= $i; ?][id]' value='?= $formerEmplyers[$i]['FormerEmployer']['id']; ? / ?php endfor; ? Then in the post back you can loop over the records with foreach ($this-data['FormerEmployers'] as $employer){ $this-FormerEmployer-create(); // you need this line to stop the model repeatedly saving over the initial record $toSave = array(FormerEmployer = $employer); $this-FormerEmployer-Save($toSave); } Geoff -- http://lemoncake.wordpress.com On Jul 26, 7:42 am, chewie124 [EMAIL PROTECTED] wrote: Hi All - I'm have a job-board app that has a Resume model which hasMany FormerEmployer models (like for employment history). There's plenty of discussion on how to save the data on the controller side (at least, that I've been able to find), but my question is, is how do you build up the form, so when I hit submit, it will send the Resume and the associated FormerEmployer (more than one) data fields that I can handle back in the controller? Can somebody fill in the missing pieces for me? ?php echo $form-input('first_name'); ? ?php echo $form-input('last_name'); ? h2Former Employers/h2 ?php foreach ($this-data['FormerEmployer'] as $employer): // THIS ISN'T RIGHT... ? ?php echo $form-input('FormerEmployer.name'); ? ?php endforeach; ? I'm using cake 1.2... Thanks, Rich --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Cake PHP group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~--~~~~--~~--~--~---
Re: How to build form with multiple records?
Hmm.. Very interesting. I had not seen that before. I might have a play later on and see if I can get it to work. I'll let you know how I go. Geoff -- http://lemoncake.wordpress.com On Jul 26, 8:49 am, chewie124 [EMAIL PROTECTED] wrote: Ahhh... ok. Thanks for the input. I was grepping thru the helpers.php library in the cake core, and it looks like in the setFormTag function , if there's a 3rd level branch,ie FormerEmployer. 2.name, the '2' will get parsed and tagged as the 'modelId'. Seems like though, the propigation hasn't made it back to the form helper, that can use the modelId to generate the input tags as you described above. Thanks for the input! --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Cake PHP group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~--~~~~--~~--~--~---