Re: How to handle hasMany data entry
You can place some fields that are actually not in the model in the validate array. The problem with your case, it that you do not have a definite limit. There is an option you can set in the $validate .. 'on' => 'create' and 'on' => 'update' associated with the array (same level as 'rule'). I have seen it in the code of validation.php in the cake1.2 branch and it relies on the definition of $id in the $this->data of the Model ..maybe it's on way (but really never tried it)... On Aug 7, 1:19 am, nagarjuna <[EMAIL PROTECTED]> wrote: > Thanks for the advice. Unfortunately this will not work for me > because it chokes on: > > $this->Mark->invalidate('mark_number'.$i, "Mark already exists); > > I have two types of marks for input (NewMarks and ExistingMarks), and > they are stored in the data matrix under $this->data['NewMarks'] not > under $this->data['Marks']. > So with the above statement, cake tries to look for the variable > data['Marks'] and invalidate it and I get the error message > > Undefined property: ObservationsController::$Mark [CORE\app > \controllers\observations_controller.php, line 134] > > Is there any way to invalidate a field directly in the controller if > the field is not attached to any model field? > For example, is there anyway to access the form helper directly in the > controller a.la. > $this->form->error('NewMarks'.'mark_number'.$i, "Mark already > exists" ); > > The above won't work, but you get the idea. By the way, I am using > the formHelper and validation from 1.2 --~--~-~--~~~---~--~~ 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 handle hasMany data entry
Thanks for the advice. Unfortunately this will not work for me because it chokes on: $this->Mark->invalidate('mark_number'.$i, "Mark already exists); I have two types of marks for input (NewMarks and ExistingMarks), and they are stored in the data matrix under $this->data['NewMarks'] not under $this->data['Marks']. So with the above statement, cake tries to look for the variable data['Marks'] and invalidate it and I get the error message Undefined property: ObservationsController::$Mark [CORE\app \controllers\observations_controller.php, line 134] Is there any way to invalidate a field directly in the controller if the field is not attached to any model field? For example, is there anyway to access the form helper directly in the controller a.la. $this->form->error('NewMarks'.'mark_number'.$i, "Mark already exists" ); The above won't work, but you get the idea. By the way, I am using the formHelper and validation from 1.2 --~--~-~--~~~---~--~~ 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 handle hasMany data entry
What you could possibly do (maybe it's not the best), is to loop on the single validation of the model by setting your different field value to it : In controller: $this->Mark->validate = array('mark_number' => your validation); // this is required if you have other validations in the Mark model (to eventually process other validation before) $fail = false; for($i = 0; i < $nbmark; $i++) { $this->Mark->data['Mark']['mark_number'] = $this->data['Mark'] ['mark_number'.$i]; if(!$this->Mark->validates()) { $fail = true; $this->Mark->invalidate('mark_number'.$i, "Mark already exists); } } if(fail) { $this->render(..) exit(); } hope this helps On Aug 6, 6:25 pm, nagarjuna <[EMAIL PROTECTED]> wrote: > I have the following setup: Class Observation hasMany Marks. > The Marks Class only has one relevant field: mark_number > I am working on the add form for my Observations, and the problem is > that I do not know in advance how many Marks a given observation will > have. I initialize the form with one mark field called data['Marks'] > ['mark_number0'], and then if the user has multiple marks he can click > on a "add another mark" link which dynamically creates a new field > data['Marks']['mark_number1'] and so on. > So far so good. My problem comes to dealing with data validation. > First, I need to check that each mark_number is unique and doesn't > already exist in the database and if not I need to invalidate the > field. I would like to put the validation routine inside my Mark > model, but it only has a single field mark_number, and not > mark_number0, etc, so I cannot invalidate the appropriate field there. > > I can code all of this by hand, but it is ugly and feels wrong as if I > am not taking advantage of all the cakey goodness. What is the best > practice way to deal with this situation. > > Thanks for any advice --~--~-~--~~~---~--~~ 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 handle hasMany data entry
I have the following setup: Class Observation hasMany Marks. The Marks Class only has one relevant field: mark_number I am working on the add form for my Observations, and the problem is that I do not know in advance how many Marks a given observation will have. I initialize the form with one mark field called data['Marks'] ['mark_number0'], and then if the user has multiple marks he can click on a "add another mark" link which dynamically creates a new field data['Marks']['mark_number1'] and so on. So far so good. My problem comes to dealing with data validation. First, I need to check that each mark_number is unique and doesn't already exist in the database and if not I need to invalidate the field. I would like to put the validation routine inside my Mark model, but it only has a single field mark_number, and not mark_number0, etc, so I cannot invalidate the appropriate field there. I can code all of this by hand, but it is ugly and feels wrong as if I am not taking advantage of all the cakey goodness. What is the best practice way to deal with this situation. Thanks for any advice --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---