Laravel Form Class without second argument - php

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

Related

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

Laravel & LaravelCollective HTML/Forms Error

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

form model binding in laravel 5.2

I have been reading about form model binding https://laravelcollective.com/docs/5.0/html#form-model-binding
It's very cool to populate DB values in html form.
I tried like this and this works fantastic.
{{ Form::model($university,array('url' => admin_path('universities/edit'),'id' => 'add_university','name' =>'add_university','data-validate'=>"parsley")) }}
{{ Form::label('university_name', 'University name',array('class'=>'control-label')) }}
{{ Form::text('university_name')}}
{{Form::close()}}
But the problem is here, Cause i want to add more attributes in input like class SO i am using
{{ Form::label('university_name', 'University name',array('class'=>'control-label')) }}
{{ Form::text('university_name','',array('class' => 'form-control'))}}
If i leave blank valuecolumn then nothing populate in textbox and if i using like this
{{ Form::label('university_name', 'University name',array('class'=>'control-label')) }}
{{ Form::text('university_name',$university->university_name,array('class' => 'form-control'))}}
Then what is use of model binding.
Please explain.
Thanks
{{ Form::text('university_name','',array('class' => 'form-control'))}}
It should be:
{{ Form::text('university_name',null,array('class' => 'form-control'))}}
'' means the real string, not null.
thanks, mathielo, for helping me on grammar

Add Class to select element on Laravel

I have this problem, I can't find the way to add class attribute in this Dropdown box
{{Form::select('bancada', Bancada::lists('nombre','idBancada'))}}
I have tried various syntax, but can not get it to work.
Any suggestions? Thanks
Use the forth parameter to add attributes to your element.
{{Form::select('bancada', Bancada::lists('nombre','idBancada'), null, ['class' => 'my_class'])}}
Take a look at the source ($options is the same as attributes for your element).
{{ Form::select('status', $statusList, null, array('class' => 'form-control')) }}
In above example you can add class as fourth parameter

How to input a variable value in field of name or id using blade template in laravel?

I need to input text name and id dynamically and I am trying as below
{{ Form::text('practice', $practice->name, array('class' => 'large-2','id'=<?php echo $inputTextName;?>)) }}
But it is not parsing PHP here. Even I tried 'id'={{$inputTextName}} but it is giving error.
Can you try this,
{{ Form::text('practice', $practice->name, array('class' => 'large-2','id'=>$inputTextName)) }}

Categories