Add maxlength in form textArea - laravel

The default textArea characters are 255. Now, I want to extend the characters to 2000. How to add maxlength in Form textArea using Laravel Blade?
Here's what I've tried so far:
<div class="form-group">
{{ Form::label('comments', 'Comments*') }}
{{ Form::textArea('comments', null , ['class'=> 'form-control', 'placeholder' => 'Comments']) }}
<span class="error-msg">{{ $errors->first('comments') }}</span>
</div>

Try setting the maxlength of the textArea.
e.g.
{{ Form::textArea('comments', null , ['class'=> 'form-control', 'placeholder' => 'Comments', 'maxlength' => '2000']) }}

If the maxlength attributes don't work, you can write it out as a macro.
open your AppServiceProvider and enter the following in the boot method.
Form::macro('myTextArea', function()
{
return '<textarea name="comments" class="form-control" placeholder="Comments" maxlength="2000"></textarea>';
});
and just call Form::myTextArea() in your form

Try changing textArea to textarea
{{ Form::textarea('comments', null , ['class'=> 'form-control', 'placeholder' => 'Comments', 'maxlength' => 2000, 'size' => '30x5']) }}
If the text area has an attribute named "size" it should be in the format "30x5" where the first digit represents the columns and the second digit represents the rows.

Related

Text Area and Null, Laravel Collective

I've been using Laravel Collective for my forms and I seem to have encountered an issue with textareas. One that won't let me update null textarea fields with the same code I would use for a text field. I think the issue is with 'null' as it allows me to change the field if the textarea has text loaded. Does anyone know how to fix this so I can change null fields with textareas?
{!! Form::label ('otherinfo', 'Other information:') !!}
{!! Form::textarea ('otherinfo', null, array('class' => 'form-control', 'required' => '', 'maxlength' =>'1500') ) !!}
Your example should work fine. Make sure you update your Controller to accept and save the value that is present in $request->input('otherinfo').
<?php
$otherinfo = 'Hello World';
?>
<div class="form-group">
{!! Form::label('otherinfo', 'Other information:') !!}
{!! Form::textarea('otherinfo', $otherinfo, ['class' => 'form-control', 'size' => '50x3']) !!}
</div>

Prefill input text with LaravelCollective

I have a Laravel 5.2 application, I have a form with a lot of elements (checkboxes,selects, inputs file, input text, etc). So, I'm using the Form & HTML extension of LaravelCollective
So, I'm using form::model to bind the model to the form, that's working great with all the elements except with the input text, and a email element. I have this:
{!! Form::model($mymodel,array('url'=>'myurl','method'=>'POST')) !!}
{!! Form::email('mail', $value = null, $attributes = array('class'=>'form-control')) !!}
{!! Form::text('Username', '', array('class' => 'form-control')) !!}
{!! Form::close() !!}
Then, listing the attributes that I have in the model (I made dd($mymodel) and copied only the fillable):
array:25 [▼
0 => "mail"
1 => "Username"
]
The others attributes are prefilled properly, except input text and input mail.
The field names need to match the columns in your Model. The column names are usually lower-case.
The email field should be:
Form::email('mail', null, array('class'=>'form-control'))
I.e. it should NOT have $value = null or $attributes = ...
I would also set the username field to null:
Form::text('Username', null, array('class' => 'form-control'))

Laravel validation with input field as array "title[]" throws always the same error

