I want to set check-boxes state from database, so I write,
{{ Form::checkbox('asap', null, $offer->asap) }}
But if I want to set 'id' to the check-box like
{{ Form::checkbox('asap', null, $offer->ASAP, array('id'=>'asap')) }}
It always set my check-box state to true. (Before user select it)
So question how set 'id' in blade check-boxes when check-box state is set before user select it?
I know this question was answered before, in this one I am going to explain step by step how to implement checkboxes with Laravel/blade with different cases ...
So in order to load your checkboxes from the database or to test if a checkbox is checked :
First of all you need to understand how it works, as #itachi mentioned :
{{ Form::checkbox( 1st argument, 2nd argument, 3rd argument, 4th
argument ) }}
First argument : name
Second argument : value
Third argument : checked or not checked this takes: true or
false
Fourth argument : additional attributes (e.g., checkbox css classe)
Example :
{{ Form::checkbox('admin') }}
//will produces the following HTML
<input name="admin" type="checkbox" value="1">
{{ Form::checkbox('admin', 'yes', true) }}
//will produces the following HTML
<input checked="checked" name="admin" type="checkbox" value="yes">
How to get checkboxes values ? ( in your controller )
Methode 1 :
public function store(UserCreateRequest $request)
{
$my_checkbox_value = $request['admin'];
if($my_checkbox_value === 'yes')
//checked
else
//unchecked
...
}
Methode 2 :
if (Input::get('admin') === 'yes') {
// checked
} else {
// unchecked
}
Note : you need to assign a default value for unchecked box :
if(!$request->has('admin'))
{
$request->merge(['admin' => 0]);
}
this is nice right ? but how could we set checked boxes in our view ?
For good practice I suggest using Form::model when you create your
form this will automatic fill input values that have the same names as
the model (as well as using different blade Form:: inputs ..)
{!! Form::model( $user, ['route' => ['user.update', $user->id], 'method' => 'put' ]) !!}
{!! Form::checkbox('admin', 1, null) !!}
{!! Form::close() !!}
You can also get it like this :
{{ Form::checkbox('admin',null, $user->admin) }}
Okey now how to deal with :
multiples checkboxes
Add css classe
Add checkbox id
Add label
let's say we want to get working days from our database
$working_days = array( 0 => 'Mon', 1 => 'Tue', 2 => 'Wed',
3 => 'Thu', 4 => 'Fri', 5 => 'Sat', 6 => 'Sun' );
#foreach ( $working_days as $i => $working_day )
{!! Form::checkbox( 'working_days[]',
$working_day,
!in_array($working_days[$i],$saved_working_days),
['class' => 'md-check', 'id' => $working_day]
) !!}
{!! Form::label($working_day, $working_day) !!}
#endforeach
//$saved_working_days is an array of days (have 7 days, checked & unchecked)
I've spent sometime to figure out how to deal with multiple checkboxes I hope this can help someone :)
3rd argument decides whether checkbox is checked or not. So probably $offer->ASAP (or $offer->asap is true (or not false or not null). If you want to to make checkbox unchecked either set it to false, or don't use 3rd argument (set to to null or false):
{{ Form::checkbox('asap',null,null, array('id'=>'asap')) }}
EDIT
Another possibility is that you have on your page some custom JavaScript code that finds element by asap id and checks this checkbox. So when you don't set id, JavaScript cannot check it, but when you set this id, the checkbox will be checked by JavaScript.
in FormBuilder.php
public function checkbox($name, $value = 1, $checked = null, $options = array())
{
return $this->checkable('checkbox', $name, $value, $checked, $options);
}
1st param: name
2nd : value
3rd : checked or not (i.e. null, false or true)
4th : attributes.
{{ Form::checkbox('asap',null,$offer->ASAP, array('id'=>'asap')) }}
your order is wrong.
it should be,
{{ Form::checkbox('asap',$offer->ASAP, null, array('id'=>'asap')) }}
Good luck!
<div class="togglebutton">
<label>
#if ($programmer->play === 1)
<input type="checkbox" name="play" checked="">
#else
<input type="checkbox" name="play" {{ old('play') ? 'checked' : '' }} >
#endif
Play
</label>
</div>
Related
I have this dropdown i want to make it read only or disable
{{ Form::select('name[]',$names,$name : '' ,['class'=>'form-control name input_fields','readonly'=>true,'id'=>'name']) }}
tried disabled also
{{ Form::select('name[]',$names,$name : '' ,['class'=>'form-control name input_fields','disabled'=>true,'id'=>'name']) }}
but i want to disable options only the option that is pre-selected should be saved in database, using readonly i can change the dropdown and using disable i can't get its value.
This worked for me
# assume "$names" as your data array, which comes from the backend
#php
$names = array('L' => 'Large', 'S' => 'Small')
#endphp
{{ Form::select('name[]',$names, '' ,['class'=>'form-control name input_fields','disabled'=>true,'id'=>'name']) }}
In form remove "$names,$name : ''" and run only with $names array
You have to use a hidden input
Example based on your code ..
{{ Form::select('name_placeholder', $names, $name , ['class'=>'form-control name input_fields', 'readonly'=>true,' id'=>'name']) }}
{{ Form::hidden('name', $name) }}
i have a problem where a checked checkbox is not displaying checked.
My view:
#if(in_array($searchgroup->id.','.$searchtag->id, session()->get('zoektermen')))
{{ Form::checkbox($searchtag->naam,$searchgroup->id. ',' .$searchtag->id, true, ['class' => 'search-checkbox']) }} {{ $searchtag->naam }}
#else
{{ Form::checkbox($searchtag->naam,$searchgroup->id. ',' .$searchtag->id, null,['class' => 'search-checkbox']) }} {{ $searchtag->naam }}
#endif
I'm using laravel collective.
I'm using this code on other places on the website in different scenario's, where this works perfectly and the checkboxes display checked.
I'm using firefox but i also tried safari, no difference.
Any help is appreciated..
Consider the example having checked checkbox or not depending on the say user status Active or Inactive
{!! Form::checkbox('status', 'Active', ($userData->status == 'Active') ? 'checked="checked"' : '' ,['data-prompt-position' => 'centerRight','data-title'=>'Active']) !!}
If you have included "uniform.default.js" in your project, it can create such an issue. Please use the following code to resolve it.
$('#checkbox').prop('checked',true).uniform('refresh');
Further reading: jQuery Uniform Checkbox does not (un)check
Thanks.
I have a multi-page form with two radio buttons with the same name attribute. When I select one and click the next step button, I save the value of that radio button into a session array with the form field name and chosen value. If the user comes back to the page, I want the previously chosen radiobutton to be checked.
This is what I came up with:
View: choose-listing-type.blade.php
<div class="form-group">
<?php $checked_status = Session::get('listing_form_data.type') === 'property' ? true : false; ?>
{{ Form::radio('type', 'property', $checked_status) }} Create Property Listing
</div>
<div class="form-group">
<?php $checked_status = Session::get('listing_form_data.type') === 'room' ? true : false; ?>
{{ Form::radio('type', 'room', $checked_status) }} Create Room Listing
</div>
This works, but it seems sloppy. First off, I don't think the if statement that checks the session value should be in the view, and I would love to find a way to do this in blade.
Using Laravel 4, what is the best practice to mark a radiobutton as checked depending on the value of a specified session key?
Since you mentioned you wanted to do it in the controller:
$type = Session::get('listing_form_data.type');
return View::make('view')->with('type', $type);
View:
{{ Form::radio('type', 'property', $type === 'property') }} Create Property Listing
{{ Form::radio('type', 'room', $type === 'room') }} Create Room Listing
Or even:
$type = Session::get('listing_form_data.type');
$isProperty = ($type === 'property');
$isRoom = ($type === 'room');
return View::make('view')->with(compact('isProperty', 'isRoom'));
View:
{{ Form::radio('type', 'property', $isProperty) }} Create Property Listing
{{ Form::radio('type', 'room', $isRoom) }} Create Room Listing
Why don't you just put the conditional right inline with the Form helper, like this:
<div class="form-group">
{{ Form::radio('type', 'room', (Session::get('listing_form_data.type') === 'room') ? true : false) }} Create Room Listing
</div>
Personally I don't see anything wrong with checking a session setting from the view...
I have a edit form with some checkboxes that I am trying to make checked when the associated many to many relationship is established.
Distributors belong to many beers
Beers belong to many distributors
in my controller I have:
$breweries = Brewery::lists('name', 'id');
$all_dist = Distributor::all();
$beer = Beer::find($id);
$distributions = [];
foreach ($beer->distributors as $distributor)
{
$distributions[$distributor->id] = BeerDistribution::where('beer_id', '=', $beer->id)
->where('distributor_id', '=', $distributor->id)->first()->price;
}
return View::make('beers.edit', ['beer' => $beer, 'distributors' => $all_dist, 'distributions' => $distributions, 'breweries' => $breweries, 'styles' => $styles]);
and I in my edit form I have:
{{ Form::model($beer, ['route' => ['beers.update', $beer->id], 'method' => 'PATCH']) }}
#foreach ($distributors as $distributor)
<?php $carried = in_array($distributor->id, array_keys($distributions)) ? true : false ?>
{{ Form::checkbox('distributors[]', $distributor->id, $carried); }}
{{ Form::label($distributor->name) }}
{{ Form::label('price' . $distributor->id, 'Retail:') }}
<?php $price = $carried ? $distributions[$distributor->id] : null ?>
{{ Form::text('price' . $distributor->id, $price ) }}
#endforeach
{{ Form::submit('Save') }}
{{ Form::close() }}
Basically I am passing an associated array of each distributor_id => price. This array also tells me which distributors the beer already belongs to so that I can mark those checked off in my edit form.
Here's where things get wonky. When I load this form, all the checkboxes will be checked no matter what. If I change my controller loop to this:
foreach ($beer->distributors()->lists('distributor_id') as $distributor_id)
Then I can do create my array.
Why does calling $beer->distributors in the controller would result in all the checkboxes being checked?
The problem is with the last argument : $carried
{{ Form::checkbox('distributors[]', $distributor->id, [ "checked" => $carried ]); }}
Pass the last parameter as array, and tell it exactly which attribute to modify and the attribute value.
I'm using the laravel framework for a project, and I'm implementing a basic form page, where I require certain values to be required, something that can be done very easily in HTML5.
<input type="text" name="abc" required>
In laravel, without the required attribute, the same would be :
{{ Form::text('abc') }}
How do I incorporate a required attribute in the above statement?
Since simply writing ['required'] did not work, I searched online a bit more and found the answer, so I thought I'd share it here.
The third parameter is an array of optional attributes, which, in convention, must be written as:
{{ Form::text('abc','',array('required' => 'required')) }}
Similarly, for a radio button with default selected/checked we have:
{{ Form::radio('abc', 'yes', array('checked' => 'checked')) }}
Check out the API-Docs. The method signature shows that you can provide 3 parameters.
The first one is the name attribute, the second one is the value attribute. Third one is your array with any additional attributes.
So just call your method with:
{{ Form::text('key', 'value', ['required']) }}
And a required attribute will be attached to your input field.
I believe the correct answer is similar to the other post where the third parameter is
array('required' => 'required')
however to get the attribute without any value you can do the following:
array('required' => '')
The input field (for text example), will then look what was necessary in the question.
Laravel Example:
{{ Form::text('title', '', array('tabindex' => '1', 'required' => '')) }}
HTML output:
<input tabindex="1" required name="title" type="text" value="" id="title">
I believe this actually shorthand for required='', just wanted to add this note
Radio required featured works with laravel 5.7 version
#foreach($status_list as $status_key => $status)
{!! Form::radio('status', $status_key, false, array('id'=>'status_'.$status_key, 'required'=>'required' )); !!}
{!! Form::label('status_'.$status_key, $status ) !!}
#endforeach
I hope, this will help you also. :)