10

I have a requirement to generate a page/form based on the user's list of items. As is, the user can specify a list of items that have a text description of the item, and whether it's a text or list field type. (Imagine a list of equipment that can be checked out, and the list of items to check varies by the type of equipment. There are certainly some shared items, but they vary from equipment type, model, etc.). So, instead of simply loading an existing XML file from the models\forms\ folder and running with it, can a new fieldset and a series of new fields be added on the fly.

If so,

  1. How is that accomplished ?
  2. Where would be the correct place to do it so that MVC treats it as though it were in the "standard" form XML?
  3. How best to retrieve a list of those fields for displaying in the View?

models\myform.php

$form = $this->loadForm('com_mycomponent.myform', 'myform', array('control' => 'jform', 'load_data' => $loadData));

controllers/myform.php

// Get the user data.
$data = JFactory::getApplication()->input->get('jform', array(), 'array');

// Validate the posted data.
$form = $model->getForm();
if (!$form) {
    JError::raiseError(500, $model->getError());
    return false;
}
...
// Validate the posted data.
$data = $model->validate($form, $data);
...
// Attempt to save the data.
$return = $model->save($data);

1 Answer 1

4

I was working with jForm and methods setField() and getFieldset() to set and retrieve the fields, but Joomla was throwing errors. While trying to add a list Joomla was unable to parse the XML until I added option_on="Yes" and option_off="Yes". (I'm not sure what/why these are necessary, but with them, the below code works).

models\myform.php

 public function getForm($data = array(), $loadData = true)
{
    // Get the form.
    $form = $this->loadForm('com_mycomponent.mymodel', 'myform', array('control' => 'jform', 'load_data' => $loadData));
    if (empty($form)) {
        return false;
    }
    $element = new SimpleXMLElement('<fieldset name="myFieldset">
        <field name="myfield1" type="list"
        label="My List"
        default="2"
        option_on="Yes"
        option_off="Yes">
        <option value="1">Low</option>
        <option value="2">Normal</option>
        <option value="3">High</option>
        </field>
        <field name="myfield2" type="text" label="My field 1" class="inputbox" size="30" />
        <field name="myfield3" type="text" label="My field 2" class="inputbox" size="30" />
    </fieldset>');
    $form->setField($element);
    return $form;
}

views/myview/tmpl/default.php

$this->form->getFieldset('myFieldset'), true)
// Loop through these results and display them accordingly
$myFieldset = $this->form->getFieldset('myFieldset');
if(count($myFieldset)){
    foreach($myFieldset as $field) {
        $field_name = $field->getAttribute('name');
        echo $this->form->getLabel($field_name);
        echo $this->form->getInput($field_name);
    }

}

controllers/myview.php

// Get the user data.
$data = JFactory::getApplication()->input->get('jform', array(), 'array');
/* $data DOES contain my input fields*/
// The model/table doesn't contain columns for my custom fields, so the data to be saved has to be manipulated here to "go somewhere permanent".

// Validate the posted data.
$form = $model->getForm();
/* $form DOES contain my input fields*/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.