FOSUserBundle customize error message change_password - php

I'm using Symfony 2.1 for a project and FOSUserBundle to manage the users.
I'm trying to customize the change password form and I can't display well the error messages. Indeed, when an input is wrong filled, the error message is printed between the label and the input (with a list structure). But I like to display it after the input or below it.
Moreover, I'd like to display my change password form within a settings page so I need to display some other forms. How can I integrate this form in a precise place in a page?
Thanks in advance,
Valentin

For my first question, I succeeded with this form for the change_password:
<form action="{{ path('fos_user_change_password') }}" {{ form_enctype(form) }} method="POST" class="fos_user_change_password">
<div class="form_errors_change_pwd">
{{ form_errors(form) }}
</div>
<div>
{{ form_label(form.current_password) }}
{{ form_widget(form.current_password) }}
<span class="form_error_field">{{ form_errors(form.current_password) }}</span>
</div>
<div>
{{ form_label(form.new.first) }}
{{ form_widget(form.new.first) }}
<span class="form_error_field">{{ form_errors(form.new.first) }}</span>
</div>
<div>
{{ form_label(form.new.second) }}
{{ form_widget(form.new.second) }}
<span class="form_error_field">{{ form_errors(form.new.second) }}</span>
</div>
{{ form_rest(form) }}
<div>
<input type="submit" value="{{ 'change_password.submit'|trans({}, 'FOSUserBundle') }}" />
</div>
But I'm still trying to integrate this form inside another page with multiple forms...

Related

Comment delete laravel

I want delete a comment, but when i click, this redirect me to laravel error page and display "" that error. Why is happen ?
web.php
Route::post('/comments/destroy/{id}', 'CommentController#destroy')->name('comment.destroy');
commentcontroller
public function destroy($id){
$comment=Comment::where('id',$id)->first();
$comment->delete();
return redirect()->back();
}
blade
#foreach($comment as $comments)
<form action="{{route('comment.destroy',$comments->id )}}" method="post">
<input type="hidden" name="_method" value="DELETE">
<button type="submit" class="btn"><i class="fa fa-trash-o"></i></button>
{{ csrf_field() }}
{{ method_field('DELETE') }}
<div class="comment-body"><p>{{ $comments->body }}</p></div>
</form>
#endforeach
If you add {{ method_field('DELETE') }} on the html form, you must change the route method to delete:
Route::delete('/comments/destroy/{id}', 'CommentController#destroy')->name('comment.destroy');
But if you want to use POST method on the route, you must remove {{ method_field('DELETE') }} from your form.

Laravel lightbox to show view/form

I have fleshed out a Laravel project but wanting to do something a little different, I am using Bootstrap so the standard nav and modal for the popout.
I am wanting to show the modal with the registration or login form inside, but as you can appreciate the way it is currently built, once you hit the login or register link it just renders the page view.
So I have my views/layouts/navigation.blade.php
<div class="container navigation">
<nav class="navbar navbar-default navbar-top">
<li class="navbar-right">Home</li>
<div class="container">
#if(Auth::check())
<li>Sign out</li>
<li>Change password</li>
#else
<li>Login</li>
<li><button type="button" class="btn btn-default navbar-btn data-toggle="modal" data-target="#myModal"">Sign Up</button></li>
#endif
</div>
</nav>
</div>
And seperate to this I have my views/account/create.blade.php & views/account/signin.php
#extends('layout.main')
#section('content')
<form action="{{ URL::route('account-create-post') }}" method="post">
<div class="field">
Email: <input type="text" name="email"{{ (Input::old('email')) ? ' value="' . e(Input::old('email')) . '"' : '' }}>
#if($errors->has('email'))
{{ $errors->first('email') }}
#endif
</div>
<div class="field">
Username: <input type="text" name="username"{{ (Input::old('email')) ? ' value="' . e(Input::old('username')) . '"' : '' }}>
#if($errors->has('username'))
{{ $errors->first('username') }}
#endif
</div>
<div class="field">
Password: <input type="password" name="password">
#if($errors->has('password'))
{{ $errors->first('password') }}
#endif
</div>
<div class="field">
Password Again: <input type="password" name="password_again">
#if($errors->has('password_again'))
{{ $errors->first('password_again') }}
#endif
</div>
<input type="submit" value="Create Account">
{{ Form::token() }}
</form>
#stop
Really I wanting the button in the nav to fire up the modal and show the forms inside? Amy ideas on how to approach this?
You'll basically be using Javascript on the client to display the form and process the request to the server and the server's response. I would:
Pick a CSS/Javascript framework that will display a modal form for you
Put your normal form generation inside the dialog it shows
Have a Javascript handler on the submit button of the modal dialog that makes an AJAX post to a new RESTful HTTP endpoint in Laravel that calls the same logic your standard signup form uses, but sends back a response your AJAX code will understand (e.g. either successful or failed, with an error message).
I hope i got the question right. The Key to achieve what you want should be a concept called nested views, which is very well explained on this coderwall. Also, check out the laravel docs on views (passage Passing A Sub-View To A View).
You would have to create a view for your login-form an nest it in the login modal as well as in the standard login-view (if you have one). Hope this helps.

Overriding "Change Password" Template from FOS User Bundle

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'.

