How to set form input required attribute in laravel 4
Asked Answered
B

4

19

I'm using the laravel framework for a project, and I'm implementing a basic form page, where I require certain values to be required, something that can be done very easily in HTML5.

<input type="text" name="abc" required>

In laravel, without the required attribute, the same would be :

{{ Form::text('abc') }}

How do I incorporate a required attribute in the above statement?

Brentonbrentt answered 25/6, 2014 at 9:55 Comment(0)
B
26

Since simply writing ['required'] did not work, I searched online a bit more and found the answer, so I thought I'd share it here.

The third parameter is an array of optional attributes, which, in convention, must be written as:

{{ Form::text('abc','',array('required' => 'required')) }}

Similarly, for a radio button with default selected/checked we have:

{{ Form::radio('abc', 'yes', array('checked' => 'checked')) }}
Brentonbrentt answered 26/6, 2014 at 20:4 Comment(3)
Why is 'required' => 'required' needed? Will just require not work?Noonberg
@Noonberg From an html perspective, "required" on its own is fine, but this function needs a key => value array pair to workVirelay
@JonRhoades If you check out Laravel's HtmlBuilder class, and specifically the attributeElement method, you will see that you are mistaken. If a key is numeric, the key will be set to the value. So ['required'] in effect is the same as ['required' => 'required'].Monseigneur
E
14

Check out the API-Docs. The method signature shows that you can provide 3 parameters.

The first one is the name attribute, the second one is the value attribute. Third one is your array with any additional attributes.

So just call your method with:

{{ Form::text('key', 'value', ['required']) }}

And a required attribute will be attached to your input field.

Encephalography answered 25/6, 2014 at 10:1 Comment(0)
M
7

I believe the correct answer is similar to the other post where the third parameter is

array('required' => 'required')

however to get the attribute without any value you can do the following:

array('required' => '')

The input field (for text example), will then look what was necessary in the question.

Laravel Example:

{{ Form::text('title', '', array('tabindex' => '1', 'required' => '')) }}

HTML output:

<input tabindex="1" required name="title" type="text" value="" id="title">

I believe this actually shorthand for required='', just wanted to add this note

Medullated answered 19/12, 2014 at 15:38 Comment(2)
Excellent on L4.2Ament
Just a note for anyone finding this and looking to add 'required' to a file input field - rather than the third parameter, for file inputs required should be the second parameter (in 5.4 at least, not tested in others). So for a required file input you'd create your field as below {!! Form::file('thumbnail', ['required']) !!}Alexiaalexin
R
2

Radio required featured works with laravel 5.7 version

@foreach($status_list as $status_key => $status)
  {!! Form::radio('status', $status_key, false, array('id'=>'status_'.$status_key, 'required'=>'required' )); !!}
  {!! Form::label('status_'.$status_key, $status ) !!}
@endforeach

I hope, this will help you also. :)

Rockwell answered 19/1, 2019 at 12:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.