Re: [jgen] Joomla 3: Dynamically add a hidden field to a JForm object without a hidden.xml

148 views
Skip to first unread message

Mark Dexter

unread,
Jan 18, 2014, 12:53:42 PM1/18/14
to Joomla! General Development
Have you tried the form load() method? I think that is what you can use to load fields and fieldsets from plain strings or from XML files. See https://github.com/joomla/joomla-cms/blob/staging/libraries/joomla/form/form.php#L669. Good luck. Mark


On Sat, Jan 18, 2014 at 9:19 AM, Clubnite <tin...@googlemail.com> wrote:
Hello,

anyone who could explain how to add a hidden field element to a JForm object in onXXXPrepareForm handler without creating and using a hidden.xml form field file? I seem not to get this managed on my own.

Here is what i tried:

$hidden = new SimpleXMLElement('<field type="hidden" name="test" value="test" default=""/>');

// 1st try
$form->setField($hidden, 'hidden', true);

// dump form fieldsets and fields for testing
foreach ($form->getFieldsets() as $group => $fieldset)
{
   echo '<pre>group: ' . print_r("{$group}: ", true) . '</pre>';

   $fields = $form->getFieldset($group);

   foreach ($fields as $field)
   {
      echo '<pre>field: ' . print_r("{$field->name}: {$field->value}", true) . '</pre>';
   }

}

// 2nd try
$hidden->setForm($form);

// dump form fieldsets and fields for testing
foreach ($form->getFieldsets() as $group => $fieldset)
{
   echo '<pre>group: ' . print_r("{$group}: ", true) . '</pre>';

   $fields = $form->getFieldset($group);

   foreach ($fields as $field)
   {
      echo '<pre>field: ' . print_r("{$field->name}: {$field->value}", true) . '</pre>';
   }

}

What am i doin' wrong?

--
You received this message because you are subscribed to the Google Groups "Joomla! General Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to joomla-dev-gene...@googlegroups.com.
To post to this group, send an email to joomla-de...@googlegroups.com.
Visit this group at http://groups.google.com/group/joomla-dev-general.
For more options, visit https://groups.google.com/groups/opt_out.

Message has been deleted
Message has been deleted

Clubnite

unread,
Jan 18, 2014, 1:35:38 PM1/18/14
to joomla-de...@googlegroups.com
Hello Mark,

ye, i even tried this method without success.

To answer my own question and providing one solution:

I could manage to load the XML string after i prepended a form and fielset tag. So the properly working code in my case is:

foreach (array('field1', 'field2', 'field3') as $fieldname)
{
   $form->setField(new SimpleXMLElement(''.
      '<form>'.
         '<fieldset name="hidden">'.
            '<field type="hidden" name="' . $fieldname . '" required="true" readonly="true" default="" />'.
         '</fieldset>'.
      '</form>'
  )
      ,null
      ,true
   );
}

If anyone knows an easier solution or a has a suggestion to improve the above code, i was highly appreciated to know about it.
Reply all
Reply to author
Forward
0 new messages