Skip to main content

Field Events

To extend Freeform Field functionality use the following events:

Adding field attributesNew in 5.0+

To add attributes to field components, you need to extend this event and modify the attributes object on it.

use Solspace\Freeform\Fields\FieldInterface;
use Solspace\Freeform\Events\Fields\FieldRenderEvent;
use Solspace\Freeform\Fields\TextField;

Event::on(
FieldInterface::class,
FieldInterface::EVENT_COMPILE_ATTRIBUTES,
function (CompileFieldAttributesEvent $event) {
$field = $event->getField();
$attributes = $event->getAttributes();

// Attach a "data-foo" attribute to all container elements
$attributes->getContainer()->set('data-foo', 'bar');

// Append a green border rule to any existing input styles, if present, or create if not present
$attributes->getInput()->append('style', 'border: 2px solid green;');

// Add a "required" attribute to all text fields
// Make labels for those fields green
// Set a class for the label
if ($field instanceof TextField) {
$attributes->getInput()->set('required', 'required');
$attributes
->getLabel()
->append('style', 'color: green;')
->set('class', 'label-class')
;
}
}
)

Parsing a POST value

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\Fields\FieldInterface;
use Solspace\Freeform\Events\Fields\TransformValueEvent;
use Solspace\Freeform\Fields\TextField;

Event::on(
FieldInterface::class,
FieldInterface::EVENT_TRANSFORM_FROM_POST,
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())
);
}
)

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_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.");
}
}
)