Laravel Custom Form With Multiple Color - php

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?

Related

Retrieve old value from Laravel Form::select

I have the following form field in a Laravel project in my create view (create.blade.php):
{{ Form::label('format', 'Type', ['class'=>'label']) }}
{{ Form::select('format', array('is_html' => "HTML", 'is_video' => 'Video'), null, ['class' => 'form-control format']) }}
I retrieve this inside the format column in my database so i can use this data in my project.
Now, inside the edit (edit.blade.php) view I want to have the already selected data to reflect. So when the user has selected "video", the select option will already be set to Video.
While I was typing this question, i figured it out. Already typed the entire question so might as well post it to help someone out.
The third argument needs to correspond with the data I retrieve from the database. So, for example, if i wanted the Video to be selected, it would look like this:
{{ Form::label('format', 'Type', ['class'=>'label']) }}
{{ Form::select('format', array('is_html' => "HTML", 'is_video' => 'Video'), 'is_video', ['class' => 'form-control format']) }}
I replaced the is_video with the variable that contains the data from my database and it's working as expected.
{{ Form::label('format', 'Type', ['class'=>'label']) }}
{{ Form::select('format', array('is_html' => "HTML", 'is_video' => 'Video'), $var->format, ['class' => 'form-control format']) }}

htmlspecialchars() expects parameter 1 to be string, array given

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

How can i forbid displaying a form field in twig from formType in symfony?

How can I cancel displaying form field in twig, if I do not need it?
<p>Form: <br>
$form ->add("rxOriginCode", ChoiceType::class, [<br>
"label" => "Rx Origin",<br>
"required" => true,<br>
"choices" => <br>PrescriptionOriginCode::getDictionary(EnumFactory::FLAG_SHORT),<br>
"mapped" => false,<br>
"data" => $data->getRx()->getRxOriginCode(),
If you are displaying your elements one by one, there is a parameter in the method form_end to prevent Twig from generating elements you did not display yes :
{{ form_end(form, {'render_rest': false}) }}
http://symfony.com/doc/current/reference/forms/twig_reference.html#form-end-view-variables

Laravel 5 Form loop gives htmlentities() expects parameter 1 to be string, array given

I have the following stripped form:
{{ Form::text('invoicerow[]', null, array( 'class' => 'form-control '.( $errors->has('initial') ? 'errorborder':'' ) ) ) }}
You can add a row by JS.
When i post this and validates it false it's return to the form page with this error.
htmlentities() expects parameter 1 to be string, array given (View: /home/xxx/public_html/resources/views/invoice/create.blade.php)
How can i obviated this?
Using forms that create arrays cause problems with Laravel. The workaround for this is to add explicit indexes to your form names (as below). This should resolve the issue for you, and allow you to correctly validate each field.
{{ Form::text('invoicerow[0]', null, array( 'class' => 'form-control '.( $errors->has('initial') ? 'errorborder':'' ) ) ) }}
{{ Form::text('invoicerow[1]', null, array( 'class' => 'form-control '.( $errors->has('initial') ? 'errorborder':'' ) ) ) }}
{{ Form::text('invoicerow[2]', null, array( 'class' => 'form-control '.( $errors->has('initial') ? 'errorborder':'' ) ) ) }}
...
However, it does mean that the + and x buttons you have in the form need to recalculate the indexes for the fields when a new row has been added, or deleted.
E.g. if I delete invoicerow[1], your JavaScript will need to recalculate the index for the other fields, so invoicerow[2] is renamed to invoicerow[1].
Similarly, if I add a new row to the end, the name for the new field should be invoicerow[3] (assuming I've already added 3 fields before), i.e. invoicerow[n+1]

Blade select input - selling a selected option

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

Categories