Adding classes/ids to forms in Laravel 4 - php

I am trying to add classes and ids to specific elements of a form in Laravel 4. For example, I would like this:
<textarea type="text" id="description" onfocus="this.value=''; setbg('#f0f7f8');" onblur="setbg('white')" name="description" value="" rows="10"></textarea>
to be applied to:
{{ Form::label('description', 'Description:') }}
{{ Form::textarea('description')}}
I didn't see this in the documentation. Thank you!

Use the third parameter for the Form::textarea method, passing a key-value array. Ex:
Form::textarea('description', null, [
'id' => 'description',
'rows' => 10,
]);

Although its an old question, I just wanted to say that you can escape the javascript like this:
Form::textarea('description', null, array(
'id' => 'description',
'rows' => 10,
'onFocus' => 'this.value=\'\'; setbg(\'#f0f7f8\');'
));
Thats it :)

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

Laravel Form::select shows nothing

Hi I'm using Laravel version 5.6 and got a problem in view with Form::select.
I already made some Form::open with 'text' and 'textarea' they all worked fine, but the Form::select do not generate the select fild on my view.
I used this code:
{!! Form::label('isPropaganda', 'Propaganda:') !!}
{!! Form::select('isPropaganda', ['Não' => '0', 'Sim' => '1'], null, ['class'=>'form-control','multiple']) !!}
I found the example here http://laravel-recipes.com/recipes/163/creating-a-select-box-field but didn't workout. How can I fix it?
Try flipping the keys and values. Keys cannot have special characters in them. Also, I'm not sure what that 'multiple' attribute is doing there. If it's meant to be a css class, place it inside the 'class' array key, otherwise add it to a new attributes array.
{!! Form::select('isPropaganda', ['0' => 'Não', '1' => 'Sim'], null, ['class'=>'form-control multiple']) !!}

Adjust number of Rows to Form:: Textarea Laravel 5

How can I control the number of Rows added to a textarea using the Illuminate\Html\FormFacade class?
I have added the field into my template.
<div class="form-group">
{!! Form::label('placeOfDeath','Place of Death') !!}
{!! Form::textarea('placeOfDeath',null,['class'=>'form-control']) !!}
</div>
When it gets rendered the textarea has cols="50" and rows="10"
<textarea class="form-control" name="placeOfDeath" cols="50" rows="10" id="placeOfDeath"></textarea>
I want a way to control these numbers, I have checked the documentation but couldnt spot anything?
The options (third parameter) array is actually the attributes array of that element, you so can just pass any 'key' => 'value' and the element will have it as attributes, for example:
{!! Form::textarea('placeOfDeath',null,['class'=>'form-control', 'rows' => 2, 'cols' => 40]) !!}
I have accepted the other answer as it works perfectly.
I have also found that the class actually checks for an attribute size
protected function setQuickTextAreaSize($options)
{
$segments = explode('x', $options['size']);
return array_merge($options, array('cols' => $segments[0], 'rows' => $segments[1]));
}
Its a minor space saving, Im not sure it makes the code anymore readable, but it is an alternative to cut off a few characters
['size' => '30x5']
Also try this:
{!! Form::textarea('placeOfDeath',null, array('class'=>'form-control',
'rows' => 10, 'cols' => 50)) !!}

Laravel form inputs - how to set maxlength

Maximal length of HTML inputs can be set by:
<input type="text" name="" maxlength="10">
Is there a way how to set maxlength of form input when creating inputs in Laravel way?
{{ Form::text('name', null, array('placeholder' => '')) }}
{{ Form::text('name', null, array('placeholder' => '','maxlength' => 10 )) }}
Works with the Chrome Version 39.0.2171.95 (64-bit)
You can also check in validation as follow. For more details, you can take a look in validation
$validator = Validator::make(
array('name' => 'name'),
array('name' => 'required|max:5')
);

How to create a form text field with required attribute in Laravel?

Like
Username: <input type="text" name="usrname" required>
How I'll create a form text field with required attribute in Laravel?
Simple, extra attributes can be passed as an array in the text method:
{{ Form::text('usrname', 'Default Value', array('required' => 'required', 'autofocus' => 'autofocus', 'placeholder' => 'Enter your username here')) }}
You may try this:
{{ Form::text('usrname', Input::old('usrname', 'default value'), array('required')) }}
The array passed in as the third parameter handles all attributes of the HTML tag you are creating within the Form::text.

Categories