How To Use Form With Blade Template Layout In Laravel

admin_img Posted By Bajarangi soft , Posted On 23-09-2020

Forms are one of the most needed parts of working with html and Web Development. We need html forms to collect user input in an accurate and useful way. in this article we discuss how can we use form in blade file in laravel

How to use Form with Blade Templates in Laravel

Opening A Form

{{ Form::open(array('url' => 'foo/bar')) }}
//
{{ Form::close() }}


CSRF Field

Anytime you define an HTML form in your application, you should include a hidden CSRF token field in the form so that the CSRF protection middleware can validate the request. You may use the @csrf Blade directive to generate the token field:

<form method="POST" action="/profile">
    @csrf
    ...
</form>

echo Form::token();

Attaching The CSRF Filter To A Route

Route::post('profile', array('before' => 'csrf', function()
{
    //
}));


Method Field

Since HTML forms can't make PUTPATCH, or DELETE requests, you will need to add a hidden _method field to spoof these HTTP verbs. The @method Blade directive can create this field for you:

<form action="/foo/bar" method="POST">
    @method('PUT')
    ...
</form>

echo Form::open(array('url' => 'foo/bar', 'method' => 'put'))


Validation Errors

The @error directive may be used to quickly check if validation error messages exist for a given attribute. Within an @error directive, you may echo the $message variable to display the error message:

<!-- /resources/views/post/create.blade.php -->

<label for="title">Post Title</label>

<input id="title" type="text" class="@error('title') is-invalid @enderror">

@error('title')
<div class="alert alert-danger">{{ $message }}</div>
@enderror

You may pass the name of a specific error bag as the second parameter to the @error directive to retrieve validation error messages on pages containing multiple forms:

<!-- /resources/views/auth.blade.php -->

<label for="email">Email address</label>

<input id="email" type="email" class="@error('email', 'login') is-invalid @enderror">

@error('email', 'login')
<div class="alert alert-danger">{{ $message }}</div>
@enderror
 


Form Model Binding:form based on the contents of a model. To do so, use the Form::model method:

echo Form::model($user, array('route' => array('user.update', $user->id)))


Labels 

Generating A Label Element
echo Form::label('email', 'E-Mail Address');

Specifying Extra HTML Attributes
echo Form::label('email', 'E-Mail Address', array('class' => 'myclass'));

 

Text, Text Area, Password & Hidden Fields

Generating A Text Input
echo Form::text('username');

Specifying A Default Value
echo Form::text('email', 'example@gmail.com');

Generating A Password Input
echo Form::password('password');

Generating Other Inputs
echo Form::email($name, $value = null, $attributes = array());
echo Form::file($name, $attributes = array());


Checkboxes and Radio Buttons

Generating A Checkbox Or Radio Input
echo Form::checkbox('name', 'value');

echo Form::radio('name', 'value');

Generating A Checkbox Or Radio Input That Is Checked
echo Form::checkbox('name', 'value', true);

echo Form::radio('name', 'value', true);


Number

Generating A Number Input
echo Form::number('name', 'value');


File Input

Generating A File Input
echo Form::file('image');


Drop-Down Lists

Generating A Drop-Down List
echo Form::select('size', array('L' => 'Large', 'S' => 'Small'));

Generating A Drop-Down List With Selected Default
echo Form::select('size', array('L' => 'Large', 'S' => 'Small'), 'S');

Generating A Grouped List
echo Form::select('animal', array(
'Cats' => array('leopard' => 'Leopard'),
'Dogs' => array('spaniel' => 'Spaniel'),
));

Generating A Drop-Down List With A Range
echo Form::selectRange('number', 10, 20);

Generating A List With Month Names
echo Form::selectMonth('month');


Buttons

Generating A Submit Button
echo Form::submit('Click Me!');


Custom MacrosRegistering A Form Macro

Form::macro('myField', function()
{
return '<input type="awesome">';
});

echo Form::myField();

Related Post