Hi Stephane,
You can modify your form in getForm function to add new fields. I often do that like this.
class MyComponentModelMyItem extends JModelAdmin
{
public function getForm($data = array(), $loadData = true)
{
// Get the form.
$form = $this->loadForm('com_mycomponent.myitem', 'myitem', array('control' => 'jform', 'load_data' => $loadData));
if (empty($form))
{
return false;
}
// Modify the $form object here to change field attributes or add/remove fields.
$myFields = array(
// Array of dynamic fields.
);
// Add new fields to a new fieldset.
$xml = '<fieldset name="custom_fields">';
foreach ($myFields as $field)
{
$xml .= '<field name="' . $field->name . '" type="' . $field->type . '" label="' . $field->label . '" description="' . $field->desc . '" />';
}
$xml .= '</fieldset>';
// Add new fieldset to form.
$element = new SimpleXMLElement($xml);
$form->setField($element);
// We can also change field attribute here.
$form->setFieldAttribute('somefield', 'required', 'false');
// Or remove a field.
$form->removeField('removeme', 'somefieldset');
$data = $this->loadFormData();
$form->bind($data);
return $form;
}
}
To validate your dynamic fields you also need to do the same to validate() function, otherwise the new fields are not there to validate.
public function validate($form, $data, $group = null)
{
// Add dynamic fields or modify existing fields here before validating the inputs.
return parent::validate($form, $data, $group);
}
I know it is really good, if anybody has a better solution please let us know.