Disable dropdown in Laravel Blade - php

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) }}

Related

Laravel Form Class without second argument

I simply want to give the text element of my edit-form a class.
{{ Form::text('first_name') }}
I know that i have to use an array with the class of the field as third argument. But i do NOT want to give a second argument. This one
{{ Form::text('first_name' , ' ' , array('class' => 'form-control')) }}
applys the class correctly, but sets the dafault value to an empty string. This is a problem, because now laravels auto-complete function doesn't fill the form with the correct data from the database. Is there a way to give no second argument or setting it to default like this?
{{ Form::text('first_name' , default , array('class' => 'form-control')) }}
Thanks!
Okay, i solved it myself. It is as easy as it always is. Simply use
{{ Form::text('first_name' , null , array('class' => 'form-control')) }}

A checked checkbox is not displaying checked? Laravel

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.

perplexing check box behavior

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.

Laravel blade check box

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>

How to input a variable value in field of name or id using blade template in laravel?

I need to input text name and id dynamically and I am trying as below
{{ Form::text('practice', $practice->name, array('class' => 'large-2','id'=<?php echo $inputTextName;?>)) }}
But it is not parsing PHP here. Even I tried 'id'={{$inputTextName}} but it is giving error.
Can you try this,
{{ Form::text('practice', $practice->name, array('class' => 'large-2','id'=>$inputTextName)) }}

Categories