I have a form and if i submit the form with all the right data everything goes perfectly fine... but if I intentionally make any flaw
(validation for example 'title' => 'required|min:2')
and I put only one character for title or if I miss any required field I get this error:
htmlspecialchars() expects parameter 1 to be string, array given
I have figured out that the problem is with this select box
{!! Form::select('item[0][]', $items, null, ['class' => 'form-control', 'required']) !!}
and I even tried to use a normal select box without form helper {!! !!}
But I still get the same error!
So the problem is somewhere with validation when there is a nested array....is there a way to fix this?
OK I finally have an answer for this problem....it seems like something has changed in Laravel 5.3 and if you want to have a name with array like this
{!! Form::label('title', '* Eventname: ', ['class' => 'control-label']) !!}
{!! Form::text('title[]', null, ['class' => 'form-control', 'required') !!}
You must put [0] something in brackets 'indices' like this:
{!! Form::text('title[0]', null, ['class' => 'form-control', 'required') !!}
and then in validation use
title.*
for rule
UPDATE
Because i use dynamic form that can be expanded and new form fields added (optionally) i needed to put [] array notation for a name but actually if you already have hard coded many fields with the same name like item[]
you don't have to put [0] indices inside. The validation will work for them.
The problem comes only if you have a single input field and you put [] array notation along the name for example 'item[]'
this will trigger the error if any validation rule is broken...
Related
{!! Form::select('country',['1' => 'Albania','2'=>'Kosovo','3'=>'Germany','4'=>'France'],null, ['class'=>'form-control','placeholder'=>'Select Country']) !!}
This type of input doesn't take the values of the array but instead, it takes the pre-default value of option inside select.enter code here
According to the Form Collective documentation, the first argument of the Form::select() is the name of the select box, the second is an array of the input values while the third is the default value (this could be set to null). The fourth is an optional array of attributes/values.
This should work for you.
{!! Form::select('country',['Albania' => 'Albania','Kosovo'=>'Kosovo','Germany'=>'Germany','France'=>'France'],'Kosovo',['class'=>'form-control','placeholder'=>'Select Country']) !!}
{!! Form::select('country', [null => 'Select Country'] + ['Albania' => 'Albania','Kosovo'=>'Kosovo','Germany'=>'Germany','France'=>'France'], null, ['class' => 'form-control']) !!}
For more information : https://laravel.com/docs/4.2/html#drop-down-lists
Hi I'm using Laravel version 5.6 and got a problem in view with Form::select.
I already made some Form::open with 'text' and 'textarea' they all worked fine, but the Form::select do not generate the select fild on my view.
I used this code:
{!! Form::label('isPropaganda', 'Propaganda:') !!}
{!! Form::select('isPropaganda', ['Não' => '0', 'Sim' => '1'], null, ['class'=>'form-control','multiple']) !!}
I found the example here http://laravel-recipes.com/recipes/163/creating-a-select-box-field but didn't workout. How can I fix it?
Try flipping the keys and values. Keys cannot have special characters in them. Also, I'm not sure what that 'multiple' attribute is doing there. If it's meant to be a css class, place it inside the 'class' array key, otherwise add it to a new attributes array.
{!! Form::select('isPropaganda', ['0' => 'Não', '1' => 'Sim'], null, ['class'=>'form-control multiple']) !!}
I am using form builder and I share one form for edit and crete page so there are some variables I don't want to use.
I know that:
{!! Form::text('name', isset($admin_link->name) ? $admin_link->name : null, ['class' => 'form-control']) !!}
would work but I do not consider it the best way. Is there any way to use something else like isset for form builder?
I sent the model to the blade with values what caused autocomplete of the input what is probably what I have been looking for:
{!! Form::model( $admin_link, ['route' => ['admin.links.update', $admin_link->id], 'method' => 'post', 'class' => 'main_link_form']) !!}
I am using laravel form to insert multiple colors using from input type as color,
{{ Form::input('color','color[]',null, array('class' => 'form-control-color','placeholder' => 'Enter Color','id' => 'exampleInputTitle1')) }}
But i am getting this error, "htmlentities() expects parameter 1 to be string, array given", Can any one have an idea to how to handle the Color input field as multiple values.
I am unsure of this, but if you try:
{{ Form::input('color','color[0]',null, array('class' => 'form-control-color','placeholder' => 'Enter Color','id' => 'exampleInputTitle1')) }}
Does it work?
How do I set a certain option as currently selected?
My current code is:
{{ Form::select('make',$makes , NULL, ['class' => 'form-control']) }}
$makes is an array of car makes.
Lets say the array looks like:
[
0 => 'Audi',
1 => 'BMW',
2 => 'Mercedes'
]
If I am editing a car, I know that in my DB I have said that car is X make.
In this case, lets say its a BMW. When I create this select input I want BMW to already be shown as the selected option. I have this value already, its just getting the blade input to set it to selected.
I tried:
[$tMake->display_name] + $makes
But that just add a new option to the list, which is not what I want.
Thanks.
You need to tell the select the ID you want pre-selected.
You can do this:
{{ Form::select('make',$makes , $tMake->id, ['class' => 'form-control']) }}
And if you are using validation - you'll want to do something like this, so that the old input is shown if the validation fails
{{ Form::select('make',$makes , Input::old('make', $tMake->id), ['class' => 'form-control']) }}