0

I am trying to remove a fieldset from a form. I have not found a function that does this, so I do it through an array to remove them individually.

$infofield = $form->getFieldset('company');

foreach ($infofield as $key => $field) {
       $name = preg_split("/[\[\]]/", $field->name);
       $form->removeField($name[1]);
        }

I am using this method but I do not like.

How can I recover from the $infofield only the name of the field without the jform or alternatively there is a method to romeve the fieldset ?

1
  • Why do you want to remove the fieldset element? Aug 8, 2017 at 12:24

4 Answers 4

1

I see a lot of weird answers with regular expressions and attribute handling... Why? Joomla! has a great API in JForm to do everything you need. Since it's not quite clear what you want to accomplish I'll give you three alternatives for doing things the Joomla! Way.

If you want to remove a specific field from a form it's easy to do it if you know the name of the fieldset and the field.

$form->removeField('my_field_name', 'my_fieldset_name');

If you want to get the names of all fields in a fieldset -- and possibly remove them from the form -- try:

$fields = $form->getFieldset('my_fieldset_name');
foreach ($fields as $field)
{
    $name = $field->getAttribute('name');
    // And possibly delete the field here:
    // $form->removeField($name, 'my_fieldset_name');
}

However, this removes the fields, not the fieldset. If you want to remove an entire fieldset you just need to know that before Joomla! 1.6 they were called "groups", hence the surviving method being called removeGroup:

$form->removeGroup('my_fieldset_name');

I recommend using an IDE such as PhpStorm or NetBeans and type hinting (in the form of /** @var JForm $form */)to get autocompletion of the method names. In most IDEs you can CTRL-click (macOS: CMD-click) the method name to go into its declaration in Joomla's core to understand what it does. That's how I learned how Joomla works: following the code in an IDE. It's not magic, it's Open Source Software :)

0

I could be wrong and haven't tested this, but try using:

<?php foreach ($this->form->getFieldset('company') as $field): ?>
{
    $name = preg_split("/[\[\]]/", $field->name);
    $form->removeField('$name');
}
0

If you don't care about the value inside the company field and you just want to hide it in the form, then why not just use CSS? Set the display attribute of its div to none and that's it.

0

Use getAttribute() to get an attribute of the field as it appears in XML form:

foreach ($form->getFieldset('company') as $field)
{
    $form->removeField($field->getAttribute('name'));
}

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.