Laravel form inputs - how to set maxlength - php

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

Related

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.

Disabled select doesn't validate when order is submitted in blade template

I have this condition in my order.blade
<div class="form-group">
<label for="shipping_method" class="control-label">Shipping method:</label>
#if( $total < $free_delivery )
{{ Form::select('shipping_method', [
Settings::SETTINGS_SHIPPING_NORMAL => 'Normal Delivery',
Settings::SETTINGS_SHIPPING_EXPRESS => sprintf('Express Delivery - $%.2f', Settings::getOption('express_shipping_cost'))
], null, ['class' => 'form-control']) }}
#else
{{ Form::select('shipping_method', [
Settings::SETTINGS_SHIPPING_FREE => 'Free Delivery'
], null, ['class' => 'form-control','disabled']) }}
#endif
</div>
When user make order for more than X amount he gets free delivery. The problem here is that when I try to submit order with free delivery i.e. in else block I've got that shipping method is required.
In IF block is working. Why doesn't get the name of else block? Here is my validation rule
$validatorRules = array(
'captcha' => 'required|captcha',
'shipping_method' => 'required|in:' . implode(',', [Settings::SETTINGS_SHIPPING_NORMAL, Settings::SETTINGS_SHIPPING_EXPRESS, Settings::SETTINGS_SHIPPING_FREE])
);
In a form if an element is disabled, it's not send with your request, you can enable it since you only have this in your select when shipping is free.
If you really want it disable you can catch when your form is submitted in JS, and the value to and hidden field with the same name or just add an hidden field in your form
{{ Form::hidden('shipping_method', /* Params */) }}
{{ Form::select('shipping_method', [
Settings::SETTINGS_SHIPPING_FREE => 'Free Delivery'
], null, ['class' => 'form-control','disabled']) }}

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

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.

Adding classes/ids to forms in Laravel 4

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 :)

Categories