I’m creating an AJAX form. The problem is when I’m trying to create a input form with formhelper, my input’s name attribute is not correctly renedered in the view. Here’s my code:
$form->input('MainAttribute.'.$i.'.SubAttribute.'.$j.'.score', array('label' => '', 'options' => $scores));
I created it that way because I want SubAttribute to be inside MainAttribute. When I inspect the HTML, the name attribute of the form is cutted of like:
name="data[SuperMainAttribute]"
How can I specify the name attribute to the one that I’m planning on doing?
(e.g. data[MainAttribute][0][SubAttribute][0][score])
Edit:
Here are my model relationships:
Control hasMany MainAttribute
MainAttribute hasMany SubAttribute
The ctp is in a view of the Control Controller
In general, almost anytime you call
FormHelper::input, the first parameter will appear in one of the following formats:hasOneandbelongsToassociations:$form->input('Model.field')hasManyassociations:$form->input("Model.{$n}.field")hasAndBelongsToManyassociations:$form->input("Model.Model.{$n}.field")(In these cases,
$nis an iterator (0,1,2,3, etc.), allowing you to add multiple records tohasMany– andhasAndBelongsToMany-associated models.)Your specific case is tricky, because you want to save a
Controlrecord, and all of itsMainAttributerecords, and all of eachMainAttribute‘sSubAttributerecords. This isn’t possible without some data manipulation in the controller. The way I’d probably tackle this problem is the following.In the view:
In
ControlsController:HTH.