Skip to content

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
            [$this, 'emptyMyField']

        return $form;

    public function emptyMyField(EntityFormInterface $form)
        $country = $form->getField('country_id')->getValue();
        if (! $this->isExampleAvailable($country)) {

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.