How to customize Edit-Form - php

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.

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.

Symfony2 : Session upload progress

I'm trying to use the php upload_progress feature with symfony2. I set my session.upload_progress.prefix and session.upload_progress.name in my php.ini.
My form below :
<form id="form-import-file" action="" method="post" {{ form_enctype(form) }} class="form-horizontal">
{{ form_widget(form.file, { 'attr':{'class':'input-file-import'}}) }}
{{ form_rest(form) }}
{{ form_errors(form.file) }}
<input type="hidden" name="{{ upload_progress_name }}" value="123" />
<button type="submit" class="btn btn-success">Submit</button>
</form>
where upload_progress_name = ini_get("session.upload_progress.name").
The upload is okay, but the session doesn't show any upload infos.
Any help ?
Ensure that <input type="hidden" name="{{ upload_progress_name }}" value="123" />
is before the file input fields. Just place it under the form tag.
Also what helped me out is this list: issues on php upload progress
Finally there is good symfony2-bundle, which may help you to not reinvent everything new.OneUpBundle
With this bundle you can pick a Frontend solution or create your own one.

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 use update Resource Controllers laravel 4?

I have Customer Controller with index, edit, update methods
Route::resource('customer', 'CustomerController');
Controller methods update
public function update($id) { echo $id; }
my HTML Form
<form action="/customer/1" method="post">
<input type="text" name="email" value="" />
<input type="submit" value="" />
</form>
I have following a Documentation here
http://four.laravel.com/docs/controllers#resource-controllers
PUT/PATCH /resource/{id} update
It's seems not working for me, how to use it? thank you
To use the PATH, PUT or DELETE HTML methods you need to add a hidden input with _method. Like the following...
<input type="hidden" name="_method" value="PUT" />
You can use the Form Builder. Example using blade:
{{ Form::open(array('method' => 'DELETE')) }}
That will automatically add this for you
<input name="_method" type="hidden" value="DELETE">
This works for me in Laravel 4:
{{ Form::open(array('url' => URL::to('customer/1'), 'method' => 'PUT')) }}
I am using Laravel resource controller. For update a page, I copied it from insert page after then
Just I added an extra field to update view like as
{{ method_field('put') }}
Just use this as for update
<form method="post" action="{{ URL::to('customer',$customer['id'])}}">
{{ csrf_field() }}
{{ method_field('put') }}

FOSUserBundle customize error message change_password

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

Categories