I am using Symfony 2.6. I am trying to create a form without Entity, but get the following error:
The option "constraints" does not exist. Known options are: "action", "attr", "auto_initialize", "block_name", "by_reference", "compound", "csrf_field_name", "csrf_message", "csrf_protection", "csrf_provider", "csrf_token_id", "csrf_token_manager", "data", "data_class", "disabled", "empty_data", "error_bubbling", "inherit_data", "intention", "label", "label_attr", "label_format", "mapped", "max_length", "method", "pattern", "post_max_size_message", "property_path", "read_only", "required", "translation_domain", "trim", "virtual".
class MessageType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('sender', 'text', [
'constraints' => [
new Constraints\NotBlank(),
],
])
->add('recipient', 'email')
->add('message', 'textarea');
}
public function getName()
{
return 'message';
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$collectionConstraint = new Constraints\Collection(array(
'fields' => [
'sender' => [
new Constraints\NotBlank(),
new Constraints\Email(),
],
'recipient' => [
new Constraints\NotBlank(),
new Constraints\Email(),
],
'message' => [
new Constraints\NotBlank(),
],
],
));
$resolver->setDefaults([
'validation_constraints' => $collectionConstraint,
]);
}
}
Using only setDefaultOptions shows no error, but it does not work, does not validate the fields.
As can be seen, tried anyway. I also tried as is the documentation for using the component form outbox, but get the same error.
http://symfony.com/doc/current/components/form/introduction.html#form-validation
EDIT
I also tried this way and get the same error.
$form = $formFactory->createBuilder()
->add('task', 'text', array(
'constraints' => new NotBlank(),
))
->add('dueDate', 'date', array(
'constraints' => array(
new NotBlank(),
new Type('\DateTime'),
)
))
->getForm();
The 'constraints' option is part of the Validator extension form. How I solved the problem:
$ValidatorExtension = new ValidatorExtension($validatorBuilder->getValidator());
$formRegistry = new FormRegistry([$csrfProvider, new CoreExtension(), $ValidatorExtension], Yii::$symfony->container->get('form.resolved_type_factory'));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With