Symfony2 Sonata Admin show attribute only as a readyonly text - php

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

Related

Symfony forms entity type with custom optgroup

I have a Symfony form with select boxes that lists down its options from an entity. But how can I list the database records (options) inside an <optgroup> which I manually specify (label of the optgroup)?
$form = $this->createFormBuilder()
->add('type', EntityType::class, array(
'required' => true,
'class' => 'AppBundle:Types',
'choice_label' => 'name',
'empty_value' => 'Type',
));
The optgroup label is "What is the Type?" and I need to list the above data inside this optgroup.
This can be solved by using the group_by option:
$form = $this->createFormBuilder()
->add('type', EntityType::class, array(
'required' => true,
'class' => 'AppBundle:Types',
'choice_label' => 'name',
'empty_value' => 'Type',
'group_by' => 'group',
));
You should add a field to AppBundle:Types which is named for example 'group'.
If this is a simple translation of a static string, I suggest you manually wrap your fields in an optgroup in your view (you could use a form theme for example), and set the label with your translated string. It would make more sense in my opinion.
If the label is dynamic, then I think this is what you're looking for :
Symfony Doctrine - how to generate optgroup select form

Add custom class to Symfony form label

I am building a Symfony 2.7 form and I would like to be able to customise the labels.
At the moment I am able to define the labels using the form builder outlined in the Symfony docs, however I can't see any way to customise the CSS of the label without having to do it on the template.
I know that I would be able to add the CSS class by individually printing the form_label inside of a with my class, but I would prefer to keep it all inside of the form builder.
I've added my current form below for reference.
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('dob_day', 'choice', array(
'required' => true,
'choices' => $dobDays,
'mapped' => false,
'attr' => array(
'label' => 'Day',
'class' => 'form-control'
)
))
->add('dob_month', 'choice', array(
'required' => true,
'choices' => $dobMonths,
'mapped' => false,
'attr' => array(
'label' => 'Month',
'class' => 'form-control'
)
))
->add('dob_year', 'choice', array(
'required' => true,
'choices' => $dobYears,
'mapped' => false,
'attr' => array(
'label' => 'Year',
'class' => 'form-control'
)
));
}
If you want to add some CSS to your HTML in the form type, use the style attribute.
->add('dob_year', 'choice', array(
'required' => true,
'choices' => $dobYears,
'mapped' => false,
'attr' => array(
'label' => 'Year',
'class' => 'form-control',
'style' => 'max-width: 4rem;'. // Concat. the styles across lines for clarity.
'border: solid 0.1rem teal;'
)
));
That said,
inside the formType is probably the last place I'd go looking to modify these elements' styles if I was the poor dev who had to maintain this form when you were gone.
While it's possible to add inline CSS here, don't put more than a few lines. If it grows to be more, add a class to some external 'main.css' then add that to your primary template (that all others inherit from) and you'll be able to reuse the styles you've made.
Last thought
This is actually pretty helpful when you're trying to make your formType modular and is why I wanted to answer :) Then I got carried away in a best practice mantra that came off as if this technique doesn't have its place, but it does!

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.

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

Magento create new field type for admin module

in a custom module with admin pages, in the file
app\code\local\Namespace\Mymodulw\Block\Myblock\Edit\Tab\Form.php
you can add somthing like this
$fieldset->addField('title', 'text', array(
'label' => Mage::helper('mymodule')->__('Title'),
'class' => 'required-entry',
'required' => true,
'name' => 'title',
));
this create an input of type text in the edit page, what i'm trying to do is create a new type, then i can make something like this
$fieldset->addField('title', 'mytype', array(
'label' => Mage::helper('mymodule')->__('Title'),
'class' => 'required-entry',
'required' => true,
'name' => 'title',
));
can you see the diference??
thanks
The adminhtml field types extend Varien_Data_Form_Element_Abstract and are located in \lib\Varien\Data\Form\Element. So you would need to create a new file called Mytype.php with a declaration of class Varien_Data_Form_Element_Mytype extends Varien_Data_Form_Element_Abstract and then override the Abstract methods to function as you need.
Check out the files in that directory for examples.
Cheers,
JD

Categories