Laravel & LaravelCollective HTML/Forms Error - php

I'm building a simple form with a username and password.
When I construct a username field like this:
{{ Form::text('username') }}
My page loads without issue. As soon as I want to define a class like this:
{{ Form::text('username', ['class' => 'awesome']) }}
I get an error, like so:
ErrorException in HtmlBuilder.php line 65:
htmlentities() expects parameter 1 to be string, array given
Haven't been able to find any info regarding this error online. These examples are taken STRAIGHT from the LaravelCollective documentation.
Any ideas? Thanks!

You should pass the class in the third parameter like so:
{{ Form::text('username', null, ['class' => 'awesome']) }}
or:
{{ Form::text('username', '', ['class' => 'awesome']) }}
The second parameter is the value field

If you are using Laravel 5 You should write like this:
{!! Form::text() !!}
Not like this:
{{ Form::text() }}
Also you have attributes array given as second parameter which is default value of this input. Please change it to:
{!! Form::text('your_name', null, ['class' => 'someclass']) !!}

Looking at the source code, the expected parameters are:
Form::text($name, $value = null, $options = [])
The second parameter is for specifying a default value for the form element. So to define a class, you should pass the array to the third parameter:
Form::text('username', null, ['class' => 'awesome'])

If you are using laravel 5.4
you shouldn't use double curly for laravel collective html form like this
{{ Form::() }}
try this instead
{!! Form::text('username', $value = null, ['class' => 'awesome']) !!}

Related

Laravel Form Class without second argument

I simply want to give the text element of my edit-form a class.
{{ Form::text('first_name') }}
I know that i have to use an array with the class of the field as third argument. But i do NOT want to give a second argument. This one
{{ Form::text('first_name' , ' ' , array('class' => 'form-control')) }}
applys the class correctly, but sets the dafault value to an empty string. This is a problem, because now laravels auto-complete function doesn't fill the form with the correct data from the database. Is there a way to give no second argument or setting it to default like this?
{{ Form::text('first_name' , default , array('class' => 'form-control')) }}
Thanks!
Okay, i solved it myself. It is as easy as it always is. Simply use
{{ Form::text('first_name' , null , array('class' => 'form-control')) }}

Laravel Collective Formbuilder label localization

I just started using the Laravel Collective Form Service Provider/FormBuilder.
I use this custom component for rendering a text field with label. The problem is that I am trying to translate it's label with the __() function but the $name variable gets tansformed from first_name to First Name in a really late stage.
<div class="control-group">
{{ Form::label($name, null, ['class' => 'group__label']) }}
{{ Form::text($name, $value, array_merge(['class' => 'control-
group__control'], $attributes)) }}
</div>
I can't simply do this:
{{ Form::label(__($name), null, ['class' => 'group__label']) }}
Again, because it gets first_name and later transforms it into First Name. My nl.json file contains a translation for First Name, not first_name.
If I add the _() translate function to the last rule of the label method in FormBuilder is, then it solves my problem. But ofcourse, I don't want to modify vendor code!
return $this->toHtmlString('<label for="' . $name . '"' . $options . '>' . __($value) . '</label>');
So. How to solve this issue? Do I, somehow, need to create a custom FormBuilder->label() method?
Use trans() helper:
{{ Form::label(trans($name), null, ['class' => 'group__label']) }}
Using name of field with trans() can create errors when the language changed.
you should use somthing like this
{{ Form::label('name', trans(''), ['class' => '']) }}

adding data to #include throws an error

My head is aching as to what is the error in my code.
main.blade.php
Html with angular codes here....
#include('modal-template-common', ['link' => route('client.logout')])
modal-template-common.blade.php
{!! Html::link($link, 'OK', ('class'=>'btn btn-danger')) !!}
Now the problems is whenever I try to render it returns an error of
ErrorException in UrlGenerator.php line 273
angular.js:11607 Error: [$compile:tpload]
any enlightenment would greatly help me.
I am not a fan of using Html Facade.
But isn't ('class'=>'btn btn-danger') supposed to be an array ?
So, it should be like this:
{!! Html::link($link, 'OK', array('class'=>'btn btn-danger')) !!}
OR
{!! Html::link($link, 'OK', ['class'=>'btn btn-danger']) !!}
The third argument is an array. You can pass any number of key => value pairs in this array to assign other related tag's attributes. In your case, the class attribute.
For more options, refer this link

Laravel 5 binding value in bootstrap-slider in edit/Patch

I'm implementing bootstrap-slider in my CRUD, I have implemented it successfully in Create, the problem is when I try to edit it,
I want to get the current value from the Model. Idk how to do this.
This is for PATCH.
<div class="form-group">
<h3 class='box-title text-info'>Percentage</h3>
{!! Form::input('text','percentage',null,['id'=>'ex8', 'data-slider-id'=>'ex1Slider', 'data-slider-min'=>'0', 'data-slider-max'=>'100', 'data-slider-step'=>'5', 'data-slider-value'=>'50']) !!}
</div>
In your form instead of creating a new form. You will bind the form to the model.
{!! Form::model('modelname', [options here] !!}
All the fields will math the model's property values.
Edit
Here is an example
You must used something like this to create a EDIT FORM
{{ Form::model($smartphones, ['method' => 'PATCH', 'url' => 'smartphones/'.$smartphones->id]) }}
You get by using $(your_model)['inputID']...You can use in "data-slider-value"... Something like this
{{ Form::input('text','mem_ram', null, ['id' => 'mem_ram', 'data-slider-value'=>$smartphones['mem_ram']]) }}

Laravel: how do I pass a value from a form to a controller?

I have a form:
{ Form::open(array('action' => 'RatesController#postUserRate', $client->id)) }}
{{ Form::text('rate', '', array('placeholder' => 'Enter new custom client rate...')) }}
{{ Form::submit('Submit', array('class' => 'btn btn-primary')) }}
{{ Form::close() }}
How do I pass my $client->id value through the form to my controller method?
I currently have a controller method that looks like this:
public function postUserRate($id)
{
$currentUser = User::find(Sentry::getUser()->id);
$userRate = DB::table('users_rates')->where('user_id', $currentUser->id)->where('client_id', $id)->pluck('rate');
if(is_null($userRate))
{
...
}else{
....
}
}
And the error log says "Missing argument 1 for RatesController::postUserRate()"
Any ideas on how to pass this $client->id into my controller so I can use it as I want to above?
Add {{ Form::hidden('id', $client->id) }}to the form. Then, when it's posted, you can fetch its value per usual with Input::get('id').
Also, remove the postUserRate method's argument.
You simply use :
Form::open(array('action' => array('Controller#method', $user->id)))
the variable $user->id is passed as argument to the method method, also this last one should recieve an argument as well, like so : method($userId)
Source : Laravel documentation

Categories