Pass an object only if Auth is available in Laravel

Okay so I've got a view composer; that basically pulls a row from my table 'heros'. This then allow's me to obviously use the object $hero>whatever; on a page. The problem is this obviously only works for someone who is logged in.
View::composer(array('home'), function($view)
{
$view->with('herodata', Hero::where('owner_id', Auth::user()->id)->first());
});
As its using the
Auth::user()->
to find the correct row in the 'heros' table, once the user is no longer logged in it cannot access this. As no one is set to Auth.
So the error when I logout (or anyone no logged in) is this:
ErrorException
Trying to get property of non-object
));
//Authenticated group
Route::group(array('before' => 'auth'), function() {
View::composer(array('home'), function($view)
{
$view->with('herodata', Hero::where('owner_id', Auth::user()->id)->first());
});
How does one go about this?
Note: Still learning, I DID try and search but I found nothing probably because Im not sure how to put my issue into the correct sentance..
EDIT: Included home.blade.php
#extends('layout.main')
#section('content')
#if(Auth::check())
<p>Hello, {{{ Auth::user()->username }}}.</p>
#if($herodata)
Your hero:<br>
<b>{{ $herodata->hero; }}</b><br>
<b>Stats:</b>
LvL: {{ $herodata->level }}
Exp: {{ $herodata->exp }}
HP: {{ $herodata->hp }}/{{ $herodata->max_hp }}
Str: {{ $herodata->str }}
Atk: {{ $herodata->atk }}
Def: {{ $herodata->def }}
Int: {{ $herodata->int }}
Blk: {{ $herodata->blk }}
<form action="{{ URL::route('hero-delete') }}" method="POST">
<input type="submit" value="Delete"><br>
</form>
<form action="{{ URL::route('rest') }}" method="POST">
<input type="submit" value="Rest">
</form>
<form action="{{ URL::route('attack') }}" method="POST">
<input type="submit" value="Random attack">
</form>
<form action="{{ URL::route('hero-died') }}" method="POST">
<input type="submit" value="Died">
</form>
#else
<form action="{{ URL::route('hero-create') }}" method="POST">
Hero:<br>
You do not have a hero, create one!
<input type="text" name="hero">
<input type="submit" value="Create hero">
#if($errors->has('hero'))
{{ $errors->first('hero')}}
#endif
{{ Form::token()}}
</form>
#endif
#else
<p>You are not signed in.</p>
#endif
#stop
Check if the user is logged in before setting the hero data:
if (Auth::check())
{
View::composer(array('home'), function($view)
{
$view->with('herodata', Hero::where('owner_id', Auth::user()->id)->first());
});
}

How to customize Edit-Form

I already had a post that lead to this question but now I isolated one problem and so its better to make a new clean post :
Symfony 2.1.3 with jordillonch/CrudGeneratorBundle
I used this CrudGenerator on an entity in which I have 2 timestampable fields that need to be updated automatically :
I installed Gedmo for the timestampable funtionnality.
The fields are :
cree_le (in english created_at) : #Gedmo\Timestampable(on="create")
and modifiele (in english updated_at) - #Gedmo\Timestampable(on="update")
The problem is that I want to customize the form to show only the fields to update by the user.
When I use the not customized edit.html.twig using {{ form_widget(edit_form) }} the update is working but the values of updated_at are not changed because the old value is submitted with the form.
I tried several things to customize the form but I did not yet find the solution.
This version of customized edit.html.twig is not working because the submitted form is not valid :
<form class="well" action="{{ path('employee_update', { 'id': entity.id }) }}" method="post" {{ form_enctype(edit_form) }}>
<input type="hidden" name="_method" value="PUT" />
<div>
{{ form_label(edit_form.nom) }}
{{ form_errors(edit_form.nom) }}
{{ form_widget(edit_form.nom) }}
</div>
<div>
{{ form_label(edit_form.email) }}
{{ form_errors(edit_form.email) }}
{{ form_widget(edit_form.email) }}
</div>
<div>
{{ form_label(edit_form.telephone, 'Téléphone') }}
{{ form_errors(edit_form.telephone) }}
{{ form_widget(edit_form.telephone) }}
</div>
<div>
{{ form_label(edit_form.actif) }}
{{ form_errors(edit_form.actif) }}
{{ form_widget(edit_form.actif) }}
</div>
<input type="hidden" id="too_employeebundle_employee_cree_le" name="too_employeebundle_employee[cree_le]" value="{{ entity.creele|date('Y-m-d H:i:s') }}">
<input type="hidden" id="too_employeebundle_employee_modifie_le" name="too_employeebundle_employee[modifie_le]" value="{{ entity.modifiele|date('Y-m-d H:i:s') }}">
{{ form_widget(edit_form._token) }}
<p>
<button type="submit" class="btn btn-success">{{ 'views.edit.editbutton'|trans({}, 'JordiLlonchCrudGeneratorBundle') }}</button>
</p>
</form>
Sure I'm doing something wrong but who could tell me how to go on ?
Remove cree_le and modifiele from your Form Class and dont render them on your template.
Then let Timestampable do his job! when you create something the cree_le value will be automatic set. When you change something the modifiele value will be automatic set too.

Categories