Field Events

To extend Freeform Field functionality use the following events:

Parsing a POST value 3.12+

This event is called when a POST value is being assigned to a field. Use this event to transform a value before it is set for the field. Freeform uses this event to handle Drag & Drop file upload values transforming their UID's to ID's.

use Solspace\Freeform\Library\Composer\Components\FieldInterface;
use Solspace\Freeform\Events\Fields\TransformValueEvent;
use Solspace\Freeform\Fields\TextField;

Event::on(
  FieldInterface::class,
  FieldInterface::EVENT_BEFORE_VALIDATE,
  function (TransformValueEvent $event) {
    $form = $event->getForm();
    $field = $event->getField();
    
    // Only transform text field values
    if (!$field instanceof TextField) {
        return;
    }

    // Make all posted text field values always uppercase
    $event->setValue(
      strtoupper($event->getValue())
    );
  }
)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

Before a field is validated

This event is fired for every active Freeform field right before it's being validated when a form is submitted. Use this event to attach your own validators for fields.

use Solspace\Freeform\Services\FieldsService;
use Solspace\Freeform\Events\Fields\ValidateEvent;

Event::on(
  FieldsService::class,
  FieldsService::EVENT_BEFORE_VALIDATE,
  function (ValidateEvent $event) {
    $form = $event->getForm();
    $field = $event->getField();

    // Only perform extra field validation on our trusty "specificFormHandle" form
    if ($form->getHandle() !== 'specificFormHandle') {
      return;
    }

    // Add special extra validation for the "firstName" field
    if ($field->getHandle() === 'firstName' && $field->getValue() === 'John') {
      $field->addError("Sorry, John, we didn't make the rules.");
    }
  }
)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

After a field is validated

This event is fired for every active Freeform field right after it has been validated.

use Solspace\Freeform\Services\FieldsService;
use Solspace\Freeform\Events\Fields\ValidateEvent;

Event::on(
  FieldsService::class,
  FieldsService::EVENT_AFTER_VALIDATE,
  function (ValidateEvent $event) {
    $form = $event->getForm();
    $field = $event->getField();

    // Perform some actions here
  }
)
1
2
3
4
5
6
7
8
9
10
11
12
13

Before saving a field

This event is fired before a field is saved.

use Solspace\Freeform\Services\FieldsService;
use Solspace\Freeform\Events\Fields\SaveEvent;

Event::on(
  FieldsService::class,
  FieldsService::EVENT_BEFORE_SAVE,
  function (SaveEvent $event) {
    $fieldModel = $event->getModel();
    $isNew = $event->isNew();

    // Perform some actions here
  }
)
1
2
3
4
5
6
7
8
9
10
11
12
13

After saving a field

This event is fired after a field is saved.

use Solspace\Freeform\Services\FieldsService;
use Solspace\Freeform\Events\Fields\SaveEvent;

Event::on(
  FieldsService::class,
  FieldsService::EVENT_AFTER_SAVE,
  function (SaveEvent $event) {
    $fieldModel = $event->getModel();
    $isNew = $event->isNew();

    // Perform some actions here
  }
)
1
2
3
4
5
6
7
8
9
10
11
12
13

Before deleting a field

use Solspace\Freeform\Services\FieldsService;
use Solspace\Freeform\Events\Fields\DeleteEvent;

Event::on(
  FieldsService::class,
  FieldsService::EVENT_BEFORE_DELETE,
  function (DeleteEvent $event) {
    $form = $event->getForm();
    $field = $event->getField();

    // Perform some actions here
  }
)
1
2
3
4
5
6
7
8
9
10
11
12
13

After deleting a field

use Solspace\Freeform\Services\FieldsService;
use Solspace\Freeform\Events\Fields\DeleteEvent;

Event::on(
  FieldsService::class,
  FieldsService::EVENT_AFTER_DELETE,
  function (DeleteEvent $event) {
    $form = $event->getForm();
    $field = $event->getField();

    // Perform some actions here
  }
)
1
2
3
4
5
6
7
8
9
10
11
12
13
Last Updated: 9/27/2021, 5:59:26 PM