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().'" />'
))
)
Related
I'm new in Symfony and Sonata/AdminBundle. I would like to know how to mark selected an option when the entity has a field from other entity. For example: I have two entities: Shop and City. The Shop entity has a field called id_city.
My problem is when I'm rendering the edit form Shop because always the first id_city in the option is selected.
This is the piece of code where I'm rendering the configuration form in AdminStores class:
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->tab('Tiendas')
->with('Content', array('class' => 'col-md-9'))
->add('nombreTienda', 'text')
->add('cifTienda', 'text')
->add('direccionTienda', 'text')
->add('personaContacto', 'text', array('required' => false,'empty_data' => ''))
->add('cp', 'text', array('label' => 'Código Postal', 'required' => false, 'empty_data' => '00000'))
->add('urlTienda', 'text', array('required' => false, 'empty_data' => ''))
->add('emailTienda', 'text')
->add('telefonoTienda', 'text')
->add('login', 'text')
->add('pass', 'password', array('required' => false))
->add('idMunicipio', 'entity', array(
'class' => 'AppBundle:Municipios',
'choice_label' => 'municipio',
'query_builder' => function (EntityRepository $er) {
$lista = $er->createQueryBuilder('ss')
->orderBy('ss.municipio', 'ASC');
},
'data' => $this->subject->getIdMunicipio()
)) // end array idMunicipio y add()
->add('idProvincia', EntityType::class, array(
'class' => 'AppBundle:Provincias',
'label' => 'Provincia',
'choice_label' => 'provincia',
'choice_value' => 'getId',
'by_reference' => true,
))
->add('descripcionTienda', 'textarea')
->end()
->end()
->tab('Multimedia')
->with('Content', array('class' => 'col-md-3'))
->add('fotoTienda', 'file', array(
'label' => 'Imagenes (puedes subir hasta 6 imágenes)',
'attr' =>array('class' => 'form-control', 'multiple' => 'multiple', 'accept' => 'image/*'),
'data_class' => null,
'required' => false,
'empty_data' => 'noDisponible',
));
}
In this piece of code, I'm recovering all cities in AdminStores class:
->add('idMunicipio', 'entity', array(
'class' => 'AppBundle:Municipios',
'choice_label' => 'municipio',
'query_builder' => function (EntityRepository $er) {
$lista = $er->createQueryBuilder('ss')
->orderBy('ss.municipio', 'ASC');
},
'data' => $this->subject->getIdMunicipio()
)) // end array idMunicipio y add()
I would like to know, please, the logic for " if this->id_city == entity->id_city then, option is selected".
Thanks in advance
I edit this comment because I think that I solved it.
In my AdminController called ShopsAdmin I have created a method called getAllMunicipios which return an array with their name and id:
$allCities = array(
'Tokyo' => 1
'Madrid => 2
);
This is the method:
protected function getAllMunicipios()
{
$municipios = $this->getConfigurationPool()
->getContainer()
->get('doctrine')
->getRepository('AppBundle:Municipios')
->findBy([], ['municipio' => 'ASC']);
$todosmunicipios = array();
foreach ($municipios as $municipio) {
$todosmunicipios[(string)$municipio->getMunicipio()] = (int)$municipio->getId();
}
return $todosmunicipios;
}
Now my AdminStores::configureFormFields method like that this:
->add('idMunicipio', 'choice', array(
'choices' => $this->getAllMunicipios(),
'required' => false,
'by_reference' => false,
'data' => $this->subject->getIdMunicipio()
))
It is a good way to do it? I think that the method that return all, must be placed into the entity and not int the controller but I dont know how do it static
just call setCity(\AppBundle\Entity\City $city) in your Shop entity. and give the right city entity as the first and only parameter. Do this before you render the form
I use Sonata Admin Bundle in my Symfony2 project. In one form, I have a list of choice containing two items, 'article' and 'event', and a date field, which is relevant only if 'event' is selected in the list.
How can I disabla/enable according to which value in the list is selected?
Here is my relevant code :
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->add('title', null, array(
'label' => 'Titre',
))
->add('type', 'choice', array('choices' => array('0' => 'Article', '1' => 'Evénement',)) )
->add('gameDate', null, array('required' => false));
}
Thank you for your help.
Maybe: https://sonata-project.org/bundles/admin/master/doc/reference/form_types.html#sonata-adminbundle-form-type-choicefieldmasktype if you just show additional field based on choice
just to save you some scrolling, here's the relevant section from the doc:
13.1.5. SONATA\ADMINBUNDLE\FORM\TYPE\CHOICEFIELDMASKTYPE
According the choice made only associated fields are displayed. The others fields are hidden.
<?php
// src/AppBundle/Admin/AppMenuAdmin.php
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Form\Type\ChoiceFieldMaskType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
class AppMenuAdmin extends AbstractAdmin
{
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->add('linkType', ChoiceFieldMaskType::class, [
'choices' => [
'uri' => 'uri',
'route' => 'route',
],
'map' => [
'route' => ['route', 'parameters'],
'uri' => ['uri'],
],
'placeholder' => 'Choose an option',
'required' => false
])
->add('route', TextType::class)
->add('uri', TextType::class)
->add('parameters')
;
}
}
I'm struggling with the "sonata_type_translatable_choice" field type.
PageAdmin.php :
protected function configureFormFields(FormMapper $formMapper) {
$formMapper
->with('page.title', ['class' => 'col-md-12'])
->add('name', null, ['label' => 'page.field.name'])
->add('code', null, ['label' => 'page.field.code'])
->add('content', 'ckeditor', ['label' => 'page.field.content'])
->add('status', 'sonata_type_translatable_choice', [
'choices' => Page::$statuses,
'catalogue' => 'admin',
'label' => 'page.field.status'
])
->end()
;
}
Page.php :
public static $statuses = array(
self::STATUS_HIDDEN => 'page.status.hidden',
self::STATUS_OFFLINE => 'page.status.offline',
self::STATUS_DRAFT => 'page.status.draft',
self::STATUS_ONLINE => 'page.status.online'
);
admin.en.yml :
page:
status:
hidden: hidden
offline: offline
draft: draft
online: online
The form is displaying the select field with the values : "page.status.hidden" instead of "hidden" located in my .yml file.
Can someone help me with this ?
I have two entities and two Admin classes(StripePage and Stripe) for them. Form looks good, it has my fields and button to add new subforms. When "add new" button is clicked, new subform with form from Admin class is rendered, but if you click second time on this button or try to save entity error appears:
Catchable Fatal Error: Argument 1 passed to
Fred\CoreBundle\Entity\StripePage::removeStripe() must be an instance
of Fred\CoreBundle\Entity\Stripe, null given in
C:\Users\lubimov\OpenServer\domains\tappic.dev\src\Fred\CoreBundle\Entity\StripePage.php
So symfony tries to remove entity instead of adding it.
StripePageAdmin class:
class StripePageAdmin extends Admin
{
protected function configureListFields(ListMapper $listMapper)
{
$listMapper
->addIdentifier('id', null, array('label' => 'id'))
->add('name', null, array('label' => 'Page name'));
}
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->add('name', 'text', array('label' => 'Page name'))
->add('stripes', 'sonata_type_collection',
array('label' => 'Stripes', 'required' => false, 'by_reference' => false),
array('edit' => 'inline', 'inline' => 'table', 'sortable' => 'pos')
)
->end();
}
}
StripeAdmin class:
class StripeAdmin extends Admin
{
protected function configureListFields(ListMapper $listMapper)
{
$listMapper
->addIdentifier('id')
->add('picture', null, array('sortable' => true))
->add('text', null, array('sortable' => true))
->add('deep_link', null, array('sortable' => true));
}
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->add('file', 'file', array('label' => 'Picture'))
->add('text', null, array('label' => 'Text'))
->add('deep_link', 'choice', array(
'choices' => array('test1' => 'Test1', 'test' => 'Test2'),
'label' => 'Deep Link',
))
->end();
}
}
What is the problem? Stripe form in admin class must be configured by other way?
Well, solution that I use now is to set 'by_reference' => true in 'sonata_type_collection' settings.
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->add('name', 'text', array('label' => 'Page name'))
->add('stripes', 'sonata_type_collection',
array('label' => 'Stripes', 'required' => false, 'by_reference' => true),
array('edit' => 'inline', 'inline' => 'table', 'sortable' => 'pos')
)
->end();
}
After this need to declare setStripes() method in entity.
public function setStripes(\Doctrine\Common\Collections\ArrayCollection $stripes) {
$this->stripes = $stripes;
}
If somebody finds how to solve this using addStripe() type of methods, please share it here.
I'm having the following error in my Symfony2 project:
Automatic initialization is only supported on root forms. You should set the "auto_initialize" option to false on the field "descriptionEN".
I'm using the Sonata Admin Bundle. I would like to populate a text field in my form before rendering the form. So I am using the form event PRE_SET_DATA . In Sonata you only have prePersist & preUpdate so I am doing it like this:
// Fields to be shown on create/edit forms
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->add('tag', 'text', array('label' => 'Tag'))
->add('description', 'text', array('label' => 'Beschrijving'))
->add('content', 'textarea', array('label' => 'Tekst', 'attr' => array('class' => 'ckeditor'), 'help' =>
'Schrijf 2 paragrafen onder elkaar, deze worden naast elkaar geplaatst op de website.'))
->add('files', 'file', array('required' => false, 'multiple' => true, 'help' =>
'<b>Home:</b> 1277×670.png<br><b>Gallerij:</b> 1284×110.jpg<br><b>Diensten:</b> 1282×375.jpg<br><b>Footer:</b> 1281×375.jpg'))
;
$builder = $formMapper->getFormBuilder();
$factory = $builder->getFormFactory();
$func = function (FormEvent $e) use ($factory) {
$form = $e->getForm();
$page = $e->getData();
$pageLocale = $this->getSubject();
$pageID = $pageLocale->getPageId();
if($pageID === null)
{
return;
}
$form->add($factory->createNamed('descriptionEN', 'text', array(
'auto_initialize' => false,
'label' => 'Beschrijving Engels',
'query_builder' => function (EntityRepository $repository) use ($pageID) {
return $repository->getDescriptionEN($pageID);
}
)));
};
$builder->addEventListener(FormEvents::PRE_SET_DATA, $func);
}
The strange thing is I'm getting the error that auto_initialize should be false when I've specifically set it to false ... . I'm definitely sure that's the field causing problems because when I comment the field I get no errors.
What could be another reason causing this?
The array of options is the fourth parameter of the function createNamed
$form->add($factory->createNamed('descriptionEN', 'text', null, array(
'auto_initialize' => false,
'label' => 'Beschrijving Engels',
'query_builder' => function (EntityRepository $repository) use ($pageID) {
return $repository->getDescriptionEN($pageID);
}