简体   繁体   中英

Validate symfony form without post

I an using the symfony form to create a form and validate it. But i want to pre-validate the form on page loading itself ie. validate the form without post. Is it possible to do it in symfony?.

I have tried to use $form1->isValid(); on the else part of post. But its not working. Also I tried to use the submit() ,

$data = $form1->getData();
$form1->submit($data);
$form1->isValid();

but with no success

*the form fields are dynamic and the validations are also dynamic. So a form that is preloaded can have error fields.

What you can do is for sure manual Entity Validation. I can imagine cases when you can't trust data already saved in database or, you want to pre-fill object with data from other source (external request response) before giving it for user to edit.

Please read docs about validation here: http://symfony.com/doc/current/book/validation.html

Possible code:

$author = new Author();

// ... do something to the $author object

$validator = $this->get('validator');
$errors = $validator->validate($author);

In this case you don't use constraints from FormType but validator constraints (they can be declared in entity - they will be used also by FormType): http://symfony.com/doc/current/book/validation.html#constraints

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM