Symfony2 simple contact form on every page - php

I would like to have a simple contact form in the footer of every page. I already created a controller which is working fine on a seperate page. But when i render the controller inside of a template:
{% render(controller('MyBundle:Default:contact', {'request': app.request })) %}
It is working for rendering the form, validation and sending the mail BUT
1.) my flashmessage is not shown:
$this->get('session')->getFlashBag()->add(...)
and 2.) When i try to redirect i get an error
return $this->redirect($this->generateUrl('homepage'))
So my question is:
How can i set the flash message and redirect?
Is rendering a controller in twig actually the best practice? Or are there other approaches?
(I also tried to create a twig extension but ran into different other problems like i cant use formbuilder function etc...)

Method get from FlashBag apart from getting the message in addition removes it... So probably you've invoked get method somewhere before. For example following code always shows nothing:
{% if (app.session.flashbag.get('message')) %}
<div class="message">{{ app.session.flashbag.get('message') }}</div>
{% endif %}
If this is the case, then instead of the first get you should use has method in the condition.
Moreove if you want get and only get the message from the flash bag - without removing it, you can use peek method.

Related

How to dynamically load and display translations in TWIG from PHP variables?

I'm trying to make the trans('key' : value) method work by providing the variables either by PHP or preparing them on TWIG, both without any success so far.
So far I have tried to send the full line from the PHP Controller, which would be
{{bodymail| trans({'%user.name%': user.name,'%user.surname%': user.surname}) | raw}}
The following above works correctly when hard-coded, but when sending/composing it, it displays either the above code literally, or the correct message but without applying the translations.
This is something else I have tried, now on TWIG itself
{% set message = "'"~bodymail~"'| trans({" %}
{% for sub in variables %}
{% set message = message~"'%"~sub~"%': "~sub~"," %}
{% endfor %}
{% set message = message | trim (',', 'right') %}
{% set message = message~"}) | raw" %}
{{message}}
Code above is something I also tried for constructing the whole thing in TWIG itself... which resulted in directly not rendering the bodymail message (reference to .yml file).
Trying to make it work as a generic for anything that I input following the format (now i'm using user.name and user.surname, but I might want to use product references later for example).

Symfony 3.4 - form_errors form theme not displaying

EDIT: Doing some digging, and it looks like my problem stems from the fact that the form I'm creating builds off of the FOSUserBundle's registration form. Why is that an issue? Because while their RegistrationController throws a REGISTRATION_FAILURE event if the form isn't valid, it doesn't look like the event has a listener. So, the invalid state doesn't actually do anything aside from not being handled at all by the system. It doesn't even return the form (with errors).
I have a relatively simple form theme I want to use in order to display my form errors in a block at the top of my form:
{% form_theme form _self %}
{% block form_errors %}
{% spaceless %}
{% if errors|length > 0 %}
<div class="errors pt-2 mb-2">
<ul>
{% for error in errors %}
<li>{{ error.message }}</li>
{% endfor %}
</ul>
</div>
{% endif %}
{% endspaceless %}
{% endblock form_errors %}
I reference it in the form template with:
{% form_theme form 'Form/errors.html.twig' %}
And then I try to invoke it with a simple:
{{ form_errors(form) }}
But, when I intentionally provide incorrect data to my form inputs and try to submit, there's no error div present. The submission fails, due to there being 6 validation errors, but no errors are displayed/in the HTML source.
Looking at the documentation, I think I'm doing it right, but I'm obviously missing something.
You have two problems and you have to solve both of them in order to make the form validation work.
1st Problem:
First problem is that the listener is not working. If you intentionally make errors in the form data and the form validation is not kicking in, then you will never see the form_errors template. You have to go over the documentation for the FOSUserBundle.
First, check if you enabled/configured the validation.yml correctly.
Secondly, check the PHP class that generates the form. Make sure that the validation criteria are set properly.
Now, the most reliable way to check if the form is producing errors is to {{dump(form)}} in the Twig template. It will show all the data for the form, including the errors.
Keep working at it until you manage to see some errors. Otherwise you will not be able to do the next step.
2nd Problem:
The second problem is that you probably not including the correct template. I can not count how many times I had the same problem. You may have 3 or 4 templates that contain html/twig code for <form> but Symfony actually uses like 1 or 2 of them. Add your code to every one of them and make sure that your Twig block is not overridden by some other form block.
Also keep in mind that you have to clean both the app cache (php bin/console cache:clean -e prod) and the browser cache after every change in the templates. You might have done everything right and the damn cache is showing the cached results.
One last advice: use the browser developer tools. Often you find a lot of information and errors are shown in the tools' console.
what is the error of form?
For throwing error you have to set asserts on your entity.
for example:
class User extend baseUser
{
/**
* #Assert\NotBlank
*/
public $name;
}
for more information: https://symfony.com/doc/current/validation.html

Find Symfony Widget Content File Location

For the first time, i am working on Symfony widgets.
{% render "EducateToolsBundle:Shared:selectMenu" with {'entity': 'Stores'} %}
its Meaning is SharedController -->selectMenuAction
{% form_theme form 'EducateToolsBundle:shared:_form_theme.html.twig' %}
What is the meaning of this.
From where i am getting the values to this form .?
You must look at selectMenuAction() method in the Shared controller. It is that function that set what template is used. if using the default Symfony coding standards it should be something like selectMenu.html.twig in a Shared sub folder in the Resources/views of the bundle.
You should looks at the documentation first :
https://symfony.com/doc/current/form/form_customization.html
Because we won't teach you symfony here, it's very well explain in the documentation and it is a task too big for us.
In your case theForm.id is the field of the formType we want to displays.
If we want to display the <input> of an form of user lastname we would use
form_widget(userForm.lastname)
form_widget tells symfony to display only the widget (the ) of the id given.
if you want to have a label + input + the errors of the field you could use
form_row(userForm.lastname)
wich is almost equal to
<div>
{{ form_label(form.lastname) }}
{{ form_errors(form.lastname) }}
{{ form_widget(form.lastname) }}
</div>
(it depends of the form theme, but you should read the doc for the details ;) )

