Field Events

If you wish to extend the capabilities of Fields in Express Forms, feel free to use any of the events listed below:

Before a Value is Set

The EVENT_BEFORE_SET_VALUE event is called right before a value for a field is being set. You can use this event to override a field value when the form is being submitted.

use Solspace\ExpressForms\fields\BaseField;
use Solspace\ExpressForms\fields\Hidden;
use Solspace\ExpressForms\events\fields\FieldSetValueEvent;

Event::on(
    BaseField::class,
    BaseField::EVENT_BEFORE_SET_VALUE,
    function (FieldSetValueEvent $event) {
        $field = $event->getField();

        if ($field instanceof Hidden && $field->getHandle() === 'session_id') {
            $event->setValue(session_id());
        }
    }
);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

When Populating a Field from Array Data

Before Building the Field Object

The EVENT_BEFORE_BUILD_FROM_ARRAY event is called before the field object is populated with values.

TIP

This is a cancelable event, so you could cancel the field building process if needed.

use Solspace\ExpressForms\factories\FieldFactory;
use Solspace\ExpressForms\events\fields\FieldBuildFromArrayEvent;

Event::on(
    FieldFactory::class,
    FieldFactory::EVENT_BEFORE_BUILD_FROM_ARRAY,
    function (FieldBuildFromArrayEvent $event) {
        // Override all field names
        $fieldData = $event->getData();
        $fieldData['name'] = 'A static field name';

        $event->setData($fieldData);
    }
);
1
2
3
4
5
6
7
8
9
10
11
12
13
14

After Building the Field Model

The EVENT_AFTER_BUILD_FROM_ARRAY event is fired after the field is built and before it's returned.

TIP

This is a cancelable event, so you could cancel the field building process if needed.

use Solspace\ExpressForms\factories\FieldFactory;
use Solspace\ExpressForms\events\fields\FieldBuildFromArrayEvent;

Event::on(
    FieldFactory::class,
    FieldFactory::EVENT_AFTER_BUILD_FROM_ARRAY,
    function (FieldBuildFromArrayEvent $event) {
        $field = $event->getField();

        // Do something here...
    }
);
1
2
3
4
5
6
7
8
9
10
11
12
Last Updated: 4/23/2019, 4:30:43 AM