Constraints

Table of content:

Required

Set field mandatory. You can add this contraint like this.

$form->addConstraint('my_field', 'required', 'My field is required');

Email

If set, the field must be filled with valid email. You can add this contraint like this.

$form->addConstraint('my_field', 'email', 'My field must be an email');

Sameas

If set, the field must be filled with the same value as a other field. For password or email confirmation for example. You can add this contraint like this.

$form->addConstraint('my_field_confirmation', 'sameas', 'Confirmation is not valid', array('sameas' => 'my_field'));

Unique

If set, the field must be filled with an unique value. You can add this contraint like this.

$form->addConstraint('username', 'unique', 'Username must be unique', array('repository' => $this->frontenduserRepository));

Custom

Custom contraint. You can add this contraint like this.

$form->addConstraint('username', 'custom', 'Custom validation not valid', array('method' => function($value, $form) {
        // your code here. return true if result is valid. Else, false
}));

Filesize

For upload element. Check the upload file max size. You can add this contraint like this.

$form->addConstraint('file', 'filesize', 'File too big', array('maxsize' => '2M'));

Fileextension

For upload element. Check the upload file max size. You can add this contraint like this.

$form->addConstraint('file', 'fileextention', 'Your file must be a picture', array('allowed' => 'jpg,gif,png'));