Laravel - htmlentities() expects parameter 1 to be string, array given - php

My blade.php code is:
{!! Form::input('text', 'tactic[]', null, array('id' => 'tactic', 'class' => ' form-control TabOnEnter', 'placeholder' => 'Tactics_1')) !!}
HtmlBuilder.php code is
public function escapeAll($value)
{
return htmlentities($value, ENT_QUOTES, 'UTF-8');
}
The error message is:
ErrorException in HtmlBuilder.php line 65:
htmlentities() expects parameter 1 to be string, array given (View: /home/seyali-02/dev/htdocs/scam/resources/views/dashboard/Scam/edit.blade.php)
And i have changed the blade.php as like
{!! Form::input('text','', 'tactic[]', null, array('id' => 'tactic', 'class' => ' form-control TabOnEnter', 'placeholder' => 'Tactics_1')) !!}
and
{!! Form::text('name', 'tactic[]', null, array('id' => 'tactic', 'class' => ' form-control TabOnEnter', 'placeholder' => 'Tactics_1')) !!}
and also text('text', .. But nothing works for me and throwing me the same error as i mentioned above.. I have gone through all the similar questions related to this but none of those answers solved my problem . So please avoid doing duplication of this question and give me clear and correct solution..

You are adding tactic[] to the name which is an array and hence when you post the data it is going as an array. Either remove it or at php end use implode.

If you want to take the input as an array then you can use this code
{!! Form::text('tactic[]',null,['id' => 'tactic', 'class' => ' form-control TabOnEnter', 'placeholder' => 'Tactics_1']) !!}

Related

Laravel - Select input not keeping old value

I have a piece of code:
{!! Form::select('option_employee_review', old('option_employee_review', $employeeReviews), $employeeReviews, ['id' => 'option_employee_review', 'class' => 'form-control ']); !!}
It saves the value to the database correct. When i go to edit the item again the select input does not keep the old value that's in the database. How do i make it so that the select input does keep its old value.
$employeeReviews:
[
2843 => "Medewerker review 1"
2849 => "Medewerker review 2"
]
I am not using your syntax, but something like this will do I think.
<option value="{{$channel->id}}" {{ (old("channel_id") == $channel->id ? "selected" : "" ) }}>{{$channel->title}}</option>
Second parameter to select function must be array of options.
Try changing it like this
{!! Form::select('option_employee_review', $employeeReviews, old('option_employee_review', $employeeReviews), ['id' => 'option_employee_review', 'class' => 'form-control ']); !!}
Or based on your parent object let's say employee you can try
{!! Form::select('option_employee_review', $employeeReviews, $employee->option_employee_review, ['id' => 'option_employee_review', 'class' => 'form-control ']); !!}

How to put translation into array in Laravel Form?

I need to have a prompt box which would be set through the key 'onsubmit' in Form array. This version works:
{!! Form::model($currentUser,
['route' => ['post.users.current.account.index', $currentUser->id],
'onsubmit' => 'return confirm(\'Are you sure?\')']) !!}
But I am unable to inject a translation into it to make it work. So far I tried this without success:
{!! Form::model($currentUser,
['route' => ['post.users.current.account.index', $currentUser->id],
'onsubmit' => 'return confirm(\''.{{trans('Users::users.current.account.index.box.confirm')}}.'\')']) !!}
Is there a way to concatenate function output inside the array value?
Please Try
{!! Form::model($currentUser,
['route' => ['post.users.current.account.index', $currentUser->id],
'onsubmit' => "return confirm('".trans('Users::users.current.account.index.box.confirm')."')"]) !!}

Store an array of elements to database (Laravel)

I need advice how to store an array to database. For example i have an input with name="user_phone[]" and i want to store to database the value of this input.
I have a form like so, also there other inputs but i copy just one:
{!! Form::open([route('some.router')]) !!}
<fieldset class="form-group">
{{ Form::label(null, 'Phone') }}
{{ Form::text('user_phone[]', null, ['class' => 'form-control'] ) }}
</fieldset>
{!! Form::close() !!}
and the controller:
public function postAddOrder(Request $request)
{
$this->validate($request, [
'receipt_date' => 'date|required',
'return_date' => 'date|required',
'user_name' => 'required',
'user_phone' => 'required',
'work_sum' => 'integer|required',
'user_descr' => 'required',
'foruser_descr' => 'required'
]);
$user = new User;
$user = $user->all()->find($request->input('user_name'));
$order = new Order([
'receipt_date' => $request->input('receipt_date'),
'return_date' => $request->input('return_date'),
'user_name' => $user->fio,
'user_phone' => $request->input('user_phone'),
'device' => $request->input('device'),
'work_sum' => $request->input('work_sum'),
'master_name' => $request->input('master_name'),
'user_descr' => $request->input('user_descr'),
'foruser_descr' => $request->input('foruser_descr'),
'type' => $request->input('type'),
'status' => $request->input('status'),
'engineer' => $request->input('engineer'),
'partner' => $request->input('partner'),
'office' => $request->input('office')
]);
$order->save();
return redirect()->route('admin.orders.index');
}
The problem is when i'm submitting the form and getting the error:
htmlentities() expects parameter 1 to be string, array given
Also i'm using casts to store an array to DB:
/**
* The attributes that should be casted to native types.
*
* #var array
*/
protected $casts = [
'user_phone' => 'array',
];
The values are storing correctly, the main problem is that the validate() method is not catching the errors. For example im not filling some data in inputs which are required. When instead of getting the error like something is required im getting error
htmlentities() expects parameter 1 to be string, array given
When im filling all input with data everything goes ok.
I think the problem comes from your rule
'user_phone' => 'required
To validate array values you should use the array validation. (Link)
rewrite your rule like so
"user_phone.0" => "required"
this will ensure that at least one user_phone is provided.
In case you wanna validate phone format just go with:
"user_phone.*" => "{Insert validation here}"
Found the definition.
{!! Form::open([route('some.router')]) !!}
<fieldset class="form-group">
{{ Form::label(null, 'Phone') }}
{{ Form::text('user_phone[0]', null, ['class' => 'form-control'] ) }}
</fieldset>
{!! Form::close() !!}
We must pass the index in inputs. Like name="user_phone[0]" after that we are not getting the error:
htmlentities() expects parameter 1 to be string, array given
And validate() method catching the errors. It was the only solution for me.

Laravel not letting me add styles to my Form::text()

This is my code for the form builder, and when I remove the array from the text() everything is fine, but when I add it I get this error
ErrorException in helpers.php line 454: htmlentities() expects
parameter 1 to be string, array given (View:
/Users/samir/Sites/elicant/resources/views/pages/classes.blade.php)
{!! Form::open(array("url" => 'addClass', "method" => 'post')) !!}
{!! Form::text('classname', array('style' => 'width:80%;')) !!}
{!! Form::text('classcolour') !!}
{!! Form::submit("Add") !!}
{!! Form::close() !!}
What have I done wrong?
Form::text has 3 parameters: $name, $value, array $options = array()
So to make your code working:
{!! Form::text('classname', null, array('style' => 'width:80%;')) !!}

How to send the parameter in a string using Laravel

When I use Form::select, inside I can pass an attribute parameter that is an array, but I have problems concatenating the Id with the rest of the string
{!! Form::select('product_category_id', $categories, null, ['class' => '','onchange' => 'addComboCategory(this, null,\'ajax_product_category\', \'ajax_product_info\', \'?parameter_id=2\')']) !!}
The method:
'onchange' => 'addComboCategory(this, null,\'ajax_product_category\', \'ajax_product_info\', \'?parameter_id=2\')
The parameter:
\'?parameter_id=2\'
How about this?
'onchange' => "addComboCategory(this, null, 'ajax_product_category', 'ajax_product_info', '?parameter_id=2')"
With variable:
$id = 2;
'onchange' => "addComboCategory(this, null, 'ajax_product_category', 'ajax_product_info', '?parameter_id={$id}')"

Categories