How do I use TrSteelCkEditorBundle in Symfony2? - symfony

I am new in the world of sf2 and I am trying to learn it.
I installed TrSteelCkEditorBundle with composer and now I am trying to get the editor in a view.
My bundle is active in the AppKernel.
As a beginner my question is:
What do I have to do to make it works?
I put this code and paste the value in the render
$form = $this->createFormBuilder()
->add('title', 'text')
->add('content', 'ckeditor', array(
'transformers' => array(),
))
->getForm();
And in the twig view i have line 6:
{{ form_widget(form) }}
but i'm getting an error :
An exception has been thrown during the rendering of a template ("Catchable Fatal Error:
Argument 1 passed to Symfony\Component\Form\FormRenderer::searchAndRenderBlock()
must be an instance of Symfony\Component\Form\FormView,
instance of Symfony\Component\Form\Form given, called in
/Applications/mamp/htdocs/Sf2/app/cache/dev/twig/5c/eb/e10823d760716de7f56b39640e79.php
on line 29 and defined in
/Applications/mamp/htdocs/Sf2/vendor/symfony/symfony/src/Symfony/Component/Form/FormRenderer.php
line 131") in amTestBundle:Default:index.html.twig at line 6.
If someone had a clue to resolve that it'll help me a lot.
Thank you.

The {{ form_widget(form) }} doesn't work because your $form variable is the form itself. In order to get Twig to create the widgets for it, you have to send it to the Twig template with:
$form->createView()
Here is an example of when you return in your controllerAction:
return $this->render(
'AcmeFooBundle:Acme:template.html.twig',
array('form' => $form->createView()) //Here you see the createView()
);

Related

Silex, how to translate login error messages?

How to translate security error messages like 'Bad credentials' in Silex?
Currently I show login form using this code from Silex docs https://silex.symfony.com/doc/2.0/providers/security.html:
$app->get('/login', function(Request $request) use ($app) {
return $app['twig']->render('login.twig', array(
'error' => $app['security.last_error']($request),
'last_username' => $app['session']->get('_security.last_username'),
));
});
twig:
{{ error }}
But looks like $app['security.last_error'] is just a string, so I can't get its key for translation like this {{ error.messageKey|trans(error.messageData, 'security') }}.
This http://symfony.com/doc/2.8/security/form_login_setup.html suggests to use $this->get('security.authentication_utils')->getLastAuthenticationError() but looks like it's not available in Silex?
It's not ideal, but I am simply using Bad credentials as a key in my array with translations.

Unable to set custom data in show action field in symfony sonata admin

I have a show page and I want to add a custom value.
I have tried doing what I did in other actions which is to add an array to the
third parameter with the data key like so:
protected function configureShowFields(ShowMapper $showMapper)
{
$showMapper
->add('name')
->add('dateEnd')
->add('example', null,
array('data' => 'example value')
)
;
}
In the configureListFields action, this works. I have injected custom values with the data attribute.
But still I am not able to access key example in the show.html.twig file.
It gives me this error
Variable "example" does not exist.
What should I do to access this custom variable in the twig file ?
Try
{{ elements.elements.example.options.data }}
in your twig template
I used this solution. In the configureShowFields() method of an Admin class:
$showMapper
->with('Tab Name')
->add(
'any_name',
null,
[
'template' => 'Admin/Custom/any_name_show_template.html.twig',
'customData' => $this->someRepository->getSomeEntityBy($field),
'anotherCustomData' => $this->someService->getSomeDataBy($value),
]
)
;
In the custom template, you can access custom data by field_description.options.<customFieldName>, so for provided example data accessors would be {{ field_description.options.customData }} and {{ field_description.options.anotherCustomData }}
For the shorter field name in the Twig template, you can do like this:
{% set customData = field_description.options.customData %}
and access the custom data like {{ customData }}
Hope this helps and saves time.

Symfony2 Form Anchors

I have a number of forms on the one page under different tabs
After the form is processed, I would like to return to the same tab as the form was sent from.
Basically, I would like to modify the target_route to go to the current page with an Anchor at the end of the URL. (EG company/view/6#editdetails)
Could someone provide or link to an example that I can put in my controller or into twig?
The answer is simply:
$form = $this->createForm(new ContactType($contact), $contact, array(
'method' => 'POST',
'action' => '#editdetails'
));
You may also set this in the template itself as an attribute. See example:
{{ form_start(form, {attr: { novalidate: "novalidate", class: 'requiredStar', action: '#form' }}) }}

How can I unmap a File input without using the form builder?

I have inherited an application that seems to have existed before the form builder existed. The developer kind of rolled his own with twig macros. Now I want to add a file upload to some existing forms, but I get what seems to be a well known error:
There was 1 error:
myBundle\Tests\Controller\snip\DefaultControllerUnitTest::testCanStoreDocumentAtS3
Exception: Serialization of
'Symfony\Component\HttpFoundation\File\UploadedFile' is not allowed
C:\apath\vendor\symfony\symfony\src\Symfony\Component\HttpKernel\DataCollector\DataCollector.php:27
C:\apath\vendor\symfony\symfony\src\Symfony\Component\HttpKernel\Profiler\Profiler.php:218
The solutions is to unmap the file field:
$builder->add('pic','file');
to this :
$builder->add('pic','file', array('mapped'=>false));
But in this case a builder is not used. Instead it looks like this:
{# file(name, value) #}
{% macro file(name, value) %}
<input type="file" name="{{ name }}" id="{{ name }}" value="{{ value }}" />
{% endmacro %}
Is there anything I can add to this macro, or do in the controller action to keep the Profiler from serializing this?
The answer was in part pilot error, but there is enough going on here that I hope I can help others avoid the same pitfall.
This error was triggered by a unit test where I was simulating a file upload along with other parameters. Initially the code that caused this error was:
$photo = new UploadedFile(
__DIR__ . '/TestReportMethod.xlsx',
'TestReportMethod.xlsx',
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
123
);
....
$form['reportFile'] = $photo;
//submit
This caused the error in my original post.
After trying a few things I moved to this form instead.
$crawler = $this->client->request('POST',
$url,
array('id' => 328),
array('agencyInfoId' => 328),
array('reportFile' => $photo)
);
Which causes an InvalidArgumentException I think a little better example in the cookbook could have prevented. The correct form needs to be an array of parameter values, followed by an array of files:
$crawler = $this->client->request('POST',
$url,
array('id' => 328,
'agencyInfoId' => 328),
array('reportFile' => $photo)
);
I hope this helps somebody else!

Symfony2 Form with collection type (many-to-many)

What I have
I have a database table "Teams" and a table "Players", connected with a Teams_has_Players table (many-to-many relationship). I generated my entities. I can now succesfully retrieve the players from a team by doing: $players = $team->getPlayers()->toArray();
What I'm trying to do
I'm trying to make a form where you can edit all of the team's player names and their position on the field. So I basically would like to have some rows with in each row an input field with the name and an input field with his location.
What I tried to do
So I read a lot about the Symfony2 Collection type and tried this:
$form = $this->createFormBuilder(null)
->add('name', 'text', array('label' => 'Name', 'data' => $team->getName()))
->add('players', 'collection', array('data' => $team->getPlayers()->toArray()))
->getForm();
In my view I tried this:
<ul>
{% for player in form.players %}
<li>
{{ form_widget(player.name) }}{{ form_widget(player.position) }}
</li>
{% endfor %}
</ul>
But I get this error:
The form's view data is expected to be of type scalar, array or an
instance of \ArrayAccess, but is an instance of class
MatchTracker\Bundle\AppBundle\Entity\Players. You can avoid this error
by setting the "data_class" option to
"MatchTracker\Bundle\AppBundle\Entity\Players" or by adding a view
transformer that transforms an instance of class
MatchTracker\Bundle\AppBundle\Entity\Players to scalar, array or an
instance of \ArrayAccess.
So I added 'data_class' => 'MatchTracker\Bundle\AppBundle\Entity\Players', but then I get an error that The form's view data is expected to be an instance of class MatchTracker\Bundle\AppBundle\Entity\Players, but is a(n) array. You can avoid this error by setting the "data_class" option to null
Anyone that can help me solve this problem? I just want to edit the team's player names/locations/.. in a form. If that works I'm going to extend the form so I can add/remove players.
This is what your are trying to do.

Resources