MYHOSPITALNOW.COM

Find Trusted Doctors & Hospitals

Discover healthcare providers, compare hospitals, and explore treatment options worldwide.

Free Treatment Quotes • Doctor Search • Hospital Comparison

Find Healthcare Options

www.myhospitalnow.com

Request Validation using ValidatesRequest Trait in Laravel.

Q1. What is Validation?

Validation is used to protect or validate the request we’re getting from the user. Suppose we make a form and we’ve not used validation in the form so when a user submits a blank form then it might give error or exception and also we get blank data from the submitted form, so we use validation to protect the request. For example, we’ve added a field to get an email of the users and we want to get unique email id whenever user fill the field, so we use email validation in our page to protect the request.

Q2. How to Validate the Request?

To validate the request, we use a form to get input and validate it. At first, we make a simple HTML form to get input and it is a view file so we name it form.blade.php. See the below code of the form:-

In the form, we used an available object of message bank called error to pass the message in the form. It is predefined in all the view file. After that, we used the csrf token in the form and then our form code. In the code we also used value as old. why ?. To preserve the value we filled in the given field we use value as old. When we press the submit button then it calls the submitmyform page to give output.

To set the customise message we pass it as a 3rd parameter.

Now, we make function of our form as well as submitmyform in our controller, see below:-

Laravel controller code using $this->validate to require name, email and age, with a custom name message

Now we have to make the Route. But careful when making the route of submitmyform because the method is post for submitmyform so we write post in place of get. See the route below:-

Route::get(“myform”, “ProductController@myform”);
Route::post(“submitmyform”, “ProductController@submitmyform”);

Now See the output whether it returns a message when we did not provide value in the given field.

Empty form submitted, showing required-field errors for name, email and age

Now, When we fill 1-2 field and left other then what message returns. See below:-

Form with age left blank returns The age field is required and keeps the filled values

When we fill all the fields, then what returns:-

All form fields filled and submitted, so submitmyform shows Form Submitted

If you want to get valid Email then add the below code in the controller. See below:-

Laravel submitmyform validate code with email rule required|email|min:8 and custom Enter a Valid E-mail message

See when we provide invalid email then what message appears:-

Laravel form submitted with invalid email dgdcmkduid returns the Enter a Valid E-mail message

Find Trusted Cardiac Hospitals

Compare heart hospitals by city and services — all in one place.

Explore Hospitals
With MotoShare.in, you can book a bike instantly, enjoy doorstep delivery, and ride without worries. Perfect for travelers, professionals, and adventure enthusiasts looking for a seamless mobility solution.

Related Posts

[SOLVED] ErrorException preg_match(): No ending delimiter ‘/’ found

I’m working on Laravel 5.8. I’m trying to use Validator to validate a name field with regex, here is my rule below:   But as soon as…

Read More

Create Laravel multiple upload images and Displaying on view

Today , We will build a simple multiple images upload system using Laravel 5.8. We will use a jQuery plugin to populate image field and submit the multiple images to…

Read More

Special Characters Not Allowed Validation in Laravel 5.8

 let’s see example of laravel validation no special characters. Example -1 use this code for validation:-  ‘name’ =>  ‘required|regex:/^[a-zA-Z]+$/u’ Example:2- Use this code for validation:- ‘applyjobsforuser’ => ‘required|unique:applyjobs,applyjobsforuser’, Example-3 use this code…

Read More

Request Validation in Laravel.

Request Validation using ValidatesRequest Trait in Laravel. (Click to Open) Request Validation using Form Request in Laravel. (Click to Open) Request Validation using Custom Validator in Laravel….

Read More

How to Create a Simple Login Authentication System in LARAVEL?Part-2

How to Create a Simple Login Authentication System in LARAVEL? Firstly, Visit this Blog Part-1. Step 15. Now add these classes within MainController file. Step 16. Create…

Read More

[SOLVED] Laravel Error: Integrity constraint violation: 1062 Duplicate entry

This Error is found when we insert duplicate data which already exist in the database. To overcome from it just make the key_name unique in Validation. See…

Read More