How to show multiple selected values of selectbox in Laravel 5.3? - laravel-5

I want to show multiple selected values in laravel5.3 form
{!! Form::select('category[]', $categories['all_cat']['categories'], null,['multiple' => 'multiple'], ['class' => 'form-control', 'id' => 'category']) !!}

Try with this:
{!! Form::select('category[]', $categories['all_cat']['categories'], null, ['multiple' => true, 'class' => 'form-control', 'id' => 'category']) !!}

Related

Get ID from Form:select inside if-else in Laravel

I have 2 Form::select's inside an id-else statement in my view in Laravel 5.5. How do I get the id from these? Tried to put the ID in there on several location but none works..
this is part of the view:
<div class="o-grid__col u-12/12#sm">
{!! Form::label('', __('profile.contactSalutation').'*') !!}
#if( Session::get('urlLang') == "en" )
{!! Form::select(__('contact_contactSalutation'), array('Miss' => 'Miss', 'Sir' => 'Sir'),array('class' => 'c-dropdown c-dropdown__simple u-mb-x6'),['required' => 'required']) !!}
#else
{!! Form::select(__('contact_contactSalutation'), array('Frau' => 'Frau', 'Herr' => 'Herr'),array('class' => 'c-dropdown c-dropdown__simple u-mb-x6'),['required' => 'required']) !!}
#endif
</div>
This is the definition of Form::select:
{!! Form::select(NAME, OPTIONS, DEFAULT_SELECTED, ['id' => 'test', 'class' => 'any-class', ... so on with the extras in this array]) !!}
in your case will be:
{!! Form::select(__('contact_contactSalutation'), array('Miss' => 'Miss', 'Sir' => 'Sir'), null, array('id' => 'YOUR ID HERE', 'class' => 'c-dropdown c-dropdown__simple u-mb-x6', 'required' => 'required')) !!}
notice the null (so nothing pre-selected) as the third argument.
Try this
$choices = ['Frau'=> 'Frau', 'Herr'=> 'Herr'];
{!! Form::select(__('contact_contactSalutation'), $choices, null, ['id' => 'my_id', 'class'=> 'c-dropdown c-dropdown__simple u-mb-x6', 'required']) !!}
or //if you want to select a default value
{!! Form::select(__('contact_contactSalutation'), $choices, $result->contact ?? null, ['id' => 'my_id', 'class'=> 'c-dropdown c-dropdown__simple u-mb-x6', 'required']) !!}
$result->contact is your DB column name
3rd argument is used for a selected value you can use coalesce Operator either from a saved value or null

Unsupported operand types (View: C:\Users\10User\fyp2.2\resources\views\route\editRoute.blade.php)

Routes
Route::get('/editRoute/{id?}',['uses'=>'RouteController#edit' , 'as' =>'route.editRoute']);
Edit View blade
{!! Form::select('driver_id', ['$driver_id' => '---Select Driver---']+$drivers, null, ['class' => 'form-control']) !!}
Edit Controller
$routes = Route::find($id);
return view('route.editRoute', compact('routes'));
You can change that in one of two ways:
Add placeholder:
{!! Form::select('driver_id', $drivers, null, ['class' => 'form-control', 'placeholder'=>'---Select Driver---']) !!}
Change current select list:
{!! Form::select('driver_id', ['' => '---Select Driver---']+$drivers, null, ['class' => 'form-control']) !!}
Both option will work but first is my preferred :)

Is there any way to prevent input type=“number” getting negative values?

How can I prevent the numbers which was given as input in the form, not to go as negative value?
Here is my code:
{!! Form::input('number', 'mobile', null, array('id' => 'mobile', 'class' => 'input-lg form-control TabOnEnter', 'placeholder' => 'Eg: 9876543210', 'tabindex' => 15)) !!}
Use the min attribute like this:
<input type="number" min="0">
In Laravel:
{!! Form::input('number', 'mobile', null, ['type' => 'number', 'min' => 0, 'id' => ....]) !!}
Use type="number" and min=0:
<input type="number" min="0">
Or:
{!! Form::input('number', 'mobile', null, ['type' => 'number', 'min' => 0, 'id' => ....]) !!}
You can also try Form::number:
{!! Form::number('number', 'mobile', ['min' =>0, 'id' => ....]) !!}
You can use server side validation provided by Laravel Validator class.
https://laravel.com/docs/5.3/validation#rule-min

Laravel validation with input field as array "title[]" throws always the same error

No matter which validation rule i brake as long as i have an array notation in input name like this
<div class="form-group">
{!! Form::label('titile', '* Eventname: ', ['class' => 'control-label']) !!}
{!! Form::text('title[]', null, ['class' => 'form-control', 'required']) !!}
</div>
i get this error:
I have tried to use simple plain html input like this
<input type="text" name="title[]" />
and even like this
{!! Form::text('title', null, ['name' => 'title[]','class' => 'form-control', 'required']) !!}
But nothing works.
Only if i make the input field without array notation [] the validation works properly...
my validation rule is this
$this->validate($request, [
'title' => 'required|min:2',
]);
I don't know what else to do, if anyone had similar problem please help.
UPDATE:
i have tried it like this now with only one form input:
<div class="form-group">
{!! Form::label('title', '* Eventname: ', ['class' => 'control-label']) !!}
{!! Form::text('title', null, ['name' => 'title[]','class' => 'form-control', 'required', 'placeholder' => 'z.B. Deutscher Filmpreis']) !!}
</div>
-
public function rules()
{
$rules = [
];
foreach($this->get('title') as $key => $val)
{
$rules['title.'.$key] = 'numeric';
}
return $rules;
}
Write your validation in individual request file. This ('title' => 'required|min:2') validation does not work for array input. Try this technique for dynamic field validation.
public function rules()
{
$rules = [
];
foreach($this->request->get('title') as $key => $val)
{
$rules['title.'.$key] = 'required|min:2';
}
return $rules;
}
Very Good example at laravel news site.
https://laravel-news.com/2015/11/laravel-5-2-a-look-at-whats-coming/
OK i finally solved this. In Laravel 5.3 something is changed and you can't put empty array brackets for field name.
You must declare indices inside...for example:
this doesn't work
{!! Form::text('title[]', null, ['class' => 'form-control', 'required']) !!}
but this works
{!! Form::text('title[0]', null, ['class' => 'form-control', 'required']) !!}
UPDATE
So after solving this now i know that if you put empty [ ] brackets this will work if you have multiple input fields with same name...but if you put empty brackets and you have an option to add new fields dynamically like i did...then that single field with empty array brackets will fail because you actually don't have an array...and you must put [0] some indices inside...
and then it works

Laravel 5 required_if validation for radio buttons

The laravel required_if validation doesn't seem to work when you have radio buttons.
I have the following rules:
'method' => 'required|in:Email,Url',
'email' => 'required_if:method,Email'|'email',
'url' => 'required_if:method,Url'|'url',
In my form I have the following:
{!! Form::radio('method', 'Email', true ) !!}
{!! Form::radio('method', 'Url', false ) !!}
{!! Form::text('email', null, ['maxlength' => '255', 'class' => 'form-control']) !!}
{!! Form::text('url', null, ['maxlength' => '1000', 'class' => 'form-control']) !!}
But the validation doesn't seem to fire?
My mistake - added extra apostrophes to the following:
'email' => 'required_if:method,Email'|'email',
'url' => 'required_if:method,Url'|'url',
Should be:
'email' => 'required_if:method,Email|email',
'url' => 'required_if:method,Url|url',

Resources