Laravel: how to set checkbox to be checked on default? - laravel

How to set checkbox to be checked on default but use same form for create and edit ?
I have form :
{!! Form::model($product, ['route' => $formRoute, 'method' => $formMethod]) !!}
{{ Form::checkbox('is_active') }}
{!! Form::close() !!}
and I want is_active to be checked on create, but load from $model on edit, if I put :
{{ Form::checkbox('is_active', 1, true) }}
it is checked on create, but is also checked on edit even if in db is unchecked (false).
I am looking for best solution...

Try this:
{{ Form::checkbox('is_active', 1, isset($product) ? true : false) }}
We are assuming that you have a $product var in your edit form

Related

Cannot display values when editing form in laravel 5

There is no output in the form when clicking a value. But I can see the values when using the return function. Here is my code:
ReportController
public function edit($id)
{
$crime_edit = CrimeReport::findOrFail($id);
$victim_info = VictimProfile::findOrFail($id);
return $victim_info;
//return $victim_info->firstname;
//return $victim_info;
$display_crime_type = CrimeType::lists('crime_type','id');
$display_crime_name = CrimeName::lists('crime_description','id');
return view('crimereports.edit',compact('crime_edit','victim_info','display_crime_name,'display_crime_type'));
}
edit view page
{!! Form::model($crime_edit,['method' =>'PATCH','route'=>'crime_reports.update',$crime_edit->id],'class'=>'form-horizontal']) !!}
<div class="form-group">
{!! Form::label('victim_name', 'Victim Name',['class'=>'col-md-3 control-label']) !!}
<div class="col-md-3">
{!! Form::text('victim_name', null, ['class' => 'form-control','placeholder' => 'Firstname']) !!}
</div>
<div class="col-md-2">
{!! Form::text('v_middle_name', null, ['class' => 'form-control','placeholder' => 'Middlename']) !!}
</div>
<div class="col-md-3">
{!! Form::text('v_last_name', null, ['class' => 'form-control','placeholder' => 'Last Name']) !!}
</div>
</div>
{!! Form::close() !!}
Am I missing something?
The null is the default value. I think Form::model is suppose to bind the values, but have you tried removing the null?
I don't use Form::model, but as a default value I always put old('field_name', $model->field_name). The old function will look into both GET and POST and if a key matches that'll be shown. If that doesn't exist, it'll use the second value.
Based on your debug output statements ("return $victim_info;"), it looks like you are trying to bind the form to the $crime_edit model while accessing the values from the $victim_info model. You can not bind a form to two different models at once, so if the blank victim name fields are not attributes of the $crime_edit model, your current implementation will not work.
You will need to either explicitly add $victim_info->victim_name, etc. in place of the 'null' values, or bind the form to the $victim_info model instead of the $crime_edit model.
If victim_name, v_middle_name, and v_last_name are attributes of the $crime_edit model, then I have no idea.

proper usage of forms with routes in laravel 4.2

i have this form in view:
{{ Form::open(array('action' => 'StudentrecordController#viewSRS')) }}
<span><strong>Select School Year & Quarter</strong></span>
<div class="form-group">
{{ Form::select('sy', [null=> 'Select School Year'] + $schoolYearID , Input::old('modules'), array('class'=>'form-control') ) }}
</div>
<div class="form-group">
{{ Form::select('sq', [null=> 'Select Quarter'] + $schoolQuarterID , Input::old('modules'), array('class'=>'form-control') ) }}
</div>
{{ Form::submit('Sort', array('class' => 'btn btn-primary')) }}
{{ Form::close() }}
my route for this is
Route::get('sortsRec', 'StudentrecordController#viewSRS');
when i clicked the submit button it gives out a method not allowed exception.i think the form is sending out a post method but the route accepts get. how can i address this? any idea what i can do?
By default, a POST method will be assumed; however, you are free to specify another method:
{{ Form::open(['method' => 'get', 'action' => 'StudentrecordController#viewSRS']) }}
From the docs.

Combine Form::text and error check into one?

In the blade view file, I have something like this:
{{ Form::text('contact_name', null, ['class' => 'form-control']) }}
#if ($errors->has('contact_name'))
<div class="error-block">{{ $errors->first('contact_name') }}</div>
#endif
{{ Form::text('contact_email', null, ['class' => 'form-control']) }}
#if ($errors->has('contact_email'))
<div class="error-block">{{ $errors->first('contact_email') }}</div>
#endif
When user press submit, it will check inputs validation in the controller. However, if there is an error with the validation, it will then redirect back to a form and populate it with error messages {{ $errors->first() }}
Is there a way to exclude {{ $errors->first() }} in the view file and still show error messages if validation failed? So combine Form::text and $errors->hasinto one function or something like that?
Use a Form Macro to do this
Form::macro('myText', function($field)
{
$string = Form::text($field, null, ['class' => 'form-control']);
if ($errors->has($field)) {
$string .= $errors->first($field);
}
return $string;
});
Then in your view
{{ Form::myText('contact_email') }}

Laravel pre-filling multiple forms if validation failed

One of the coolest Laravel feature is, Laravel pre-filled the form fields if validation error occurred. However, if a page contain more than one form, and form fields have same name, Laravel pre-filling all forms fields.
For example:
I have a page where i have two forms to create new users or whatever.
<h1>Create user1</h2>
{{ Form::open(array('url' => 'foo/bar')) }}
{{ Form::text('name', null) }}
{{ Form::email('email', null) }}
{{ Form::close() }}
</h1>Create user2</h1>
{{ Form::open(array('url' => 'foo/bar')) }}
{{ Form::text('name', null) }}
{{ Form::email('email', null) }}
{{ Form::close() }}
Controller
class UsersController extends BaseController
{
public function store()
{
$rules = [
'name' => 'required',
'email' => 'required'
];
$validation = Validator::make(Input::all(), $rules);
if ($validation->fails()) {
return Redirect::back()->withInput()->withErrors($validation);
}
}
}
As i didn't fill up the email, Laravel will throw validation error and pre-filling the forms as following:
How to tell Laravel that do not fill-up the second form?
There's no Laravel way of doing this, but you can use HTML basic form arrays to make it work. You need to understand that you have to identify your forms and fields so Laravel knows exactly where the data came from and where to send it back to. If all your fields have the same name how could it possibly know?
This is a proof of concept that will work straight from your routes.php file.
As I did it all and tested here before posting the answer I used Route::get() and Route::post(), to not have to create a controller and a view just to test something I will not use. While developing this you will have to put this logic in a controller and in a view, where I think they are alredy in.
To test it the way it is, you just have to point your browser to the following routes:
http://yourserver/form
and when you push a button it will automatically POST tho the route:
http://yourserver/post
I'm basically giving all forms a number and giving the buttons the number that we will usin in Laravel to get the form data and validate it.
Route::get('form', function()
{
return Form::open(array('url' => URL::to('post'))).
Form::text('form[1][name]', null).
Form::email('form[1][email]', null).
'<button type="submit" name="button" value="1">submit</button>'.
Form::close().
Form::open(array('url' => URL::to('post'))).
Form::text('form[2][name]', null).
Form::email('form[2][email]', null).
'<button type="submit" name="button" value="2">submit</button>'.
Form::close();
});
And here we get the data, select the form and pass all of it to the validator:
Route::post('post', function()
{
$input = Input::all();
$rules = [
'name' => 'required',
'email' => 'required'
];
$validation = Validator::make($input['form'][$input['button']], $rules);
return Redirect::back()->withInput();
});
This is how you use it in a Blade view, now using 3 forms instead of 2 and you can have as many forms as you need:
<h1>Create user1</h2>
{{ Form::open(array('url' => URL::to('post'))) }}
{{ Form::text('form[1][name]', null) }}
{{ Form::email('form[1][email]', null) }}
<button type="submit" name="button" value="1">submit</button>
{{ Form::close() }}
</h1>Create user2</h1>
{{ Form::open(array('url' => URL::to('post'))) }}
{{ Form::text('form[2][name]', null) }}
{{ Form::email('form[2][email]', null) }}
<button type="submit" name="button" value="2">submit</button>
{{ Form::close() }}
</h1>Create user3</h1>
{{ Form::open(array('url' => URL::to('post'))) }}
{{ Form::text('form[3][name]', null) }}
{{ Form::email('form[3][email]', null) }}
<button type="submit" name="button" value="3">submit</button>
{{ Form::close() }}
And you can even use a loop to create 100 forms in blade:
#for ($i=1; $i <= 100; $i++)
User {{$i}}
{{ Form::open(array('url' => URL::to('post'))) }}
{{ Form::text("form[$i][name]", null) }}
{{ Form::email("form[$i][email]", null) }}
<button type="submit" name="button" value="{{$i}}">submit</button>
{{ Form::close() }}
#endfor
Use old input with $request->flash().
https://laravel.com/docs/5.2/requests#old-input

Laravel 4 blade drop-down list class attribute

Laravel blade drop down list class attribute not working.
I cannot find any reference to class or assigning attributes to select / drop-down lists in the documentation.
http://www.laravel.com/docs/html#drop-down-lists
Examples tried:
{{ Form::select('product_id', $productList, array('class'=>'form-control')) }}
{{ Form::select('product_id', $productList, $attributes = array('class'=>'form-control')) }}
Both return the same html but without the class attribute:
<select id="product_id" name="product_id">
... Option Stuff ...
</select>
{{ Form::select('product_id', $productList, null, array('class' => 'form-control')) }}
The third parameter is the key of the currently selected option. Defaults to null.
First get and create list in Controller for example:
$username_lists = Users::lists('username','id');
pass data to view by:
return View::make('layouts.customers')
->with('username_lists', $username_lists);
now get in view:
{{ Form::select('username_lists', $username_lists, null, array('class' => 'form-control')) }}

Resources