Textarea with Laravel Collective - laravel

Hello friends I need make a Textarea with Laravel Collective, regulary I use:
<textarea id="txt" name="txt" maxlength="1900" class="form-control" rows=1" onkeypress="return nameFunction(event);">My text</textarea>
How to create a textarea wiht Laravel Collective?

Try like this:
Form::textarea('My text', null, [
'class' => 'form-control',
'rows' => 1,
'name' => 'txt',
'id' => 'txt',
'onkeypress' => "return nameFunction(event);"
])

Related

How to resolve Array to String conversion error Laravel

I'm new to laravel and i encountered an array to string conversion error while trying to send tags select form data to sync with my blogs table.
Below is blade snippet that retrieves tags from the database
<div class="form-group">
<label for="tag" class="control-label">Tags</label>
{!! Form::select('tag[]', $tags, old('tag'), ['class' => 'form-control select2', 'multiple' => 'multiple', 'id' => 'add-tag' ]) !!}
</div>
The BlogsController
$blog_data = request()->validate([
'tag.*' => 'required'
]);
blogs = Blog::create( $blog_data );
$blogs->tags()->sync((array)request()->input('tag'));
when i perform a dd on request()->tag
array:2 [▼
0 => "1"
1 => "2"
]
Just use like this
$blogs = Blog::create( $blog_data );
If insert then use like this
$blogs->tags()->attach($request->tag);
If update then use like this
$blogs->tags()->sync($request->tag);
Thank you, i was able to remove this line of code and it worked
tag.*' => 'required'

How to show multiple selected values of selectbox in Laravel 5.3?

I want to show multiple selected values in laravel5.3 form
{!! Form::select('category[]', $categories['all_cat']['categories'], null,['multiple' => 'multiple'], ['class' => 'form-control', 'id' => 'category']) !!}
Try with this:
{!! Form::select('category[]', $categories['all_cat']['categories'], null, ['multiple' => true, 'class' => 'form-control', 'id' => 'category']) !!}

Is there any way to prevent input type=“number” getting negative values?

How can I prevent the numbers which was given as input in the form, not to go as negative value?
Here is my code:
{!! Form::input('number', 'mobile', null, array('id' => 'mobile', 'class' => 'input-lg form-control TabOnEnter', 'placeholder' => 'Eg: 9876543210', 'tabindex' => 15)) !!}
Use the min attribute like this:
<input type="number" min="0">
In Laravel:
{!! Form::input('number', 'mobile', null, ['type' => 'number', 'min' => 0, 'id' => ....]) !!}
Use type="number" and min=0:
<input type="number" min="0">
Or:
{!! Form::input('number', 'mobile', null, ['type' => 'number', 'min' => 0, 'id' => ....]) !!}
You can also try Form::number:
{!! Form::number('number', 'mobile', ['min' =>0, 'id' => ....]) !!}
You can use server side validation provided by Laravel Validator class.
https://laravel.com/docs/5.3/validation#rule-min

Laravel 5 required_if validation for radio buttons

The laravel required_if validation doesn't seem to work when you have radio buttons.
I have the following rules:
'method' => 'required|in:Email,Url',
'email' => 'required_if:method,Email'|'email',
'url' => 'required_if:method,Url'|'url',
In my form I have the following:
{!! Form::radio('method', 'Email', true ) !!}
{!! Form::radio('method', 'Url', false ) !!}
{!! Form::text('email', null, ['maxlength' => '255', 'class' => 'form-control']) !!}
{!! Form::text('url', null, ['maxlength' => '1000', 'class' => 'form-control']) !!}
But the validation doesn't seem to fire?
My mistake - added extra apostrophes to the following:
'email' => 'required_if:method,Email'|'email',
'url' => 'required_if:method,Url'|'url',
Should be:
'email' => 'required_if:method,Email|email',
'url' => 'required_if:method,Url|url',

codeigniter - input form placeholder

Hey all, how can I use the placeholder tag in CodeIgniter's form_input() helper function?
Thanks :)
Do you mean the placeholder attribute (not tag)? form_input() takes a third parameter with additional attributes.
$opts = 'placeholder="Username"';
form_input('username', '', $opts);
Or you can pass form_input() an array.
form_input(array(
'name' => 'username',
'value' => '',
'placeholder' => 'Username',
));
CodeIgniter Form Helper
The Codeigniter user guide linked to by Rocket indicates that attributes other than name and value are passed to form_input() as an array:
$data = array(
'name' => 'username',
'id' => 'username',
'value' => 'johndoe',
'maxlength' => '100',
);
echo form_input($data);
// Would produce:
<input type="text" name="username" id="username" value="johndoe" maxlength="100" size="50" />
To expand on Rocket's answer, passing 'placeholder'=>'my_placeholder' into that array should produce the placeholder attribute.
$data = array(
'name' => 'username',
'id' => 'username',
'value' => 'johndoe',
'maxlength' => '100',
'size' => '50',
'style' => 'width:50%',
'placeholder' => 'my_placeholder'
);
echo form_input($data);
Keep in mind, the placeholder attr is very new and not supported in all browsers. Check out this article at html center for html5, jQuery, and pure javascript ways to accomplish placeholders
Codeigniter form placeholder for IE6, IE7 and IE8
echo form_input(array(
'name' => 'stackoverflow',
'value' => 'yourplaceholder',
'placeholder' => 'yourplaceholder',
'onclick' => 'if(this.value == \'yourplaceholder\') this.value = \'\'', //IE6 IE7 IE8
'onblur' => 'if(this.value == \'\') this.value = \'yourplaceholder\'' //IE6 IE7 IE8
));
you can set placeholder like this
echo form_input('username','','placeholder=username');
this will look like this
<input type='text' name='username' placeholder='username'/>

Resources