Symfony3 apply class to each div of generated form - html

I want to apply a css class 'pure-control-group' to all div of a generated form.
<form name="form" method="post" class="pure-form pure-form-aligned">
<div>
<label for="form_Title" class="required">Titre</label>
<select id="form_Title" name="form[Title]" class="pure-control-group">
<option value="Modification" >Modification</option>
<option value="Construction" >Construction</option>
<option value="Autre" >Autre</option>
</select>
</div>
<div>
<label for="form_ContactWay" class="required">Moyen de Contact</label>
<select id="form_ContactWay" name="form[ContactWay]" class="pure-control-group">
<option value="Telephone" >Téléphone</option>
<option value="Email" >Email</option>
<option value="Direct" >Direct</option>
<option value="Autre" >Autre</option>
</select>
</div>
<div>
<label for="form_Log" class="required">Journal</label>
<textarea id="form_Log" name="form[Log]" required="required" class="pure-control-group"></textarea>
</div>
<div>
<button type="submit" id="form_Enregistrer" name="form[Enregistrer]">Enregistrer</button>
</div>
<input type="hidden" id="form__token" name="form[_token]" value="c19WunU5AgDgc954I3DRJXLqEhQwpOyDCBZEpF7akJs" />
</form>
I tried :
$this->logForm = $this->createFormBuilder($log, array('allow_extra_fields' => true))
->add('Title', ChoiceType::class, array(
'label' => 'Titre',
'choices' => array(
'Modification' => 'Modification',
'Construction' => 'Construction',
'Autre' => 'Autre'),
'attr'=> array('class'=>'pure-control-group')))
->add('ContactWay', ChoiceType::class, array(
'label' => 'Moyen de Contact',
'choices' => array(
'Téléphone' => 'Telephone',
'Email' => 'Email',
'Direct' => 'Direct',
'Autre' => 'Autre'),
'attr'=> array('class'=>'pure-control-group')))
->add('Log', TextareaType::class, array(
'label'=> 'Journal',
'attr'=> array('class'=>'pure-control-group')))
->add('Enregistrer', SubmitType::class)
->getForm();
The problem is that the class is add to the input. label_attr do the job but for the label.
How can I do for the div?
Notice that I would appreciate not to render each field by hand.

You can modify the template for the form row. Do this in the template that the form is rendered in:
{% form_theme form _self %}
{%- block form_row -%}
<div class="pure-control-group">
{{- form_label(form) -}}
{{- form_errors(form) -}}
{{- form_widget(form) -}}
</div>
{%- endblock form_row -%}

Related

Submit multiple forms created by foreach loop with one button

