How to set form input required attribute in laravel 4 - php

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

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

Laravel 5 binding value in bootstrap-slider in edit/Patch

I'm implementing bootstrap-slider in my CRUD, I have implemented it successfully in Create, the problem is when I try to edit it,
I want to get the current value from the Model. Idk how to do this.
This is for PATCH.
<div class="form-group">
<h3 class='box-title text-info'>Percentage</h3>
{!! Form::input('text','percentage',null,['id'=>'ex8', 'data-slider-id'=>'ex1Slider', 'data-slider-min'=>'0', 'data-slider-max'=>'100', 'data-slider-step'=>'5', 'data-slider-value'=>'50']) !!}
</div>
In your form instead of creating a new form. You will bind the form to the model.
{!! Form::model('modelname', [options here] !!}
All the fields will math the model's property values.
Edit
Here is an example
You must used something like this to create a EDIT FORM
{{ Form::model($smartphones, ['method' => 'PATCH', 'url' => 'smartphones/'.$smartphones->id]) }}
You get by using $(your_model)['inputID']...You can use in "data-slider-value"... Something like this
{{ Form::input('text','mem_ram', null, ['id' => 'mem_ram', 'data-slider-value'=>$smartphones['mem_ram']]) }}

Old inputs on Laravel FormBuilder keeps appearing even though i set new values

I got a weird behaviour with the 'old value' feature from FormBuilder in Laravel 5.1.
On my blade template I use a simple text input:
{!! Form::open([ 'url' => route('album-photo'), 'method' => 'POST' ]) !!}
{!! Form::text('album_id', $album_id) !!}
{!! Form::close() !!}
On my controller I show the view:
$album_id = 1;
return view( $name_view, compact( 'album_id' ) );
The problem is that, even though I change the album_id value on my controller, the form builder on my view will keep printing the first value.
While if I use the normal html tag:
<input type="text" name="album_id" value="{{ $album_id }}" />
it will print the updated value.
So it looks like the FormBuilder class is getting the value from the old session and it can't manage the refreshing of the value.
Is there a way to fix it and keep using the FormBuilder methods instead of plain html?

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>

Add Class to select element on Laravel

I have this problem, I can't find the way to add class attribute in this Dropdown box
{{Form::select('bancada', Bancada::lists('nombre','idBancada'))}}
I have tried various syntax, but can not get it to work.
Any suggestions? Thanks
Use the forth parameter to add attributes to your element.
{{Form::select('bancada', Bancada::lists('nombre','idBancada'), null, ['class' => 'my_class'])}}
Take a look at the source ($options is the same as attributes for your element).
{{ Form::select('status', $statusList, null, array('class' => 'form-control')) }}
In above example you can add class as fourth parameter

Categories