What is validation in Laravel? Explained from the perspective of a complete beginner by a junior engineer

Introduction

Hello! My name is Mirai, and I'm a new engineer who joined the company in 2026 with a liberal arts background and no prior experience. After completing a three-month training program, I've been working in the Systems Development Department since July

One of the things that impressed me during the training was the validation system

At first, based on the sound of the name, I thought it was something that would block something, like a barrier. In reality, the word is not derived from barrier, but from validate , and what it does is "verify whether the incoming value is correct or not."

This article is written from the perspective of a complete beginner

I hope this will be helpful to those who are also aiming to become engineers, or those who are feeling uncertain during their training!

Programming language/framework used: PHP 8.3 / Laravel 11

Reference:https://laravel.com/docs/11.x/validation

What exactly is validation?

Validation is the process of checking whether the entered value is correct .

In Japanese, this is called "verification" or "input check."

Have you ever had an experience like this in your daily life?

  • When registering as a member, I left the email address field blank and submitted it, but it displayed "This is a required field" and I couldn't proceed
  • If you enter your name incorrectly in the phone number field, the message "Please enter using numbers" will appear

That "mechanism that prevents you from moving forward and gets stuck" is precisely validation!

"Why bother checking it?"

① Prevents the insertion of inappropriate data into the database

Data with an empty title, or data with "aaaa" in the URL field. Even one such entry can cause the list view to break or prevent links from working when clicked.Stopping them at the entry point eliminates the need to fix them later.

② Security reasons

A form is "an entry point where anyone can freely submit text." There's a possibility that malicious individuals might submit inappropriate data. Validation acts like a gatekeeper standing at that entry point

③ You can write subsequent processing with confidence

Since the data after validation is "checked and clean data," you don't have to worry about "what if it's empty ... " or "what if something completely different is entered..." in subsequent processing . The code becomes simpler and readability improves!


How do you write it?

Here is the code I actually wrote when I tried creating my own tool

public function store(Request $request) { // Validate the input and save one link to the currently logged-in user's links Auth::user()->links()->create($request->validate([ 'title' => 'required', 'url' => 'required|url', 'memo' => 'nullable', ])); // Redirect to the list return redirect()->route('links.index');] }

Although it's only a few lines, in this..

  1. Input validation
  2. keep
  3. Screen transition

These three things are running

I'll explain them in order!

The code aboveexactly the same meaning as the code below, just written differently .

public function store(Request $request) { // First, validate. Put the checked data into a variable $targetData = $request->validate([ 'title' => 'required', 'url' => 'required|url', 'memo' => 'nullable', ]); // Save one link to the currently logged-in user Auth::user()->links()->create($targetData); // Redirect to the list return redirect()->route('links.index'); }

For beginners,I recommend starting with this "divided version" to understand it better . I also tried reading it in separate parts, and gradually I became able to read it.

The variable name $validated means "validated" and is a commonly used name in Laravel's official documentation and in actual code.


Decipher the code line by line

For those who still don't quite understand, let's go through it line by line!

$request->validate([...])

We will validate the data received from the form. The rules are written as follows:

item rule meaning
title required This must be filled in
URL Required | URL It is required and must be in URL format
memo Nullable It's OK even if it's empty

You can specify multiple rules simultaneously by separating them with a pipe symbol (| ). `required|url` means "required AND in URL format".

If thisrule is violated, Laravel will automatically return you to the input screen!

Auth::user()->links()->create()

This is easier to understand if you break it down into three parts

  • Auth::user() ... The currently logged-in user
  • ->links() … A collection of links owned by that user
  • ->create() … Creates a new data entry there.

When read together, it means "Create a new link in the link collection of the person currently logged in."

Once I realized it was like reading an English text, what used to look like a code suddenly became much easier to understand

return redirect()->route('links.index')

Once saving is complete, you will be redirected to the list screen ( links.index )


Why does validation run first?

This is the part that initially confused me the most

Auth::user()->links()->create($request->validate([...]));

this codefrom left to right, `create()` first . But in reality, it works in the order of "validate first, then save."

Why is that?

Answer: Because the outer part cannot be executed until the contents inside the parentheses are finalized

Try to recall the math you learned in elementary school

2 × (3 + 4) = 14

In this case, we calculate first (3 + 4) the part is . We calculate the part inside the parentheses to get 7 , and then we perform 2 ×

Programming is exactly the same

create( $request->validate([...]) ) └── Contents of the parentheses ──┘

To execute `create()` , the contents to be passed must first be finalized. That's why `validate()` inside the parentheses runs first, and only after the result is output does `create()` run with that result.

Commonly Used Validation Rules

In addition to the three rules we used this time, I'll introduce a few other commonly used rules

rule meaning When to use it
required Required field Name, title, etc
Nullable It's OK even if it's empty Notes, remarks, etc
URL URL format URL
email Email address format Membership registration, etc
Integer Being an integer Quantity, age
max:255 Up to 255 characters Text, notes
min:8 8 characters or more password

For example, if you want to set a character limit of 255 characters for the title, you can write it like this

'title' => 'required|max:255',

summary

Finally, let's review the key points of this article

  • Validation is a mechanism that checks whether the entered values ​​are correct. It keeps data clean, protects security, and simplifies subsequent processing.
  • `$request->validate()` function automatically returns you to the input screen if it fails, and returns a checked array if it succeeds.
  • The execution order of code is "what's inside the parentheses comes first." It's the same rule as in arithmetic calculations.

Things I want to tell those of you who aspire to become engineers

When I first saw the code, it really looked like nothing but a secret code

But in reality, you can think in the same way you learned in school, such as the order of parentheses in arithmetic or word order in English

To all new graduate engineers and those aspiring to become engineers, I hope this will be helpful for anyone starting from scratch like me!

Thank you for reading this far!

If you found this article helpful,please give it a "Like"!
3
3
10
X Facebook Hatena Bookmark pocket

The person who wrote this article

About the author