In my blade template, I use a foreach to create one form per iteration. Currently, each form has a submit button, but I want to use one submit button to submit all the form, because now I cannot submit all forms at the same time. How can I fix this?
#if(count($task_criterias) > 0)
#foreach($task_criterias as $task_criteria)
<td class="card">
<div>Description:{{$task_criteria->criteriadescription}}</div>
<div>Maximum Mark:{{$task_criteria->maximummark}}</div>
{!! Form::open([
'action' => 'CriteriaMarksController#store',
'method' => 'POST',
'name' => "form"
]) !!}
<div class="form-group" hidden >
{{ Form::label('criteria_id', 'criteria_id') }}
{{ Form::text('criteria_id', $task_criteria->id, ['class'=>'form-control']) }}
</div>
<div>
{{ Form::label('selfmark','Mark ') }}
{{ Form::number('selfmark', '',['placeholder'=>'', 'class' => 'col-lg-3 control-label']) }}
</div>
{!! Form::close() !!}
</td>
#endforeach
<input type="button" class="btn btn-info " value="Submit" onclick="submitForms()" />
<script>
submitForms = function() {
$("form").each(function(){
$.ajax({
method:'POST',
url:'/criteria_marks/post',
data: $(this).serialize(),
success: function(r){
//...
}
});
});
}
</script>
#endif
There can be only one post submit per request. One of the easiest solution is to perfom multiple requests using ajax:
<script>
submitForms = function() {
$("form").each(function(){
$.ajax({
method:'POST',
url:'* route to CriteriaMarksController#store *',
data: $(this).serialize(),
success: function(r){
//...
}
});
});
}
</script>
Or you can make one big form:
{!! Form::open([
'action' => 'CriteriaMarksController#store',
'method' => 'POST',
'name' => "form"
]) !!}
#foreach($task_criterias as $key => $task_criteria)
<td class="card">
<div>Description:{{$task_criteria->criteriadescription}}</div>
<div>Maximum Mark:{{$task_criteria->maximummark}}</div>
<div>
{{ Form::label('selfmark','Mark ') }}
{{ Form::number('selfmark['.$task_criteria->id.']', '',
['placeholder'=>'', 'class' => 'col-lg-3 control-label']) }}
</div>
</td>
#endforeach
<input type="submit" class="btn btn-info " value="Submit" />
{!! Form::close() !!}
This will form associative array selfmark in post with id => value pairs
Not sure how to do it with the Form facade.
What you have now is:
<input name="criteria_id" value="{{ $task_criteria->id }}" />
<input name="selfmark" value="{{ $task_criteria->id }}" />
But what you need to have is:
<input name="criteria_id[]" value="{{ $task_criteria->id }}" />
<input name="selfmark[]" />
Note the [] in the input names.

Keep the option selected in search form - Laravel

I have a search form that searches for responsible, date and device name. When someone searches for a resposible with a date, I want that search data to be maintained in the result
The entire search form
{{ Form::open(['route' => 'reviews.index', 'method' => 'GET', 'class' => 'form-inline pull-right']) }}
#csrf
{{ Form::text('device_name', null, ['class' => 'form-control', 'placeholder' => 'Nombre Equipo'])}}
<select name="name">
<option></option>
<option>OK</option>
<option>NOK</option>
</select>
{{ Form::date('created_at', null, ['class' => 'form-control', 'placeholder' => 'Creacion'])}}
<select name="responsable">
<option ></option>
#foreach($users as $user)
<option>{!! $user->name !!}</option>
#endforeach
</select>
<button style="border: none;padding: 4px 20px;background-color: #d61628;color: white">Buscar</button>
<button style="border: none;padding: 4px 20px;background-color: #d61628;color: white">Reset</button>
{{ Form::close() }}
And the index method in Controller
public function index(Request $request)
{
$responsable = $request->get('responsable');
$created_at = $request->get('created_at');
$device_name = $request->get('device_name');
$name = $request->get('name');
$devices = Device::all();
$reviews = Review::orderBy('id', 'DESC')
->responsable($responsable)
->created_at($created_at)
->device_name($device_name)
->name($name)
->paginate(30);
$users = User::all();
return view('reviews.index', compact('devices', 'reviews', 'users'));
}
Thanks
You may use old() function for re-populate the form with the last submitted data.
See https://laravel.com/docs/5.6/requests#old-input
So, in your blade code it would be something like this:
{{ Form::text('device_name', old('device_name'), ['class' => 'form-control', 'placeholder' => 'Nombre Equipo'])}}
//...
{{ Form::date('created_at', old('created_at'), ['class' => 'form-control', 'placeholder' => 'Creacion'])}}
For select, it's different, basically you compare each rendered option in the loop and if it's the one submitted you mark it as selected with html.
<select name="responsable">
<option></option>
#foreach($users as $user)
<option value="{{ $user->name }}" {{ old('responsable') == $user->name ? 'selected' : '' }}>{!! $user->name !!}</option>
#endforeach
</select>
Also, consider that old() also accepts a second parameter for default value in case you don't want it to be just null.

how to store drop-down list select into database?

