I need to keep the checked checkboxes in the form as checked after I get the validation errors in the Controller.
Actually I find quite horrendous my implementation of the checkboxes in my form.
Could you please provide some advise? Thanks
<div class="form-group">
{!! Form::label('call', 'Llamar:', ['class' => 'control-label']) !!}
{!! Form::checkbox('call', isset($category)?(bool)$category->call:true, ['class' => 'form-control']) !!}
</div>
For the checkbox part, I did not use Laravel Forms. I used normal html "input" tag instead, and used "old" to check for previous value
<input type="checkbox" name="call" value="1" {{(old('call') == "1") ? 'checked': ''}}>Call
Try using old to check the previous value and set the value to checked or not based on that.
You can do it via the form builder much easier like this:
{!! Form::checkbox('call', 1, false, ['class' => 'form-control']) !!}
call = your checkbox name
1 = your checkbox value if it's checked (change to whatever you want)
false = don't load it checked on first page load (setting true will load it always checked)
['class' => 'form-control'] = it will add the class to it
That's all.
You don't need to worry about any kind of values, it will be set automatically.
Related
I am trying to make a select box that return value from $collection, but it gives me index of the selection instead of value.
screenshot:output of my code
screenshot:browser source code view
<div class="form-group">
{!! Form::label('Supervisor') !!}<br />
{!! Form::select('supervisor_id', $lecturers, null,
array('placeholder' => '----------------','class' => 'form-control')) !!}
</div>
and when it's show in browser source code view:
<option value="">----------------</option>
<option value="0">1111111111</option>
<option value="1">1212121212</option>
Anyone who know the syntax of laravel form to make this dynamic form gives value of $lecturers please help.
Ok here we go.
First in your controller, when you are getting the lecturers list, make sure you pluck just the name and the id...in this order:
$lecturers = Lecturer::pluck('name', 'id');
Then in your view:
{{Form::select('lecturer_id', $lecturers, null, ['class' => 'form-control'])}}
Thats all.
The array of lecturers, that you plucked will be divided into "id" and "values", hence the order of plucking is important.
Now, in your form source, you will see that the option value will be lecturers ids like "1, 2, 3" etc while the "selectable" options shown to users will be the names of the lecturers.
Like this:
<select class="form-control" name="lecturers_id"><option value="1">John Doe</option><option value="2">Jane Doe</option></select>
Try these and lets know your result.
As in title how to do that? I have table comments, the structure of this table is:
-id
-title
-contents
-user_id
Here is code of my form:
{!! Form::open(['action' => ['LibraryController#newcomment', $sth->myuserid]]) !!}
{!! Form::label('title', 'Title:') !!}
{!! Form::text('title', null) !!}
{!! Form::label('contents', 'Contents:') !!}
{!! Form::textarea('contents', null) !!}
{!! Form::submit('Send') !!}
{!! Form::close() !!}
So, comment has the user_id and I have to send it too with form, maybee by this:$sth->myuserid but how can I use it in LibraryController?
Additionally, I found this: http://laravel-recipes.com/recipes/157/creating-a-hidden-input-field is it a good option?
You could use a hidden field to store the user id and then retrieve it in your controller:
{{ Form::hidden('user_id', $user_id) }}
But if you are using Auth component you can get the authenticated user by using the Auth facade:
use Illuminate\Support\Facades\Auth;
...
// Get the currently authenticated user's ID...
$id = Auth::id();
If it's for admin panel or something, you can use hidden input:
{!! Form::hidden('user_id', $user->id) !!}
But if you want to pass an ID of a current user, do not pass it through a form. Use auth()->id() in a controller to get ID.
Okay, but what if it isn't auth but something else and I want use it:['LibraryController#sth', $sth->sth_id], how to use it in LibraryController ?
I have a list of users, and for each user of the list there is a select box where the selected value role value is shown.
I was thinking a way on how allow to send all the modifications for each user with the submit button to a method of the controller which should update the role. I have no idea.
This is the form, which works correctly:
{!!Form::open(['url' => ['admin/users/edit'], 'class' => 'form-horizontal', 'method' => 'POST']) !!}
#foreach($users as $user)
<div class="form-group">
<div class="list-user-element">{!! $user->name !!}</div>
<div class="list-user-email">{!! $user->email !!}</div>
<div>
{!! Form::select('role', $roles, $user->role->label, ['class' => 'form-control']); !!}
</div>
</div>
#endforeach
<div class="form-group">
<div class="col-sm-offset-2 col-sm-5">
{!! Form::submit('Update', array('class' => 'btn btn-warning')) !!}
</div>
</div>
{!! Form::close() !!}
I don't know how to prepare the controller, neither which kind of info is passed to the controller.
Route::post('admin/users/edit', 'AdminController#showRegisteredUsersRoles');
And here it gives error:
public function editRegisteredUsersRoles(Request $request)
{
dd($request);
}
You could do the form like this
{!! Form::select('users[' . $user->id . '].role', $roles, $user->role->label, ['class' => 'form-control']); !!}
And then in the controller
public function editRegisteredUsersRoles(Request $request)
{
foreach ($request->users as $user_id => $role) {
//...
}
}
I'm not 100% sure of what your problem is, are you saying that there aren't all of the users' roles in the Request when you dd($request) ?
I think the problem lies in the fact that you are looping through the users throwing down select boxes with the same name. Generally, when you send a POST request, you access the information by saying $_POST[<Form's name>], so maybe there is a problem with duplication here. Perhaps you should try something like:
{!! Form::select('role' . $user->id, $roles, $user->role->label, ['class' => 'form-control']); !!}
What I changed above is simply tacking on the user's id to the name 'role' in order to make each of those unique.
I have a multi-step form in laravel where I pass the session variables from one view to the other and everything works ok.
This is part of my form
<div class="panel-body">
{!! Form::open(array('url' => '/post-quote')) !!}
<div class="form-group">
{!! Form::Label('brand', 'Brand:') !!}
<select class="form-control" name="brand" id="brand">
<option value="{{Input::old('brand')}}" selected="selected">{{Input::old('brand', 'Please select a Brand')}}</option>
#foreach($brands as $brand)
<option value="{{$brand->id}}">{{$brand->brand}}</option>
#endforeach
</select>
</div>
Here is part of my controller
public function quote()
{
$brands = Brand::orderBy('brand')->get();
return View::make('frontend.step1', compact('brands'));
}
The issue I got is that I am passing the id for each option value in the select and after validation, the Input::old populates the id value and it doesn't look right to display a number instead of the brand name.
Question: is there anyway to get input::old with the {{$brand->brand}} instead of the id?
My other option is to pass as a value for each option the brand name, however I dont know how to get the id for my input in the controller.
I hope you get what I am trying to achieve.
Thanks
Did you try passing the brands as an array? Like,
$brands = Brand::orderBy('brand')->lists('brand','id');
return View::make('frontend.step1', compact('brands'));
Then in your view you can use the Form::select instead of creating the select manually. Something like this:
{!! Form::select('brand',$brands, Input::old('brand'),['class' => 'form-control']) !!}
Give it a try and let me know if it works, there are some others things you can try, like using Form::model instead of Form::open (I would go with this approach)
you can pass data to next user request by using flash method like this
$request->flash()
then retrieve it in blade template with global old helper function which gives you old input data,
{{ old('brand') }}
$brands = Brand::orderBy('brand')->lists('brand','id');
return View::make('frontend.step1', compact('brands'));
Then in your view you can use the Form::select instead of creating the select manually. Something like this:
{!! Form::select('brand',$brands, Input::old('brand'),['class' => 'form-control']) !!}
I have only seen how to open a Blade page with link, how do I use a button to do the same?
a button like this?
<input type="button"
onClick="parent.location='https://google.com'"
value="Google"
formtarget="_blank">
Using Form facade:
{!! Form::button('Google', [
'onClick' => "parent.location='https://google.com'",
'formtarget' => 'fromtarget'
])
!!}
And Using Form facade with url():
{!! Form::button('Delete account', [
'onClick' => "parent.location='" . url('delete/1') . "'",
'formtarget' => 'fromtarget'
])
!!}
You can do it with either a button or input element.
Here's an example with input:
<input type="submit" onClick="location.href = '{{ url('some/route') }}'">