3

Im trying to prefill my subform with datarows from the database.

newcustomerform.xml

<?xml version="1.0" encoding="utf-8"?> <form addfieldprefix="Mydestiny\Component\Mydestiny\Site\Field"> <fieldset> <field name="id" type="text" readonly="true" class="readonly" default="0" description="JGLOBAL_FIELD_ID_DESC"/> <field name="contacts" type="subform" formsource="components/com_mydestiny/forms/contacts.xml" labelclass="control-label" multiple="true" buttons=" " layout="joomla.form.field.subform.repeatable-table" class="uk-table uk-table-middle" /> </fieldset> </form> 

contacts.xml

 <?xml version="1.0" encoding="utf-8"?> <form> <fieldset> <field name="account" type="radio" default="0" label="" description="" class="btn-group btn-group-yesno"> <option value="1">ja</option> <option value="0">Nee</option> </field> </fieldset> </form> 

I tried this:

$form = Form::getInstance('com_mydestiny.newcustomerform', 'contacts', array('control' => 'jform')); $input = Factory::getApplication()->input; $formValues = $input->getArray(array( 'jform' => array( 'contacts' => 'array' ) )); $prefillData = array("contacts" => array("account" => 1)); $form->bind($prefillData); 

But that doesn't do anything.
Can anyone help me?

1 Answer 1

5

You need to put the subform data one array deeper. This is because your subform has multiple attribute which means the data should be represented by multiple rows:

$prefillData = array("contacts" => array(array("account" => 1))); 

This should work fine for your case. When reading existing data, be aware that the array keys are built using subform field name suffixed with a numeric index suffix, so the data is stored like this:

array( "contacts" => array( 'contacts0' => array("account" => 1), 'contacts1' => array("account" => 1), // More rows ) ); 

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.