Option chosen from dropdown is not retained - Sonata Admin - symfony

When I submit the form. The value chosen from the dropdown list is successfully saved in the database.
The issue is when form is refresh in the edit mode, the value chosen for the brand is not retained.
Here is my configureFormFields:
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->add('image', 'text')
->add('brand', EntityType::class, array(
'class' => Brand::class,
'choice_label' => 'slug',
))
->add('Page', ChoiceType::class, array(
'choices' => array(
'Homepage' => 'Homepage',
'HomeProduit' => 'Home Produit',
'HomeRubrique' => 'Home Rubrique',
'PageContent' => 'Page Content'
)))
;
}

I solved the issue.
The problem was that there was no relationship between the 2 tables (relationship added and everything works fine). Hope that can help someone some day.

Related

Autocomplete field set created value

I'm using Sonata 3 in my project. I faced with the problem with autocomplete field.
protected function configureFormFields(FormMapper $form)
{
$form
->add(
'contactPerson',
ModelAutocompleteType::class,
[
'label' => 'Contact Person',
'property' => 'name',
'model_manager' => $this->modelManager,
'class' => ContactPerson::class,
'btn_add' => 'Add new',
'callback' => [CustomerCompanyFilters::class, 'filterContactPersons'],
]
)
}
I added 'Add new' button and can create a new entity in popup, but it is not set automatically as a field value, I need to search it again. Is it possible to set field value just after pressing 'Create' button popup?

How to populate a select field based on a previous selected value in Doctrine?

I am working on a Symfony2 CRM which implements Doctrine ORM for the forms. One of the forms on the CRM is for creating customer addresses, and has all the standard text fields such as street, city, postcode etc. but also a dropdown for country and county. The problem is, if I retrieve all counties from the database the list is huge and not only takes ages to load but it's very difficult for my client to find the county since it's for every country in the world.
What I'd like is for them to select the country first and then the county field is auto populated based on the selected country. However, I am unsure how to do this in Doctrine.
Here is my buildForm function in the form type (noting that here, Zone means County):
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('company' , 'text', array('required' => false ));
$builder->add('firstname' , 'text', array('required' => true, 'label' => 'First name' ));
$builder->add('lastname' , 'text', array('required' => true, 'label' => 'Last name' ));
$builder->add('address1' , 'text');
$builder->add('address2' , 'text', array('required' => false ));
$builder->add('city' , 'text');
$builder->add('country' , 'entity',
array(
'class' => 'AppBundle:Oc49Country',
'property' => 'name',
'empty_value' => 'Choose an option',
));
$builder->add('zone' , 'entity',
array(
'class' => 'AppBundle:Oc49Zone',
'query_builder' => function (EntityRepository $er) {
return $er->createQueryBuilder('z')
->orderBy('z.name', 'ASC');
},
'empty_value' => '-- Please select --',
'required' => true,
'label' => 'County/State'
));
$builder->add('postcode' , 'text');
$builder->add('save', 'submit', array(
'attr' => array(
'class' => 'btn btn-primary'
),
));
}
Is there a way of adding an additional parameter to the query builder in order to filter the county list, on the fly?
Since you want to change the County select list after the page is already rendered by symfony, you'll have to use ajax in some way to update just that county select list.
I would use an jquery/ajax call to populate the County select list based on the selected country. From your twig template, add some a jquery function along these lines:
$('#country_select).on('change', function() {
url: '/path/to/controller/to/get/countyList',
method: 'GET'
}).done(function() {
// update options
});
This question will help with details. Obviously my code is rough, but hopefully this helps.

Symfony entity type can not set select value, placeholder was selected

I am calling entity to provide drop down options. I set a place holder value. I tried setting the data value, but regardless placeholder value is alway tag with selected.
My PostFormType:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('title', TextType::class, array( 'attr' => array(
'class' => 'form-control'
)))
->add('price', TextType::class, array( 'attr' => array(
'class' => 'form-control'
)))
->add('artist', EntityType::class, [
'data'=>2,
'placeholder'=>'Choose Artist',
'class'=>Artist::class,
'choice_label'=>'artist',
'query_builder'=> function (EntityRepository $er) {
return $er->createQueryBuilder('artist')
->orderBy('artist.artist', 'ASC');
},
'empty_data' => null,
'attr' => array(
'class' => 'form-control'
)
])
->add('userId', HiddenType::class )
->add('description', TextareaType::class, array( 'attr' => array(
'class' => 'form-control'
)))
->add('purchaseDate','date')
->add('id',HiddenType::class)
;
}
You shouldn't configure data for property artist if it has to be modified by user in the form.
In case you want to set the default value of a new Entity. Even if you do, the change from UI form will not affect as the data attribute will reset the value to 2 as provided after submit.
You can always use Constructor of the relevant Entity to set any default value.
But, as artist is a referenced property. You should do it in Controller before you build the form.
$artist = $em->getRepository("AppBundle:Artist")->find(2);
$entity->setArtist($artist)
// Load the form here. The default value automatically set in the form
Hope this helps!

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

Sonata Exception get too many admin registered

I get the message:
Unable to found a valid admin for the class: Aman\VarshneyBundle\Entity\ArticleTable, get too many admin registered: sonata.admin.appsreview,sonata.admin.review,sonata.admin.article
I am not able to figure out this issue.
you have to specify "admin_code" option in your field definition
in your admin class while building your form
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper->add('user', 'entity', array(), array(
'admin_code' => 'your.user.admin.service'
));
}
It only happens when you have multiple admin classes for the same entity.
I will put code with the use for the 'configureListFields' method, if it's usefull for someone.
protected function configureListFields(ListMapper $listMapper)
{
$listMapper
->add('filename', null, array('admin_code' => 'your.file.admin.service', 'label' => 'File Name'))
->add('parent', 'sonata_type_list', array('admin_code' => 'your.file.admin.service', 'label' => 'Parent File'))
->add('_action', 'actions', array(
'label' => 'Actions',
'actions' => array(
'download' => array(
'template' => 'FileAdminBundle:File:list__action_download.html.twig'
)
)
));
}
As we see, if we have multiple fields, we must put the 'admin_code' in all of them, excepts the actions (if we have it).
Hope it helps.

Resources