Sorry for my english.
I need to create a drop down list (select option) with values taken from an Entity.
I must also add options (I use select2 to do this).
When I send the form it doesn't work. How can I configure the field type to make it work?
I need the possibilities to adding multiple persons
My current code is
...
->add('person', 'entity', array(
'attr' => array(
'class' => 'tags'
),
'class' => 'AppBundle:Person',
'data_class' => null,
'label' => 'Persons',
'mapped' => false,
'multiple' => true,
'required' => false
));
Select2
$('select.tags').select2({
language: 'it',
tags: true,
tokenSeparators: [','],
width: '100%'
});
Error message
The offset "0" does not exist.
Related
I dynamically generate a form and add constraints (i.e. Choice).
$builder->add('test', 'choice', [
'choices' => [1, 'one', 2 => 'two'],
'required' => true,
'expanded' => true,
'error_bubbling' => true,
'cascade_validation' => true,
'label' => 'this_is_a_test',
'multiple' => false,
'constraints' => [
new NotBlank([
'groups' => ['Default']
]),
new Choice([
'min' => 1,
'choices' => [1, 2],
'groups' => ['Default']
])
]
]);
When submitting the form with empty data, the error shows up for the form, not the element of the form where i added the constraint (checked in profiler as well).
There is not Option atPath for those constraints and i add them directly to the field, so i do not get why they show up for the form.
That's what the error_bubbling option does (which you set to true in your form type):
If true, any errors for this field will be passed to the parent field or form. For example, if set to true on a normal field, any errors for that field will be attached to the main form, not to the specific field.
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?
is it possible to update an option field after adding it ?
$builder
->add('examens', 'entity', array(
'class' => 'TelegrammeExamenBundle:ExamExamen',
'property' => 'libelle',
'required' => true,
'empty_value' => 'Sélectionnez un examen',
//'data' => $this->em->getReference("TelegrammeExamenBundle:ExamExamen", 510),
'data' => null,
'query_builder' => function(ExamenRepository $r) {
return $r->getSelectList();
},
'attr' => array('class' => 'bg_white_filet_gris')
))
;
how modify field option ??? (setOption don't exist)
if (...) $builder->get('examens')->setOption('property', 'test');
You can simply ->add() it again. As the API documentation suggests for the add method:
Adds or replaces a child to the form
http://api.symfony.com/2.8/Symfony/Component/Form/FormInterface.html#method_add
This can be used to modify form elements for example in a FormEvent.
Alternatively the FormBuilder provides a setAttribute() method which can be used as follows:
$builder->get('examens')->setAttribute('property', 'test');
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
)
)
)