0

I want to use a flexible field name for a field across different forms thru my extension.

My custom example field looks like this:

defined('_JEXEC') or die;

use Joomla\CMS\Factory;
use Joomla\CMS\Form\FormHelper;

FormHelper::loadFieldClass('list');

class JFormFieldExample extends \JFormFieldList
{

    public $type = 'Example';
    protected static $options = array();    
    protected function getOptions()

    {
        // Accepted modifiers
        $hash = md5($this->element);

        if (!isset(static::$options[$hash]))
        {
            static::$options[$hash] = parent::getOptions();

            $db = Factory::getDbo();

            $options[] = JHTML::_('select.option', '1', JText::_('1'), 'value', 'text');
            $options[] = JHTML::_('select.option', '2', JText::_('2'), 'value', 'text');
        }

        return static::$options[$hash];
    }
}   

When using it inside a form xml like this:

<?xml version="1.0" encoding="utf-8"?>
<form>
    <fieldset addfieldpath="/administrator/components/com_mycomp/models/fields">    
        <field name="example" multiple="multiple" type="example" default="0"  label="example" />
    </fieldset>
</form>

it gets outputted like this:

<?php echo $this->form->getLabel('example'); ?>
<div class="form-controls">
    <?php echo $this->form->getInput('example');?>
</div>

and this is the name of the field that gets outputted:

jform[example]

I want to use that field in different scenarios where field names like this should be possible:

jform[example]
jform[example][]
jform[table1][example][]

How can this be accomplished?

if changing the XML to this:

<field name="[table1][example]" multiple="multiple" type="example" default="0"  label="example" />

i get this:

jform[[table1][example]][]
0

jform[example][]

This would depend on whether the field has multiple attribute enabled or not. In your case it does so it already should look like this.

jform[table1][example][]

Put the field in a field group:

<fields name="table1">
    <field name="example" multiple="multiple" type="example" default="0"  label="example" />
</fields>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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