Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I restore the input field values after validation through custom request objects in Laravel 5.1?

Let's say I have a simple contact form.

<form action="/message" method="post">
    {!! csrf_field() !!}
    <div class="form-group">
        <label>Name: </label>
        <input type="text" name="name" class="form-control">
    </div>

    <div class="form-group">
        <label>Email: </label>
        <input type="email" name="email" class="form-control">
    </div>

    <div class="form-group">
        <label>Your Message: </label>
        <textarea name="message" class="form-control"></textarea>
    </div>

    <div class="form-group">
        <button class="btn btn-primary">Submit Message</button>
    </div>
</form>

Here is my controller to handle that request:

public function sendMessage(ContactRequest $request)
{
    dd($request->all());
}

Notice that I am injecting ContactRequest object, so the validation is working perfectly.

The Problem

How can I restore the old input values in the contact form? So that the user wouldn't have to refill all of the fields.

like image 890
Homo Sapien Avatar asked Jul 09 '15 07:07

Homo Sapien


People also ask

What methods should you implement for your custom validator laravel?

You should add all your validation logic in the passes() function. It should return true or false based on the logic you have written in the function. The message() function returns a string that specifies the error message to be displayed in case the validation fails.

What does validate return in laravel?

XHR Requests & Validation When using the validate method during an XHR request, Laravel will not generate a redirect response. Instead, Laravel generates a JSON response containing all of the validation errors. This JSON response will be sent with a 422 HTTP status code.

What is the method used to configure validation rules in form request laravel?

Laravel Form Request class comes with two default methods auth() and rules() . You can perform any authorization logic in auth() method whether the current user is allowed to request or not. And in rules() method you can write all your validation rule.

What is the method used for specifying custom messages for validation errors in form request?

After checking if the request failed to pass validation, you may use the withErrors method to flash the error messages to the session. When using this method, the $errors variable will automatically be shared with your views after redirection, allowing you to easily display them back to the user.


1 Answers

If the ContactRequest validation fails you will be redirected to your form with the errors and also the old input.

So just use {{ old('field') }} in your blade file.

Example for your code:

<form action="/message" method="post">
{!! csrf_field() !!}
<div class="form-group">
    <label>Name: </label>
    <input type="text" name="name" value="{{ old('name') }}" class="form-control">
</div>

<div class="form-group">
    <label>Email: </label>
    <input type="email" name="email" value="{{ old('email') }}"  class="form-control">
</div>

<div class="form-group">
    <label>Your Message: </label>
    <textarea name="message" class="form-control">{{ old('message') }}"</textarea>
</div>

<div class="form-group">
    <button class="btn btn-primary">Submit Message</button>
</div>

like image 180
igs013 Avatar answered Sep 19 '22 17:09

igs013