I have a form for creating projects. It contains 2 foreign keys domain and owner (domain_id and owner_id). in fact these are 2 drop-down lists. when i try to submit the form, and check my database, I found out that owner and domain have NULL as value, eventhough i selected values from the drop-down list.
this is projectController :
public function create()
{
$domains = Domain::all('nameDomaine', 'id');
$owners = Owner::all('nameOwner', 'id');
return view('projectss.create', compact('domaines', 'owners'));
}
public function store(Request $request)
{
$domain_id = Domain::all()->pluck('nameDomain', 'id');
$owner_id = Owner::all()->pluck('nameOwner', 'id');
$this->validate($request, [
'title' => 'required',
'code' => 'required',
'domain_id' => 'required',
'owner_id' => 'required',
]);
Project::create($request->all());
return redirect()->route('projects.index')
->with('success', 'Project created successfully');
}
and this is create.blade.php :
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong> Domain : </strong>
<select class="form-control" name="domain_id">
#if (!count($domains) > 0)
<strong> Whoops! Something went wrong </strong>
#else
#foreach($domains as $id => $domain)
<option value="{{ $id }}">{{ $domain->domain }}</option>
#endforeach
#endif
</select>
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong> owner : </strong>
<select class="form-control" name="owner_id">
#if (!count($owners) > 0)
<strong> Whoops! Something went wrong </strong>
#else
#foreach($owners as $id => $owner)
<option value="{{ $id }}">{{ $owner->nameOwner }}</option>
#endforeach
#endif
</select>
</div>
</div>
I read a lot of posts about this problem, but none of them is working for me.
You can try this
in controller's store function
$domain_id = Domain::all('nameDomain', 'id');
$owner_id = Owner::all('nameOwner', 'id');
In you view
<select class="form-control" name="domain_id">
#if (!count($domains) > 0)
<strong> Whoops! Something went wrong </strong>
#else
#foreach($domains as $id => $domain)
<option value="{{ $domain->id }}">{{ $domain->domain }}</option>
#endforeach
#endif
</select>
and for second select option
<select class="form-control" name="owner_id">
#if (!count($owners) > 0)
<strong> Whoops! Something went wrong </strong>
#else
#foreach($owners as $id => $owner)
<option value="{{ $owner->id }}">{{ $owner->nameOwner }}</option>
#endforeach
#endif
You was not fetching id of your object. The $id you were fetching will just give you the index.
This should work :)
Change
#foreach($owners as $id => $owner)
<option value="{{ $id }}">{{ $owner->nameOwner }}</option>
#endforeach
to
#foreach($owners as $owner)
<option value={{ $owner->id }}>{{ $owner->nameOwner}</option>
#endforeach
And repeat with the other foreach loop.
If that doesn't work, in your controller add dd($request); to see what info is being passed to it and share with us, please.
Seems close but a few tweaks are needed. Also check your Project model and see if there is a $fillable array. If there is you'll need all the fields in your validate call in it e.g.
protected $fillable= [
'title'
'code'
'domain_id'
'owner_id'
];
Remove the calls in your store function.
public function create()
{
$domains = Domain::all('nameDomaine', 'id');
$owners = Owner::all('nameOwner', 'id');
return view('projectss.create', compact('domaines', 'owners'));
}
public function store(Request $request)
{
$this->validate($request, [
'title' => 'required',
'code' => 'required',
'domain_id' => 'required',
'owner_id' => 'required',
]);
Project::create($request->all());
return redirect()->route('projects.index')
->with('success', 'Project created successfully');
}
Make sure you're adding the right ID.
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong> Domain : </strong>
<select class="form-control" name="domain_id">
#if (!count($domains) > 0)
<strong> Whoops! Something went wrong </strong>
#else
#foreach($domains as $domain)
<option value="{{ $domain->id }}">{{ $domain->domain }}</option>
#endforeach
#endif
</select>
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong> owner : </strong>
<select class="form-control" name="owner_id">
#if (!count($owners) > 0)
<strong> Whoops! Something went wrong </strong>
#else
#foreach($owners as $owner)
<option value="{{ $owner->id }}">{{ $owner->nameOwner }}</option>
#endforeach
#endif
</select>
</div>
</div>

Blade Templating Input Type Value

In Html forms i can do this as like :
<input type="hidden" name="token" value="{{ $token }}">
I would like to do this in full blade templating like this :
{{ Form::hidden('token', $token, array('class' => 'form-control')) }}
But the second option doesn't pass the $token value. Where do i go wrong ?
-- UPDATE --
I have found the solution for this :
It is quite easy question, but had to be careful while naming the inputs in blade templating.
This is my first form, which is working nice :
<form action="{{ action('RemindersController#postReset') }}" method="POST">
<input type="hidden" name="token" value="{{ $token }}">
Email<input type="email" name="email">
Password<input type="password" name="password">
Password<input type="password" name="password_confirmation">
<input type="submit" value="Reset Password">
</form>
**In blade templating form it was like **
{{ Form::open(array('action' => 'RemindersController#postReset')) }}
{{ Form::hidden('token', $token , array('class' => 'form-control')) }}
{{ Form::email('email', null, array('class'=>'form-control input-sm','placeholder'=>'Mail address')) }}
{{ Form::password('pass', array('class'=>'form-control input-sm','placeholder'=>'New Password')) }}
{{ Form::password('pass_conf', array('class'=>'form-control input-sm','placeholder'=>'Confirm Password')) }}
{{ Form::submit('Submit', array('class'=>'btn btn-info btn-block')) }}
{{ Form::close() }}
--SOLUTION--
So the error is :
In 1st form,
Password<input type="password" name="password">
Password<input type="password" name="password_confirmation">
In 2nd form,
{{ Form::password('pass', array('class'=>'form-control
input-sm','placeholder'=>'New Password')) }}
{{Form::password('pass_conf', array('class'=>'form-control
input-sm','placeholder'=>'Confirm Password')) }}
input names are password and password_confirmation, and in the second form they are named pass and pass_conf.
I changed pass to password and pass_conf to password_confirmation and it's now working fine.
Thank you for your feedbacks and patience.
It's just a guess but you can try to put also static value:
{{ Form::hidden('token', 'some token here', array('class' => 'form-control')) }}
and now check if it is in page source.
If it's not it means that you probably redirect to form using withInput and then Larravel fills the form with the same data as previous even if you set value manually to any HTML form element.
In this case you could make redirection using:
->withInput(Input::except('token'));
If it's the case you can read more about it in this topic: Laravel redirection using withInput doesn't allow to change inputs values
EDIT
You should try to change:
return Redirect::back()->with('error', Lang::get($response));
into:
return Redirect::back()->with(array ('error', 'token'), array(Lang::get($response), Input::get('token'));

LARAVEL styled password field with bootstrap

in this form i heve Form::password() and i can not styled or embeded class for that. for example this below class and style for Form::text can work correctly.
{{ Form::text('username', Input::old('username'), array('placeholder'=>'UserName', 'class'=>'form-control' ) ) }}
Generated HTML:
<input id="username" class="form-control" type="text" name="username" placeholder="username">
but for Form::password() do not work:
{{ Form::password('password', null, array('placeholder'=>'Password', 'class'=>'form-control' ) ) }}
Generated HTML:
<input id="password" type="password" value="" name="password">
This will work:
{{ Form::password('password', array('placeholder'=>'Password', 'class'=>'form-control' ) ) }}
You don't need the null as you cannot specify a default value for password fields.
You can also use a custom HTML macro that enables the syntax similar to your first example.
See my answer here:
How to pass value to Password field in Laravel
This problem occurred when the null value in the password.
{!! Form::password('password', old('password'), ['class' => 'form-control ', 'placeholder' => 'Enter Password']) !!}
Please Remove the old('password'):
{!! Form::password('password', ['class' => 'form-control', 'placeholder' => 'Enter Password']) !!}