No matter which validation rule i brake as long as i have an array notation in input name like this
<div class="form-group">
{!! Form::label('titile', '* Eventname: ', ['class' => 'control-label']) !!}
{!! Form::text('title[]', null, ['class' => 'form-control', 'required']) !!}
</div>
i get this error:
I have tried to use simple plain html input like this
<input type="text" name="title[]" />
and even like this
{!! Form::text('title', null, ['name' => 'title[]','class' => 'form-control', 'required']) !!}
But nothing works.
Only if i make the input field without array notation [] the validation works properly...
my validation rule is this
$this->validate($request, [
'title' => 'required|min:2',
]);
I don't know what else to do, if anyone had similar problem please help.
UPDATE:
i have tried it like this now with only one form input:
<div class="form-group">
{!! Form::label('title', '* Eventname: ', ['class' => 'control-label']) !!}
{!! Form::text('title', null, ['name' => 'title[]','class' => 'form-control', 'required', 'placeholder' => 'z.B. Deutscher Filmpreis']) !!}
</div>
-
public function rules()
{
$rules = [
];
foreach($this->get('title') as $key => $val)
{
$rules['title.'.$key] = 'numeric';
}
return $rules;
}
Write your validation in individual request file. This ('title' => 'required|min:2') validation does not work for array input. Try this technique for dynamic field validation.
public function rules()
{
$rules = [
];
foreach($this->request->get('title') as $key => $val)
{
$rules['title.'.$key] = 'required|min:2';
}
return $rules;
}
Very Good example at laravel news site.
https://laravel-news.com/2015/11/laravel-5-2-a-look-at-whats-coming/
OK i finally solved this. In Laravel 5.3 something is changed and you can't put empty array brackets for field name.
You must declare indices inside...for example:
this doesn't work
{!! Form::text('title[]', null, ['class' => 'form-control', 'required']) !!}
but this works
{!! Form::text('title[0]', null, ['class' => 'form-control', 'required']) !!}
UPDATE
So after solving this now i know that if you put empty [ ] brackets this will work if you have multiple input fields with same name...but if you put empty brackets and you have an option to add new fields dynamically like i did...then that single field with empty array brackets will fail because you actually don't have an array...and you must put [0] some indices inside...
and then it works

Shorthand IF within Laravel HTML Form

I have a form input set up using the HTML facade:
{!! Form::text('name', NULL, ['class' => 'form-control ($errors->has("name") ? " has-error" : "")', 'placeholder' => 'Enter your name'] ) !!}
As you can I have put the $error->has inside the class but it is just printing the if statement as I know it would.
Is there anyway I can do what I am trying but keep on using the Form facade?
Try to put the if statement outside the single quotes:
{!! Form::text('name', NULL, ['class' => 'form-control'.($errors->has("name") ? " has-error" : "").'', 'placeholder' => 'Enter your name'] ) !!}

toggle an html section on laravel 4.2 when using return Redirect

In my view i have this section where a user can add a new record to the database but the laravel form is inside a section where it is hidden by default here is my code in my view
<section id="anCity" style="display:none">
{{ Form::open(array('url'=> 'addCty')) }}
<div class="form-group">
{{ Form::label('ncty', 'City Name: ') }} <span style="color:red"><i>{{ $errors->first('ncty', ':message') }}</i></span>
{{ Form::text('ncty', Input::old('ncty'), array('class' => 'form-control','placeholder' => 'Insert City Name')) }}
</div>
{{ Form::submit('Add new City', array('class' => 'btn btn-primary')) }}
{{ Form::close() }}
</section>
And inside my controller here is my code
$rules = array(
'ncty' => 'required|max:100|alpha_num'
);
$messages = array(
'ncty.required' => 'Please enter City Name.',
'ncty.max' => 'City Name can only have a maximum of 100 characters',
'ncty.alpha_num' => 'City Name can only contain alphanumeric characters'
);
$validator = Validator::make(Input::all(), $rules, $messages);
if ($validator->fails())
{
return Redirect::to('lookup_board')
->withErrors($validator)
->withInput(Input::except('password'));
}
else
{
//insert new record
}
Is there a way to toggle the section on my view on the controller?
thanks
you can do it like:
<span class="#if($errors and $errors->has('ncty')) has-error #endif">{{ $errors->first('ncty') }}
I suppose you are using Twitter Bootstrap so it already has a class has-error for errors that you can use.
Replace your opening section tag with the following:
<section id="anCity"{{ ($errors->any() ? '' : ' style="display:none"') }}>

Resources