What is Form Context in Symfony2 - symfony

I am getting started with Symfony2 and I am trying to understand the Form Component. I am looking at this page http://docs.symfony-reloaded.org/guides/forms/overview.html
And I can understand how we create form classes but what is confusing is how we actually use those forms in our controller.
$form = ContactForm::create($this->get('form.context'));
Does anyone have a more in depth explanation of the form.context portion of this code, and the actual process behind using forms within controllers?
Thanks.

form.context service is a Symfony\Component\Form\FormContext object by default. Here's a full definition of this service:
<service id="form.context" class="%form.context.class%">
<argument type="collection">
<argument key="validator" type="service" id="validator" />
<argument key="validation_groups">%form.validation_groups%</argument>
<argument key="field_factory" type="service" id="form.field_factory" />
<argument key="csrf_protection">%form.csrf_protection.enabled%</argument>
<argument key="csrf_field_name">%form.csrf_protection.field_name%</argument>
<argument key="csrf_provider" type="service" id="form.csrf_provider" />
</argument>
</service>
Actually it's a very simple object that just prepare some basic options used by almost every form, ie. a validator, CSRF protection and field factory.
In fact the code you've posted is equivalent of:
$form = new \Symfony\Components\Form\Form(null, array(
'validator' => $this->get('validator'),
'validation_groups' => ...
...
));

Related

Symfony 2: Is there a difference between repository defined as a service vs repository injected with expression?

I can define repository as a service:
<service id="books_repository" class="MyApp\BooksBundle\Repository\BooksRepository">
<factory service="doctrine.orm.default_entity_manager" method="getRepository"/>
<argument>MyApp\BooksBundle\Entity\BookEntity</argument>
</service>
and then inject:
<service id="books_service" class="MyApp\BooksBundle\Service\BooksService">
<argument type="service" id="books_repository"/>
</service>
But I can also use expression argument:
<service id="books_service" class="MyApp\BooksBundle\Service\BooksService">
<argument type="expression">service('doctrine').getManager('default').getRepository('MyApp\BooksBundle\Repository\BooksRepository')</argument>
</service>
Are there any reasons I should prefer one solution over the other one? Are they functionally equivalent?

To add default filter to grid in Magento2?

I have created one custom module with title & status field attribute in admin grid.
Grid collections in module_test_grid_block.xml
<arguments>
<argument name="id" xsi:type="string">testGrid</argument>
<argument name="dataSource" xsi:type="object">Namespace\Module\Model\ResourceModel\test\Collection</argument>
<argument name="default_sort" xsi:type="string">id</argument>
<argument name="default_dir" xsi:type="string">desc</argument>
<argument name="grid_url" xsi:type="url" path="*/*/grid"><param name="_current">1</param></argument>
</arguments>
I just want to show the datas which are enabled.Have any option to add default filter in admin grid collections?

SonataUserBundle overriding EditableRolesBuilder

Im trying to override EditableRolesBuilder in Security folder.
I try to make same hierarchy in
Application\Sonata\UserBundle\Security\EditableRolesBuilder which
extends the base one Sonata\UserBundle\Security\EditableRolesBuilder
from vendor but with no success.
I try to override service in
Sonata\UserBundle\Resources\config\admin.xml, to pass my custom class
but i can't find anywhere how to override whole service.This service
is without parameters but hardcoded class and i cant't pass it in
config.yml
In Sonata admin if you wish to override security roles handler you have to override 2 services
sonata.user.editable_role_builder
sonata.user.form.type.security_roles
And definitions will look like as below
<services>
<service id="sonata.user.editable_role_builder" class="Acme\DemoBundle\Security\EditableRolesBuilder">
<argument type="service" id="security.context" />
<argument type="service" id="sonata.admin.pool" />
<argument>%security.role_hierarchy.roles%</argument>
</service>
<service id="sonata.user.form.type.security_roles" class="Acme\DemoBundle\Form\Type\SecurityRolesType">
<tag name="form.type" alias="sonata_security_roles" />
<argument type="service" id="sonata.user.editable_role_builder" />
</service>
</services>
For reference see Sonata Admin Override Security Roles Handler

Convert xml to yaml, section "calls"

How I can convert this configuration from xml to yaml?
<service id="sonata.media.admin.media" class="%sonata.media.admin.media.class%">
...
<call method="setTemplates">
<argument type="collection">
<argument key="list">ApplicationSonataMediaBundle:MediaAdmin:list.html.twig</argument>
</argument>
</call>
...
</service>
Thanks!
Try this :
services:
sonata.media.admin.media:
class: %sonata.media.admin.media.class%
calls:
- [setTemplates, [list: "ApplicationSonataMediaBundle:MediaAdmin:list.html.twig"]]
Docs here
The online converter from Ross Tuck turns out to be a good tool!

Convert fos user bundle service into yml

I have a FosUserBundle service I need to convert to yml format. How can I do this is there a dumper convertor or anything?
How this would look in yml?
<service id="fos_user.mailer.twig_swift" class="FOS\UserBundle\Mailer\TwigSwiftMailer" public="false">
<argument type="service" id="mailer" />
<argument type="service" id="router" />
<argument type="service" id="twig" />
<argument type="collection">
<argument key="template" type="collection">
<argument key="confirmation">%fos_user.registration.confirmation.template%</argument>
<argument key="resetting">%fos_user.resetting.email.template%</argument>
</argument>
<argument key="from_email" type="collection">
<argument key="confirmation">%fos_user.registration.confirmation.from_email%</argument>
<argument key="resetting">%fos_user.resetting.email.from_email%</argument>
</argument>
</argument>
</service>
I was trying to use Yml dumper but this is just giving me serialised object:
$cs = new ContainerBuilder();
$loader1 = new Loader\XmlFileLoader($cs, new FileLocator(__DIR__ . '/../../../../vendor/friendsofsymfony/user-bundle/FOS/UserBundle/Resources/config'));
$loader1->load('mailer.xml');
$dumper = new \Symfony\Component\Yaml\Dumper();
file_put_contents(__DIR__ . '/test2.yml', $dumper->dump($cs));
Any tips would be much appreciated. Thanks in advance.
You can try do it in such way:
services:
fos_user.mailer.twig_swift:
class: FOS\UserBundle\Mailer\TwigSwiftMailer
arguments:
- #mailer
- #router
- #templating
- { template: { confirmation: %fos_user.registration.confirmation.template%, resetting: %fos_user.resetting.email.template% }, from_email: { confirmation: %fos_user.registration.confirmation.from_email%, resetting: %fos_user.resetting.email.from_email% } }
You use wrong dumper as well you should use Symfony\Component\DependencyInjection\Dumper\YamlDumper

Resources