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]][]
1
  • I guess I'm confused. different scenarios are produced by different form XML definitions. So if you write the xml for a form that uses the custom field that way, it'll get rendered that way. If you use the same xml, you get the same output.
    – Arlen
    Commented Feb 10, 2020 at 15:15

1 Answer 1

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 and acknowledge you have read our privacy policy.

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