How can I use the bootstrap-daterangepicker in symfony 3.4? - symfony

I try with the next code:
In the form builder
->add('daterange', DateType:class, array(
'widget' => 'single_text',
'attr' => ['class' => 'js-daterangepicker'],
'html5' => false,
))
And the javascript
$('.js-daterangepicker').daterangepicker({
});
This code works, but retrieve data invalid as validator result.
Is DateType the class? How do I recover the selected data in the widget?

Related

Symfony2 - Drop down with the possibility of adding fields

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.

Access translated property of collection in form

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?

Symfony2 collection and related choice field not (yet) managed

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?

symfony form builder update option field

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');

You can avoid this error by setting the "data_class" when use sonata_media_type

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'
)
)

Resources