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.
Related
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.
I would like to have the current route in Symfony 3 in a Twig view.
I already did some research, but I didn't find any solution. I try the following in my Twig view:
{{ app.request.uri }}
It returns something like: http://localhost:8000/_fragment?_path=_format%3Dhtml%26_locale%3Dfr%26_controller%3DFOSUserBundle%253ASecurity%253Alogin
{{ app.request.get('_route') }}
Always returns NULL.
{{ app.request.getpathinfo }}
Always have: /_fragment
What I need is very simple. For an URL like localhost:8000/article/5, I would like to retrieve /article/5.How to do this?
The following code snippet will do the trick:
{{ path(app.request.attributes.get('_route'), app.request.attributes.get('_route_params')) }}
app.request.attributes.get('_route') - returns current route name.
app.request.attributes.get('_route_params') - returns current route params.
path() - generates route path by route name and params.
This approach will not work for forwarded requests. In case of forward request, there is a workaround: you should pass "_route" and "_route_params" to a forwarded params list.
return $this->forward('Yourbundle:Controller:action', array(
//... your parameters...,
'_route' => $this->getRequest()->attributes->get('_route'),
'_route_params' => $this->getRequest()->attributes->get('_route_params');
));
Also you can use app.request to access any Request object functions, such as getPathInfo(), which should return current route path.
{{ app.request.pathInfo }}
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' }}) }}
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!
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()
);