Laravel 9 Form Validation Example

<?phpuse App\Http\Controllers\UserController;

Route::get('/user/create', [UserController::class, 'create']);
Route::post('/user/store', [UserController::class, 'store']);
<?phpnamespace App\Http\Controllers;use Illuminate\Http\Request;
use App\Models\User;
class UserController extends Controller
{
public function create()
{
return view('index');
}
public function store(Request $request)
{
$validatedData = $request->validate([
'name' => 'required',
'password' => 'required|min:8',
'email' => 'required|email|unique:users'
], [
'name.required' => 'Name is required',
'password.required' => 'Password is required',
'email.required' => 'Email is required'
]);

$validatedData['password'] = bcrypt($validatedData['password']);
$user = User::create($validatedData);

return back()->with('success', 'User Created Successfully !!');
}
}
$validatedData = $request->validate([
'name' => 'required',
'password' => ['required', 'min:8'],
'email' => ['required, email, unique:users'],
]);
$request->validate([
'name' => 'bail|required|unique:users|max:255',
'password' => ['required', 'min:8'],
'email' => ['bail, required, email, unique:users'],
]);
<html>
<head>
<title>Laravel 9 Form Validation Example - Websolutionstuff</title>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha/css/bootstrap.css" rel="stylesheet">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-12" style="margin-top: 30px;">
<h1>Laravel 9 Form Validation Example - Websolutionstuff</h1>
@if(Session::has('success'))
<div class="alert alert-success">
{{ Session::get('success') }}
@php
Session::forget('success');
@endphp
</div>
@endif

<form method="POST" action="{{ url('user/store') }}">
@csrf
<div class="form-group">
<strong>Name:</strong>
<input type="text" name="name" class="form-control" placeholder="Name">
@if ($errors->has('name'))
<span class="text-danger">{{ $errors->first('name') }}</span>
@endif
</div>

<div class="form-group">
<strong>Password:</strong>
<input type="password" name="password" class="form-control" placeholder="Password">
@if ($errors->has('password'))
<span class="text-danger">{{ $errors->first('password') }}</span>
@endif
</div>

<div class="form-group">
<strong>Email:</strong>
<input type="text" name="email" class="form-control" placeholder="Email">
@if ($errors->has('email'))
<span class="text-danger">{{ $errors->first('email') }}</span>
@endif
</div>

<div class="form-group">
<button class="btn btn-info btn-submit">Submit</button>
</div>
</form>
</div>
</div>
</div>
</body>
</html>

--

--

--

I am Laravel and PHP Developer. I have also Good Knowledge of JavaScript, jQuery, Bootstrap and REST API.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Onboarding SWE? Questions to ask in your first week

Getting Started with Git

Who the fuck is Peter?

Software code written on a laptop

Unity Development — Camera Follow

Apache Cassandra, Part 5: Data Modelling with Examples

Generasi Gigih Intermediate Level, Week 2 Reflection

Why GraphQL Connection types don’t work well with Apollo cache

VIPER Project Template in Xcode 9: When module template is not enough…

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Websolutionstuff

Websolutionstuff

I am Laravel and PHP Developer. I have also Good Knowledge of JavaScript, jQuery, Bootstrap and REST API.

More from Medium

Laravel Eloquent Relationships

Create Custom Files In Laravel Using Artisan Commands

Laravel RESTful API — MarleyBlog: Introduction

Define environment variables in Laravel