Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set form input required attribute in laravel 4

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?

like image 203
GothamCityRises Avatar asked Jun 25 '14 09:06

GothamCityRises


2 Answers

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')) }}
like image 66
GothamCityRises Avatar answered Oct 20 '22 02:10

GothamCityRises


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.

like image 14
thpl Avatar answered Oct 20 '22 01:10

thpl