Skip to content

Validation

Piotr Kierzniewski edited this page Nov 9, 2017 · 3 revisions

Adding constraints

To validate form field you have to add validators to each field you want to validate. In Symfony framework such a validators are called constraints.

use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;

use Symfony\Component\Validator\Constraints as Assert;

$factory = mf_get_factory();
$form = $factory->create();

$not_blank = new Assert\NotBlank();
$email = new Assert\Email();
$length = new Assert\Length( array( 'min' => 10, 'max' => 140 ) );

$form
    ->add('full_name', TextType::class, array(
        'constraints' => array(
            $not_blank,
        )
    ))
    ->add('email', EmailType::class, array(
        'constraints' => array(
            $not_blank,
            $email,
        )
    ))
    ->add('message', TextareaType::class, array(
        'constraints' => array(
            $not_blank,
            $length,
        )
    ))
    ->add('submit', Submit::class );

This constraints will check if full_name, email and message fields are not blank, if email field is actual e-mail address and if message number of characters is between 10 and 140.

To validate form call isValid on form object. Remember to first submit the form.

// Get request object
$request = mf_get_request();

// Handle request
$form->handleRequest( $request );

// Check if form is submitted and validate data
if( $form->isSubmitted() && $form->isValid() ) { 
    // Get form data
    $data = $form->getData();

    // Perform action with form data e.g. send an e-mail
}

Rendering helpers will automatically handle errors messages and display it after input fields.

Motiforms use the same constraints, which use Symfony framework. Read Symfony constraints documentation to learn more.

Change validation message

If you want to change message of the constraint just add message to constructor options.

$not_blank = new Assert\NotBlank( array( 'message' => __( 'This field is required and can not be empty.' ) ) );

Some constraint can have more then one validation message and use palceholders. Good example is Length constraint.

$length = new Assert\Length( array(
    'min' => 10,
    'max' => 140,
    'maxMessage' => __( 'This value is too long. It should have {{ limit }} character or less.|This value is too long. It should have {{ limit }} characters or less.' ),
    'minMessage' => __( 'This value is too short. It should have {{ limit }} character or more.|This value is too short. It should have {{ limit }} characters or more.' ),
    'exactMessage' => __( 'This value should have exactly {{ limit }} character.|This value should have exactly {{ limit }} characters.' ),
    'charsetMessage' => __( 'This value does not match the expected {{ charset }} charset.' ),
) );
Clone this wiki locally