I have one really simple question. I have a form with three groups of dynamically generated check boxes. Let's say one of these groups of check boxes needs to have all check boxes as default set.
This is not a problem, the problem comes when I submit the form and I need to have the state of the checkbox.
Here is the HTML code I generate:
#for($i=0;$i<24;++$i)
{!! Form::checkbox('check_hour[]', $i, $check_hour[$i], ['class' => 'check_hour']) !!} {{($i+1)}}h
#endfor
and here is the code in the Controller:
for($i=0;$i<24;++$i) $check_hour[$i] = (isset(Input::get('check_hour')[$i])) ? true : false;
In this situation I have no problem to save the checkbox state, but I need all check boxes to bet set as default.
Related
I have a form that has a select input that called customer_id. This field is not required, and I fetch the options via the Customer Model. My code looks like this:
{!! Form::select('customer', $customers->lists('email', 'id'), null, ['class' => 'form-control']) !!}
so far so good, right? but the thing is that when I don't want to choose a customer, I can't choose a blank option. Is there a way to add a blank option so I wouldn't need to pick a customer all the time?
[''=>'---']+$customers->lists('email', 'id')->toArray()
or
array_merge([''=>'---'], $customers->lists('email', 'id')->toArray())
I'm new to laravel, in the view page I'm showing a Dropdown-List which is fetched from the database and based on that Dropdown-List value selection, I'm getting the corresponding value id. But I also want one more corresponding fields from database based on the Dropdown-List I'm selecting.
How to get it? Based on that values I want to do Javascript operation.
Here's my code:
{{
Form::select('asset_type_id',$assettype_list, Input::old('asset_type_id'),
array('class'=>'select2', 'onchange' => 'check(Input::get(this.value);)',
'style'=>'width:350px'))
}}
You cannot use Input::get(this.value) in your onchange as it will be printed literally. You can just use the code without the Input::get. Of course, this all depends on how you are using the value in your javascript.
{{
Form::select('asset_type_id',$assettype_list, Input::old('asset_type_id'),
array('class'=>'select2', 'onchange' => 'check(this.value)',
'style'=>'width:350px'))
}}
In Laravel, we can set the value for a form field element with this:
{{Form::text('name', 'predefined name')}}
The above work well, even in the edit mode the predefined name will always appear in the text field. However, for checkbox and radio button, it does not work.
{{Form::checkbox('country', 'UK',true)}}
{{Form::radio('gender', 'male',true)}}
{{Form::radio('gender', 'female',false)}}
No matter what I set for the checkbox & radio button, it will be ignored and use the data from database. This become an huge trouble for array of checkbox.
{{Form::checkbox('country[]', 'UK',true)}}
{{Form::checkbox('country[]', 'SG',false)}}
{{Form::checkbox('country[]', 'US',false)}}
How can I solve it? Thank you.
If you are referring to the visibility of the value of a checkbox or radio-array:
Usually you will add text to a checkbox or radio-element via a label.
{{ Label::for('nameOfInput', 'Value') }}
This is my html blade code
{{Form::checkbox('remember_me', '', array('id'=>'remember_id'))}}
<label for="remember_id">Remember me</label>
This is my controller code:
echo Input::get('remember_me');exit;
The result is always empty, why please?
The checkbox is always checked when I run the page, why please?
Thanks
Please have a look on the [parameter list of the Form::checkbox() method][1].
The second parameter is your checkbox value. You manually set it to an empty string. Set it to null in order to keep the browsers default values (laravel default is 1). The third parameter is a boolean. Set it to true to check the box and false to uncheck it.
The fourth parameter is your options array where you can specify your id. So the correct method call should be:
{{Form::checkbox('remember_me', null, false, array('id'=>'remember_id'))}}
Update:
Checkboxes that are not checked, will not be included in your POST data. So the only reliable way to verify that a checkbox has been checked is to check if it is set. That can be done using isset() with regular PHP functions, or if laravel is being used, by using Input::has() which returns a boolean dependent on whether your input data contains a given key.
You did not add a value to the checkbox
{{Form::checkbox('remember_me', 'value goes here', true, array('id'=>'remember_id'))}}
The second param is the value
Normally I write the checkbox without blade and I can do with it whatever I want, like normal HTML. I don't see why you can do it the normal HTML way, because it always ends up doing the same thing you expert.
{!! Form::label('Test-2') !!} {!! Form::checkbox('ch[]', 'value-2', false); !!}
I have a form where I show some items, which can be selected by the user.
There are over 1000 items so I'm using pagination.
What would be the best way to persist the checked items, when user go to the next page?
To store all these items in hidden fields wouldn't be an option, because they are to much items.
My View:
#foreach($articles['uncommitted'] as $article)
<tr>
<td><input type="checkbox" name="articles[]"></td>
<td>{{{$article->articlename}}}</td>
<td>{{{$article->category->catname}}}</td>
<td>{{{strftime('%d.%m.%Y %H:%M', strtotime($article->created_at))}}
<td>{{{$article->rrp}}}</td>>created_at))}}}</td>
</tr>
#endforeach
{{$links}}
This form will be paginated.
As far as I understand you are facing two problems: persisting the checked items across pagination request, and retrieving the checked items back to the view.
To persist the checked items across pagination request, I would flash the checked items into the Session. The controller method will look something as follows.
public function fill_form()
{
$items = Item::paginate(25);
// Retrieve checked items in session.
$checked_items = []
if (Session::has('checked_items'))
$checked_items = Session::get('checked_items');
// Persist new checked items.
$checked_items = array_merge($checked_items, Input::get('item'));
Session::flash('checked_items', $checked_items);
return View::make('form')
->with('items', $items);
}
As you can see the checked items will be available in the session within pagination requests.
Now for the case to display the checked items back to the view I would send the checked items in session to the view via old input. That said, the return value would be changed as follows.
public function fill_form()
{
# code intentionally omitted #
return View::make('form')
->with('items', $items)
->withInput($checked_items);
}
Then in your views the checked items will persist their checked value. Obviously, you should use Laravel to generate your checkboxes.
How to get all item (checked or not) on submit?
Maybe, if you are render items with checkboxes you will need to know which of those checkboxes were checked and which not when paginating. A simply solution will be adding an extra input hidden field for each checkbox with a default value, it will look as follows:
{{ Form::hidden('item1', 'off') }}
{{ Form::checkbox('item1', 'on') }}
{{ Form::hidden('item2', 'off') }}
{{ Form::checkbox('item2', 'on') }}
{{ Form::hidden('item3', 'off') }}
{{ Form::checkbox('item3', 'on') }}
After submitting the form, when paginating, for checked item you will receive the expected value, for those not checked you will receive the hidden value.
Note 1, it is important to put the hidden input before each checkbox.
Note 2, each hidden input should have the same name as the checkbox.
Unless I misunderstood your question, I think what you're looking for is caching: http://four.laravel.com/docs/cache
Here's an excerpt from the docs:
Database Cache
When using the database cache driver, you will need to setup a table to contain the cache items. Below is an example Schema declaration for the table:
Schema::create('cache', function($table)
{
$table->string('key')->unique();
$table->text('value');
$table->integer('expiration');
});
If you're wanting to store form data across requests, then you'll want to use Session - excerpt from the docs (http://four.laravel.com/docs/session):
Database Sessions
When using the database session driver, you will need to setup a table to contain the session items. Below is an example Schema declaration for the table:
Schema::create('sessions', function($table)
{
$table->string('id')->unique();
$table->text('payload');
$table->integer('last_activity');
});