Setting a field value
A form field value is set with the setValue($value)
or the empty()
methods.
Setting a field value has to be done in one of the form:*:updated
form modification hooks.
public function apply(EntityFormInterface $form): EntityFormInterface
{
$form->registerModificationListener(
'empty-value-if-field-disabled',
'form:billing:updated',
[$this, 'emptyMyField']
);
return $form;
}
public function emptyMyField(EntityFormInterface $form)
{
$country = $form->getField('country_id')->getValue();
if (! $this->isExampleAvailable($country)) {
$form->getField('my_field')->empty();
$form->getField('other_field')->setValue('nope');
}
}
Updating form fields in other modifier hook callbacks besides the form:*:updated
ones has no effect.
This is noteworthy, because the form:*:updated
hooks are not called during the preceding request.
Currently, it is not possible to reliably set a field value in a form modification hook function on the preceding request.
To enforce a field value on a preceding request, a Magento plugin or event observer outside the Checkout form API has to be used.