A newer version of

Freeform

is available.

Try Freeform 5 now →

Developer Events

Element Connection Events

If you have to perform any special checks or extra steps when validating or connecting Freeform submissions with Craft Elements, be sure to check out the events listed below:

Before a connection is validated

This event lets you add your own validations to the element connection validation process.

use Solspace\Freeform\Library\Connections\AbstractConnection;
use Solspace\Freeform\Events\Connections\ValidateEvent;

Event::on(
  AbstractConnection::class,
  AbstractConnection::EVENT_BEFORE_VALIDATE,
  function (ValidateEvent $event) {
    $form = $event->getForm();
    $connection = $event->getConnection();
    $element = $event->getSubmission();

    // Attach an error for the title field
    // This example will always invalidate every connection
    $element->addError('title', 'There are no good titles');

    // This error will only show up in a freeform form
    // if the attribute has been mapped to a form field.
  }
)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

After a connection is validated

use Solspace\Freeform\Library\Connections\AbstractConnection;
use Solspace\Freeform\Events\Connections\ValidateEvent;

Event::on(
  AbstractConnection::class,
  AbstractConnection::EVENT_AFTER_VALIDATE,
  function (ValidateEvent $event) {
    $form = $event->getForm();
    $connection = $event->getConnection();
    $element = $event->getSubmission();

    if (empty($element->getErrors())) {
      // Do something if the element passes validation
    }
  }
)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

Before an element is connected

This event is fired before the created and validated Element is saved.

use Solspace\Freeform\Library\Connections\AbstractConnection;
use Solspace\Freeform\Events\Connections\ConnectEvent;

Event::on(
  AbstractConnection::class,
  AbstractConnection::EVENT_BEFORE_CONNECT,
  function (ConnectEvent $event) {
    $form = $event->getForm();
    $connection = $event->getConnection();
    $element = $event->getSubmission();

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

After an element is connected

This event is fired before after the validated Element has been saved successfully.

use Solspace\Freeform\Library\Connections\AbstractConnection;
use Solspace\Freeform\Events\Connections\ConnectEvent;

Event::on(
  AbstractConnection::class,
  AbstractConnection::EVENT_AFTER_CONNECT,
  function (ConnectEvent $event) {
    $form = $event->getForm();
    $connection = $event->getConnection();
    $element = $event->getSubmission();

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