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' => '']) }}
Related
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')) }}
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']) !!}
i am still learning how to work with laravel.
Now i have learned how to make a form in laravel. But i have some trouble to echo a variable.
What i want to do is: i want to echo a variable as the value of an input-field if this variable exists, otherwise it should echo nothing.
so my form line looks like this
{{Form::text(
'league_name',
'#if(isset($varialble) {{$variable}} #else {{$nothing}} #endif)'
)}}
How can i echo a variable in a form?
i am using blade btw.
This is the right way to do it:
{!! Form::text('league_name', isset($varialble) ? $variable : '') !!}
If you're using PHP7, you can do this:
{!! Form::text('league_name', $varialble) ?? '') !!}
Update
To add placeholder, pass it in an array as third parameter. Also, you usually want to pass a class:
{!! Form::text('league_name', isset($varialble) ? $variable : '', ['placeholder' => 'My placeholder', 'class' => 'form-control']) !!}
Try This. Here You can use null instead of $nothing and $variable is your variable
{{Form::text('league_name',$varialble ? $variable : $nothing)')}}
In Laravel use can use normal PHP code also
{!! Form::text('league_name', !empty($varialble) ? $variable : '') !!}
here you can use {!! data/variables !!} or {{ data/variables }}
above will resolve your problem.
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']]) }}
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)) }}