2

I'd like to render the same fieldset multiple times for batch create and batch update. Is it possible to do this with JForms?

Currently I have a form like this:

<?xml version="1.0" encoding="UTF-8"?>
<form name="customer_registration_form" title="COM_CANTEEN_CUSTOMER_REGISTRATION_TITLE" description="COM_CANTEEN_CUSTOMER_REGISTRATION_DESCRIPTION" buttonLabel="COM_CANTEEN_CUSTOMER_REGISTRATION_BUTTON_LABEL" class="customer-registration">
    <fields name="customer_registration">
        <fieldset name="customer_registration">
            <field name="name" type="text" required="true" size="80"
                validate="text" minLength="4" maxLength="80" pattern="^\p{L}+(?:[\p{Zs}-]\p{L}+)+$" 
                label="COM_CANTEEN_CUSTOMER_REGISTRATION_NAME_LABEL" description="COM_CANTEEN_CUSTOMER_REGISTRATION_NAME_DESCRIPTION" message="COM_CANTEEN_CUSTOMER_REGISTRATION_NAME_MESSAGE" />
            <field name="age" type="integer" required="true" default="6" first="1" last="150" step="1"
                validate="integer" min="1" max="150"
                label="COM_CANTEEN_CUSTOMER_REGISTRATION_AGE_LABEL" description="COM_CANTEEN_CUSTOMER_REGISTRATION_AGE_DESCRIPTION" message="COM_CANTEEN_CUSTOMER_REGISTRATION_AGE_MESSAGE" />
            <field name="permanent_orderer" type="checkbox" default="1"
                label="COM_CANTEEN_CUSTOMER_REGISTRATION_PERMANENT_ORDERER_LABEL" description="COM_CANTEEN_CUSTOMER_REGISTRATION_PERMANENT_ORDERER_DESCRIPTION" message="COM_CANTEEN_CUSTOMER_REGISTRATION_PERMANENT_ORDERER_MESSAGE" />
        </fieldset>
    </fields>
</form>

Which I render with this layout:

<?php
extract($displayData);
JHtml::_('behavior.tooltip');
?>
<form action="<?php echo $action; ?>"
      method="post" name="<?php echo $form->getAttribute('name'); ?>" class="<?php echo $form->getAttribute('class'); ?> basic-form-layout">
    <legend><?php echo JText::_($form->getAttribute('title')); ?></legend>
    <?php
        echo JText::_($form->getAttribute('description'));
        foreach (array_keys($form->getFieldsets()) as $fieldset)
            echo $form->renderFieldset($fieldset);
    ?>
    <?php echo JHtml::_('form.token'); ?>
    <div class="control-group">
        <div class="control-label">&nbsp;</div>
        <div class="controls">
            <button type="submit"><?php echo JText::_($form->getAttribute('buttonLabel')); ?></button>
        </div>
    </div>
</form>

By sending the form I got a customer_registration[field] array. I'd like to get a customer_registration[i][field] or customer_registration[field][i] array instead. Is this supported somehow, or should I write a workaround?

The rendering part is okay, I guess I have to call renderFieldset multiple times, but how can I override the customer_registration to get customer_registration[i]? The only solution I could come up is rendering the fieldset, finding the field names in the resulted html, and overriding them with regex, or with simplexml. Any other idea? Or is this already implemented somehow and I was superficial by checking the feature?

2
  • Can you explain a little more on the final result that you want?. Is it that your form has to be duplicated as many time as the user needs?
    – John
    Mar 7, 2017 at 10:37
  • @John Added more info to the question.
    – inf3rno
    Mar 7, 2017 at 17:37

1 Answer 1

1

There is no need to workaround. I checked the code, there is a control option which you can give by the instantiation of JForm. If it is set, then it will be the root of the field names.

It should look like the following with vanilla joomla (I tested it with my own wrapper classes):

JHtml::_('behavior.tooltip');

$fileName = 'myform.xml';
$input = JFactory::getApplication()->input;
$isPost = $input->getMethod() == 'POST';
if ($isPost)
    $data= $input->post->getArray();
?><form action="<?php echo JUri::getInstance(); ?>"><?php
    for ($i = 0; $i < 13; ++$i) {
        $randomName = uniqid($fileName . '.');
        $form = JForm::getInstance(
            $randomName, 
            JPATH_COMPONENT . '/my/templates/' . $fileName, 
            array('control' => $i)
        );
        if ($isPost) {
            $filtered = $form->filter($data[$i]);
            foreach ($filtered as $fieldset => $fields)
                foreach ($fields as $field => $value)
                    $form->setValue($field, $fieldset, $value);
        }
        foreach (array_keys($form->getFieldsets()) as $fieldset)
            echo $form->renderFieldset($fieldset);
    }
    echo JHtml::_('form.token'); ?>
    <button type="submit">send</button>
</form>

Your Answer

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

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