Using partial view, and second controller on one view in Symfony 2

I have a question: how can 1 view/page use 2 controllers to display different data?
For example:
1) Page displays DVD's information using DvdController.
2) On that same page I want to use a Template/Partial view, that displays list of actors, and that list should come from the second ActorsController.
I can add a template view inside another page but the second controller doesn't work:
MainPage on URL: website/dvd
{% block body %}
<div>
<h1>{{ dvd.title }} </h1>
</div>
<div>
{{ include('DVDBundle:Actors:actors.html.twig') }}
</div>
{% endblock %}
Above I use the partial view actors.html.twig to show the actors, strangely the html code/div's and so on are actually displayed and working on the page, however the controllers(ActorsController) method that meant to return this partial view is not executed for some reason.?
But if I go to the view directly via link: website/actors then its working.
The Symfony documentation on embedded controllers explains this nicely.
In short, you will need to do something like this in your template file:
<div>
{{ render(controller(
'DVDBundle:Actors:actors',
{ ... parameters to the controller action here ... }
)) }}
</div>
The important thing is calling the render function, which allows a controller to be rendered, as opposed to simply includeing a template. (This assumes that your have an ActorsController with an actorsAction method; change the names as appropriate to your controller.)

Twig: url encoded twice while passing it to Twitter share button

I'm developing a simple page with Symfony2, using Twig as template engine.
I have a list of urls, and I'd like to add the Twitter share button for each url. What I do is a simple cycle on the urls array, and the dinaycally set the url for every Twitter button inside the cycle. It looks like that twig encodes the url at first, and the Twitter script encodes it again. So The Twitter share count doesn't match. The code (inside the cycle) is the following, there is another part of Twitter code at the end of the page:
Tweet
The url I get on the rendered page is: http%253A%252F%252Fwww.example.com%252F (two encoding pass)
instead of http%3A%2F%2Fwww.example.com%2F (one encoding pass, correct). It looks like the % is encoded again to %25.
And this doesn't make Twitter count work, because it consider those two as different urls.
I also tried to use some filters, e.g. {{ s.url|raw }}, but it didn't work.
So my question is: how to avoid this? Is there a way to tell twig (or twitter) to not encode the url?
You can always turn autoescaping off in Twig by using the {% autoescape false %} declaration before the code you want to leave raw. This will leave any strings you output unescaped, and thus your URL will not be escaped twice. Make sure you turn autoescaping back on with {% endautoescape %}
{% autoescape false %}
Tweet
{% endautoescape %}
Full Twig HTML Escaping Documentation
An old post but looks like you can use the "raw" filter now. This should do:
{{ s.url|raw }}
http://twig.sensiolabs.org/doc/api.html#escaper-extension

Categories