Change field depending of other in SonataAdminBundle - php

In a Symfony3 project, i have a SonataAdminBundle form like this :
protected function configureFormFields(FormMapper $formMapper) {
$formMapper->add('rentalTime', 'choice', array(
'label' => 'Durée de location',
'multiple' => false,
'choices' => array(
'3H' => '3',
'24H' => '24',
)
));
$formMapper->add('rentalTimeType', 'choice', array(
'label' => 'Type de durée de location',
'multiple' => false,
'choices' => array(
'Heure(s)' => 'H',
'Plage de jours' => 'R'
)
));
.
.
.
When i change the value of rentalTime i want to change the value of rentaltimeType.
Example: If i select 24H in rentalTime field and automatically the rentalTimeType change for 'Plage de jours'.
I've read a lot of topic with the use of $subject but it's not exactly what i need.
change row color in sonata admin bundle dependent on a value of a field
Correct way to use FormEvents to customise fields in SonataAdmin
Sonata User Admin - Custom field dependency
So is it possible to do what i need ?

You can customize the form specific to the data that was submitted by the user.
Dynamic Generation for Submitted Forms

Related

Symfony 3 - Add style to FormType (Entity::class => choiceLabels)

I have a formType that contains an EntityType :: class.
This is a list of possible types of users to choose from in a form.
->add('typeUser',EntityType::class, array(
'required' => true,
'class'=>'Site\PagesBundle\Entity\TypeUser',
'choice_label'=>'typeUtilisateur',
'expanded'=>true,
'multiple'=>true,
'empty_data' => 'Veuillez sélectionner au moins 1 type',
));
I wish I could give a "style" to these choices. For example, I would like them to be some distance from the checkbox.
So, I have to add style (margin-left by example) to :
'choice_label'=>'typeUtilisateur',
Is it possible in FormType ?
Or I should make that on my twig file ? In this case, how can I do it ?
Thanks for your help !
EDIT:
->add('typeUser',EntityType::class, array(
'required' => true,
'class'=>'Site\PagesBundle\Entity\TypeUser',
'choice_label'=>('typeUtilisateur' => 'attr' => array( 'style' => 'margin-left:15px')) ,
'expanded'=>true,
'multiple'=>true,
'empty_data' => 'Veuillez sélectionner au moins 1 type',
));
There are a few ways you can style your form fields. See a few of them below:
Option 1: Add a class attribute to the field in the form builder
$builder
->add('example', EntityType::class, [
'class' => FooBar::class,
'attr' => [
'class' => 'my-example-class'
]
])
Option 2: Specify a class in the form template
{{ form_widget(form.fooBar, {'attr': {'class': 'form-control'}}) }}
You can be specific to the choices, see this link here which shows adding a class attribute to choices.
I think that the right way is to use the attr key with which you could set a custom class="" for your field
->add('typeUser',EntityType::class, array(
'attr' => [
'class' => 'YOUR_CHOICE_CLASS',
],
'required' => true,
'class'=>'Site\PagesBundle\Entity\TypeUser',
'expanded'=>true,
'multiple'=>true,
'empty_data' => 'Veuillez sélectionner au moins 1 type',
));
Then, can define the styles that you want in your CSS file

Access properties and methods of a class while configuring EntityType in Formbuilder::add()

I`ve got a dropdown list in my Symfony2 form like this:
$builder->add('categories','entity', array(
'class' => 'MyBundle:Myentity',
'property' => 'name',
'label' => 'Mylabel',
'expanded' => false,
'multiple' => false,
'label_attr' => array ( 'class' => 'control-label' ),
'attr' => array ( 'class' => 'form-control',
'placeholder' => 'Placeholder',
'title' => "Mytitle",
'data-toggle' => 'tooltip',
'data-myidfromDB' => '????',
'data-mynamefromDB'=>'????' etc. )));
So I am getting a list of MyBundle:Myentity objects and when I choose one I want to show all its properties (like ID, name, etc.) which are stored in my DB and described in Entity class, in different html data-* fields. If I select another one from the list I want to see all information related to my newly selected option in HTML (to change dynamically). Any ideas how to do that?
Since Symfony 2.7 you can set the option choice_attr to ChoiceType and set it a callable receiving the choice as an argument.
EntityType inherits this option and the choice in that case is the instantiated entity, so you can write something like :
$builder->add('categories','entity', array(
'class' => 'MyBundle:MyEntity',
'property' => 'name',
'label' => 'Mylabel',
'attr' => array('class' => 'form-control'),
'label_attr' => array('class' => 'control-label'),
'choice_attr' => function (\AppBundle\Entity\MyEntity $myEntity) {
return array(
'data-private-property' => $entity->getPrivateProperty(),
'data-some-value' => $entity->someMethod(),
);
},
);
You can't do that in easy way.
But you can put more information in select label.
Look on
http://symfony.com/doc/current/reference/forms/types/entity.html#choice-label
Yout can put here more field details and get it from your javascript.

SonataAdmin - Custom form template for each form

I'm having problems with Sonata Admin Bundle. What I would like to do is:
Add some text before some labels in my form. Like for example:
The resolution of your image must be ..x.. .
For example I have a form like this:
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->add('locale', 'choice', array(
'choices' => array('nl' => 'NL', 'en' => 'EN'),
'required' => true,
))
->add('pageid.tag', 'text', array('label' => 'Tag'))
->add('description', 'text', array('label' => 'Beschrijving'))
->add('content', 'textarea', array('label' => 'Tekst', 'attr' => array('class' => 'ckeditor')))
->add('files', 'file', array('required' => false, 'multiple' => true))
;
}
Now I would like to add some text before my files input field.
What I've done now is:
Add this to my config.yml (overload the templates/form configuration option):
sonata_doctrine_orm_admin:
# default value is null, so doctrine uses the value defined in the configuration
entity_manager: ~
templates:
form:
- MurisBundle:PageAdmin:form_admin_fields.html.twig
But this will be used for every form, can't I set specific form templates for specific forms?
You can specify form template in your admin class overriding getFormTheme method.
Add this code to your admin class.
public function getFormTheme()
{
return array_merge(
parent::getFormTheme(),
array('MurisBundle:PageAdmin:form_admin_fields.html.twig')
);
}
getPictureUrlFull().'" alt="'.$campaign->getPicture().'" style="margin-top:10px;" />Use "help"
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->add('locale', 'choice', array(
'choices' => array('nl' => 'NL', 'en' => 'EN'),
'required' => true,
'help' => '<img src="'.$entity->getPictureUrlFull().'" alt="'.$entity->getPicture().'" />'
))
)

Symfony 2 form events

I am setting up a test site with symfony and I built a datagrid with pagination that works correctly.
One standard feature I would like to add to pagination is the number of displayed items to be set for a dropdown menu:
I have a choice field (for the moment in a controller, but later I will put it in a form class.)
Form with the choice field in built using this code:
$form = $this->createFormBuilder()
// ->setAction($this->generateUrl('userlist2'))
//->setMethod('GET')
->add('numRecords', 'choice', array(
'choices' => array('5' => '5', '10' => '10', '20' => '20'),
'required' => false,
'empty_value' => false,
'attr' => array(
'class' => 'form-control',
'style' => 'width:80px',
)
)
)
->getForm();
I have googled to check if symfony has a way to get changes in choice field but could not find it.
What I need is to intercept the change event of the choice field and get its value, so I can put the item number value in a variable and use it for pagination.
Any help appreciated
Thanks
Paolo

Symfony2 Sonata Admin show attribute only as a readyonly text

I have some immutable attributes on my entity to administrate with sonata-admin bundle.
I want to show them in the edit-view of the entity, but don't want to provide any mechanism to change it (e.g. the value shall not be inside a input field)
I couldn't find anything but this:
$formMapper
->add('post', 'entity',
array(
'label' => 'Some post',
'attr' => array(
'readonly' => true,
'disabled' => true
),
'class' => 'Acme\DemoBundle\Entity\Post'
)
)
;
I tried it out with read_only, readonly, disabled etc. all the stuff. It looks ok, it's now inside a dropdown (since it is an entity) and I can not modify it.
But I even don't want that. I really need it as text (the current one).
Especially this is annoying if you use DoctrineExtensions with softdeletable, timestampable, since every "save" saves also the form-data.
Changing the type to 'text' instead of 'entity' replaces the dropdown with a input-field.. So, what's the best approach here?
$formMapper
->add('post', 'entity',
array(
'label' => 'Some post',
'read_only' => true,
'disabled' => true,
'class' => 'Acme\DemoBundle\Entity\Post'
)
)
;
This answer tells how to customize list rendering. Maybe the same approach works with form rendering?
If not, then you can create your custom form type according to create custom field type documentation, and customizing the template.
This is a bit old but this might help someone.
Here is the code that resolves your issue.
$formMapper
->add('post', 'entity', array('label' => 'Some post','attr' => array(
'readonly' => 'readonly',
'disabled' => 'disabled',
),
'class' => 'Acme\DemoBundle\Entity\Post')
)

Categories