I use Symfony 4.1, and I try a simple usage of form builder :
$builder
->add('name', TextType::class)
->add('phone', TelType::class)
->add('password', RepeatedType::class, array(
'type' => PasswordType::class,
'invalid_message' => 'Passwords don\'t match',
'options' => array('attr' => array('class' => 'password-field')),
'required' => false,
'first_options' => array('label' => 'Password'),
'second_options' => array('label' => 'Password confirmation'),
'mapped' => false,
))
I got the error :
The option "invalid_message" does not exist. Defined options are ....
I don't find any informations on it, I just tried to copy the example from Symfony Doc : https://symfony.com/doc/current/reference/forms/types/repeated.html
Do I need to import something ?
The invalid_message option belong to FormTypeValidatorExtension which is loaded by ValidatorExtension when the Validator component is available.
You need to install this component to enable the validator options:
composer require symfony/validator
Related
in advance, sorry for my poor english.
I have a problem with the "sonata_type_collection" form type.
I got two entity, the first, "proposition" has a one-to-many relationship with "Image". "Image" has a many-to-one relationship with "proposition". Everything seems to work well, the ImageAdmin is nested in PropositionAdmin.
But when i add a row in the PropositionAdmin, without persisting object, it clear the input="file" field. I read that its the correct behavior since Sonata reload the form when adding a row. So i was wondering if there was a way to avoid that behavior.
Thanks in advance.
Here is my code :
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->add('title')
->add('axe')
->add('username')
->add('password','repeated', array('type' => 'text','options' => array('translation_domain' => 'FOSUserBundle'),
'first_options' => array('label' => 'form.password'),
'second_options' => array('label' => 'form.password_confirmation'),
'invalid_message' => 'fos_user.password.mismatch'))
->add('imgs', 'sonata_type_collection', array(
'by_reference' => false
), array(
'edit' => 'inline',
'allow_delete' => true
))
;
}
I endend up by using a Symfony2 form.
I generated the formtype with Command here
Then, in the parent entity's bundle:
>add('imgs', 'collection', array(
'label' => 'Créations',
'by_reference' => false ,
'type' => new \propalBundle\Form\ImageType(),
'allow_delete' => true,
'allow_add' => true
), array(
'edit' => 'standard',
'inline' => 'table',
))
It can require some tweaks but seems to work.
When the proposition has relation oneToMany with image you should add multiple attribute in the configuration of imgs filed in PropositionAdmin class
->add('imgs', 'sonata_type_collection', array(
'by_reference' => false
'multiple' => true
), array(
'edit' => 'inline',
'allow_delete' => true
));
I have a form with a collection. My form "PageType" with a collection look like this.
$builder->add('sections', 'collection', array(
'type' => new SectionType($this->securityContext, $this->entityManager) ,
'allow_add' => true,
'allow_delete' => true,
'label' => false,
'prototype' => true,
'prototype_name' => '__sectionPrototype__',
'options' => array(
'label' => false,
)
And my second form which represents the collection "SectionType" look like this.
->add('translations', 'a2lix_translations', array(
'fields' => array(
'title' => array(
'field_type' => 'text',
'label' => 'title',
'attr' => array('class' => 'rte sectionTitle')
),
'text' => array(
'field_type' => 'textarea',
'label' => 'hintText',
'attr' => array('class' => 'rte sectionDescription')
)
)
))
How can i access from twig the translations fields?
I think that is not possible. Maybe somebody could correct me if I'm wrong.
fields is an option item for a2lix_translations. If you would have Form object you might be able to do that, but in fact, you have FormView which represents built form.
Alternatively, you could pass that array via standard return from controller and into your twig. Does that sound good?
I've got a form with a collection field "options" and a choice field "defaultOption". My goal is to enable the user to create a new entity, add some options and select a default option from the list of option he/she just added before saving the entire thing.
Here's part of the form that shows both fields:
$form->add('options', 'collection', array(
'type' => new CustomFieldOptionType(),
'label' => 'custom.field.options',
'allow_add' => true,
'allow_delete' => true,
'options' => array(
'label_render' => false,
'widget_control_group' => false,
),
'by_reference' => false,
'attr' => array('class' => 'options')
));
$form->add('defaultOption', 'entity', array(
'label' => 'custom.field.default',
'class' => 'XFDOBundle:CustomFieldOption',
'choices' => $field->getOptions(),
'property' => 'name',
'required' => false,
'empty_value' => 'custom.field.default.empty',
'attr' => array('class' => 'default-option')
));
I've tried several things with jquery and form event listeners to (re)populate the "defaultOption" field with the new (not-yet-persisted) options, but that resulted in to nothing or "Entities passed to the choice field must be managed" exceptions.
Any idea how I can get this working?
I installed Sonata Admin and after install Sonata Media
i have class admin for "colors" and set in configureFormFields
->add('image', 'sonata_media_type', array('required' => false,
'context' => 'default',
'label' => 'Image',
'provider'=>'sonata.media.provider.image'
))
While stored in the database but when edit show this error
The form's view data is expected to be an instance of class Application\Sonata\MediaBundle\Entity\Media, but is a(n) string. You can avoid this error by setting the "data_class" option to null or by adding a view transformer that transforms a(n) string to an instance of Application\Sonata\MediaBundle\Entity\Media.
Help me, please.
You have to set the correct data class:
->add('media', 'sonata_media_type', array(
'provider' => 'sonata.media.provider.image',
'context' => 'default',
'data_class' => 'Application\Sonata\MediaBundle\Entity\Media',
'required' => false,
'label' => 'Image'
))
Notice the: 'data_class' => 'Application\Sonata\MediaBundle\Entity\Media',
Try setting the data_class option to the correct entity class as the message suggests ...
->add('image', 'sonata_media_type',
array(
'required' => false,
'context' => 'default',
'data_class' => 'Application\Sonata\MediaBundle\Entity\Media',
'label' => 'Image',
'provider' =>'sonata.media.provider.image'
)
)
is it possible to use sonata_type_collection against a custom form type instead of a property/relationship with another entity?
Something like this:
$formMapper->add('foo', 'sonata_type_collection',
array('type' => new \myVendor\myBundleBundle\Form\Type\BlockType()
));
Which throws me the following error
The current field `contingut` is not linked to an admin. Please create one for the target entity : ``
EDIT:
Something like this did the trick:
$formMapper->add('contingut', 'collection', array(
'type' => new \myVendor\myBundleBundle\Form\Type\Block1Type(),
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false
));
Instead of a custom type in your example, you can also use a native entity type.
$formMapper->add('cars',
'collection',
array(
'type' => 'entity',
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false,
'label' => false,
'options' => array(
'class' => 'YourBundle:Car',
'property' => 'name',
'label' => false
)
)
)