Tschallacka
Tschallacka

Sometimes you need to add or remove values depending on dynamic data in your form field that you cannot define in fields.yaml

There are luckely two methods that help you with that, one is extending the form from the outside. Or, if you are fiddling with your own form you can do it form the inside. In this howto i'll focus on the one from the inside.

Within your controller make the following function:

   public function formExtendFields($form)    {
   // some code here
    }

This code gets called when it's time to render the form. Any preset values you wish to change you can do here.

to add a new field it's a simple matter of $form->addField() just like the docs say.

What the doc's don't say at this time is how to get a field and change it's values.

    $form->getField('fieldname')

returns the element you need to edit it. The properties you can change as you need to reflect your wishes.
Always fun to use the undocumented stuff :-)

chris10207
chris10207

does it work inside a pivot form ?

KurtJensen
KurtJensen

$form->getField('fieldname') gets you the field but I still have not figured out how to make the changes in this function. It seems to me this is all read-only. :-(

samal.es
samal.es

$form->getField('fieldname')->value = "myValue";

1-4 of 4