I'm using yml format to translate my web app but I've one problem.
What I would like to do:
#exception.en.yml
exception.bad: 'Bad credentials'
What I know is possible to do:
#exception.en.yml
'Bad credentials': 'Bad credentials'
Is this the only method to translate the exception?
simply put in the translator and remember to add the trans statement on the messagge error dump in the Twig template.
Here an xliff example:
messages.en.xlf
<trans-unit id="1">
<source>User account is disabled.</source>
<target>Account disabled or waiting for confirm</target>
</trans-unit>
<trans-unit id="2">
<source>Bad credentials</source>
<target>Wrong username or password</target>
</trans-unit>
and in the template
{# src/Acme/SecurityBundle/Resources/views/Security/login.html.twig #}
{% if error %}
<div>{{ error.message|trans }}</div>
{% endif %}
<form action="{{ path('login_check') }}" method="post">
<label for="username">Username:</label>
<input type="text" id="username" name="_username" value="{{ last_username }}" />
<label for="password">Password:</label>
<input type="password" id="password" name="_password" />
<button type="submit">login</button>
</form>
Check this doc for exclude non active users
hope this help
Related
I installed FOSUserBundle on my Symfony2 website and I overwrited the login and register pages. It's working very well, but now, I have another issue :
Making a pop up with an AJAX check for the login and register part, working with the different routes, controllers, etc.
How to do that the best way ?
My pop up is a simple bootstrap modal of login.html.twig page for FOSUserBundle, here is the code of the modal-body for the login part (I will use the register form for the register part) :
<div class="modal-body">
{% trans_default_domain 'FOSUserBundle' %}
{% block fos_user_content %}
<form action="{{ path("fos_user_security_check") }}" method="post">
<input type="hidden" name="_csrf_token" value="{{ csrf_token }}" />
<label for="username">{{ 'security.login.username'|trans }}</label>
<input type="text" id="username" name="_username" value="" required="required" />
<label for="password">{{ 'security.login.password'|trans }}</label>
<input type="password" id="password" name="_password" required="required" />
<br /><br />
<input type="submit" id="_submit" name="_submit" value="{{ 'security.login.submit'|trans }}" />
</form>
{% endblock fos_user_content %}
</div>
I already searched on StackOverflow and other websites but didn't find the good answer I was looking for.
Thank you.
no token when you use AJAX
user=$('#username').val();
pass=$('password').val();
$.ajax(
{
type: "POST",
url: "{{ path ('your-rout')}}",
data: {'user': user,'pass':pass},
beforeSend: function() {
},
success: function(json) {}
});
I've installed Symfony2, FOS User Bundle and Twitter Bootstrap.
Then I setup the /app/Resources/FOSUserBundle/views/layout.html.twig template to override FOSUserBundle to use my site template.
It all works if I have a link to /login on the homepage.
Now I want to implement a template like the hero template where the login form is part of the main template.
The closest I've got is to use this in the main template:
{% render controller("FOSUserBundle:Security:login") %}
I can override the layout html to not extend main template, but this removes all styling from /login
Any ideas how I can handle both scenarios?
You were almost there :)
you can include the login form in any other template using the render function.
{% render controller("FOSUserBundle:Security:login") %}
... you just have to create app/Resources/FOSUserBundle/views/Security/login.html.twig and ommit the wrapping {% block fos_user_content %} found in FOSUserBundle's login.html.twig in order to have it return the form directly:
{% if error %}
<div>{{ error|trans }}</div>
{% endif %}
<form action="{{ path("fos_user_security_check") }}" method="post">
<input type="hidden" name="_csrf_token" value="{{ csrf_token }}" />
<label for="username">{{ 'security.login.username'|trans }}</label>
<input type="text" id="username" name="_username" value="{{ last_username }}" required="required" />
<label for="password">{{ 'security.login.password'|trans }}</label>
<input type="password" id="password" name="_password" required="required" />
<input type="checkbox" id="remember_me" name="_remember_me" value="on" />
<label for="remember_me">{{ 'security.login.remember_me'|trans }}</label>
<input type="submit" id="_submit" name="_submit" value="{{ 'security.login.submit'|trans }}" />
</form>
Then adjust it to fit your template.
Richard Miller's post has helped me achieve what I was trying to do.
http://richardmiller.co.uk/2013/02/18/symfony2-ajax-and-full-page-templates/
{% extends app.request.attributes.get('partial')
? '::ajax-layout.html.twig'
: '::full-layout.html.twig' %}
I couldn't get app.request.partial to work, and decided choosing based on xmlRequest wasn't ideal.
I did some research and sadly couldn't find any help for that.
So I'm rendering the FOSUserBundle ChangePasswordAction into a template of mine, but it displays the default template given from the vendor.
My template where the controller is rendered:
{% block body %}
<h2>Einstellungen</h2>
<br/>
<h4>Ändern Sie ihr Passwort</h4>
{% render controller("FOSUserBundle:ChangePassword:changePassword") %}
{% endblock %}
My template for the ChangePasswordAction, the path is app/Resources/FOSUserBundle/views/ChangePassword/changePassword.html.twig:
{% block fos_user_content %}
{% trans_default_domain 'FOSUserBundle' %}
<form action="{{ path('fos_user_change_password') }}" {{ form_enctype(form) }} method="POST" class="fos_user_change_password">
{{ form_start(form) }}
{{ form_errors(form) }}
<p><span class="edit_left_date">{{ form_label(form.current_password, 'Jetziges Passwort') }} </span>
<span class="edit_right">{{ form_widget(form.current_password) }}</span></p>
<p><span class="edit_left_date">{{ form_label(form.new_password, 'Neues Passwort') }} </span>
<span class="edit_right">{{ form_widget(form.new_password) }}</span></p>
<p><span class="edit_left_date">{{ form_label(form.new_password_confirmation 'Bestätigen Sie ihr neues Passwort') }} </span>
<span class="edit_right">{{ form_widget(form.new_password_confirmation) }}</span></p>
<div>
<input type="submit" value="{{ 'Speichern'|trans }}" />
</div>
{{ form_end(form) }}
</form>
{% endblock %}
What drives me crazy is that i render the login controller from the FOSUserBundle the same way in my layout.html.twig and it's working perfectly there.
The snippet of code from the layout:
{% block sidebar %}
{% render controller("FOSUserBundle:Security:Login") %}
{% endblock %}
and the app/Resources/FOSUserBundle/views/Security/login.html.twig:
{% block fos_user_content %}
<div class="teaser-header">Login</div>
<form role="form" action="{{ path("fos_user_security_check") }}" method="post">
<input type="hidden" name="_csrf_token" value="{{ csrf_token }}" />
<div class="form-group">
<label for="username">{{ 'MyLog Username'|trans }}</label>
<input class="form-control" type="text" id="username" name="_username" placeholder="Your Username" required="required" />
</div>
<div class="form-group">
<label for="password">{{ 'Password'|trans }}</label>
<input class="form-control" type="password" id="password" name="_password" placeholder="Your Password" required="required" />
</div>
<input type="submit" id="_submit" name="_submit" value="{{ 'Segel setzen'|trans }}" />
<a id="passwordForget" href={{ path('fos_user_resetting_request') }}>Password vergessen?</a>
</form>
{% endblock %}
Am i missing something or what am i doing wrong so i can't override the changePassword Template?
Moving from comments to an answer:
Have you cleared your cache since adding the new template?
What you basically do is you create a user bundle of your own + set it's parent to the FOS user bundle. (You should already have done this step)
Then you set the template in:
MyCompany/MyProject/UserBundle/Resources/views/Resetting/request.html.twig
Because of the fact that the file in the FOS user bundle is on the same place, your file will overwrite that template.
If you think you've done everything correctly and still it doesn't work, you can try to clear the cache with a Symfony command in the terminal, or in some cases manually delete the cache directories - 'app/cache/dev' or 'app/cache/prod'.
I use FOSUserBundle with Symfony2. By default FOSUserBundle profile edit and password change is on different pages, but I need these forms on one page and add one custom form. My idea is to override profile controller with additional forms, but maybe there is more easier way? I also had idea to post all forms in different controllers, but problem is that I need to show errors for any field and I don't think, that is good practice to save object in flash.
I got a sidebar with the login and a few teaser on my page in my layout, so it's available from everywhere.
Just do the following in the twig.html file you want to use to display the login/registration etc
{% extends "::base.html.twig" %}
{% block title %} Title {% endblock %}
{% block sidebar %}
{% render controller("FOSUserBundle:Security:Login") %}
{# Other Controller you want to render here #}
{% endblock %}
My login.twig.html file looks like this
{# app\Resources\FOSUserBunle\Security\views\login.twig.html #}
{% block fos_user_content %}
<div class="teaser-header">Login</div>
<form role="form" action="{{ path("fos_user_security_check") }}" method="post">
<input type="hidden" name="_csrf_token" value="{{ csrf_token }}" />
<div class="form-group">
<label for="username">{{ 'MyLog Username'|trans }}</label>
<input class="form-control" type="text" id="username" name="_username" placeholder="Your Username" required="required" />
</div>
<div class="form-group">
<label for="password">{{ 'Password'|trans }}</label>
<input class="form-control" type="password" id="password" name="_password" placeholder="Your Password" required="required" />
</div>
<input type="submit" id="_submit" name="_submit" value="{{ 'Segel setzen'|trans }}" />
</form>
{% endblock %}
I know that this question is perfectly answered in FOSUserBundle documentation, but yet I can't solve my problem.
I am trying to make an own login page using FOSUserBundle.
I have a main.html.twig that is EXACTLY the same as the login.html.twig included in the FOSUserBundle source.
The only difference is that main.html.twig is in my own bundle structure and login.html.twig is in FOSUserBundle folder structure.
I reach both /login and /main. I resolve them and the render starts.
But when accessing /main, I get the following error:
Twig_Error_Runtime: Variable "csrf_token" does not exist in "AcmeStoreBundle:Main:main.html.twig" at line 5
The code is known by the FOSUserBundle users, but anyway I paste it here:
<form action="{{ path("fos_user_security_check") }}" method="post">
<input type="hidden" name="_csrf_token" value="{{ csrf_token }}" />
<label for="username">{{ 'security.login.username'|trans({}, 'FOSUserBundle') }}</label>
<input type="text" id="username" name="_username" value="" />
<label for="password">{{ 'security.login.password'|trans({}, 'FOSUserBundle') }}</label>
<input type="password" id="password" name="_password" />
<input type="checkbox" id="remember_me" name="_remember_me" value="on" />
<label for="remember_me">{{ 'security.login.remember_me'|trans({}, 'FOSUserBundle') }}</label>
<input type="submit" id="_submit" name="_submit" value="{{ 'security.login.submit'|trans({}, 'FOSUserBundle') }}" />
The variable "csrf_token" is somehow not recognized from outside FOSUserBundle. Or something else that I am not getting.
Any clue over there?
Since you are not using FormBuilder, in your action that is responsible for rendering main.html.twig you should generate this token and pass it to the View.
$csrf = $this->get('form.csrf_provider'); //Symfony\Component\Form\Extension\Csrf\CsrfProvider\SessionCsrfProvider by default
$token = $csrf->generateCsrfToken($intention); //Intention should be empty string, if you did not define it in parameters
You should pass $token as csrf_token variable to your View
Check also my answer to similar question