-
-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Closed
Labels
FormhasPRA Pull Request has already been submitted for this issue.A Pull Request has already been submitted for this issue.
Description
Symfony supports using a Form without using a Data Class.
In that page it mentions how to validate individual fields using a constraint, however it's also possible to add constraints to the root form:
- In the controller ->
$form = $this
->createFormBuilder(null, [
'constraints' => [
new Expression([
'expression' => 'value["firstName"] == "john"',
])
]
])
->add('firstName')
->add('lastName')
->getForm();
- In the FormType using
configureOptions
->
public function configureOptions(OptionsResolver $resolver)
{
$collectionConstraint = new Collection(array(
'CardNumber' => array(
new Assert\CardScheme(array(
'schemes' => array('VISA', 'MASTERCARD', 'DISCOVER'),
'message' => 'The credit card number you entered is invalid.'))
),
'SecurityCode' => array(
new Length(array('min' => 3, 'max' => 3))
)
));
$resolver->setDefaults(array(
'data_class' => null,
'constraints' => $collectionConstraint
));
}
(Note: Callback constraints are also allowed and have access to the whole form)
Metadata
Metadata
Assignees
Labels
FormhasPRA Pull Request has already been submitted for this issue.A Pull Request has already been submitted for this issue.