Add a Form Validation in Laravel 9 | use of {{old('name')}}

 Add a Form Validation in Laravel 

The form validation code available in lang>en>validation.php in laravel.

You can use any validation from these all.

While using validation you have to target name attributes inside input elements.

<form action="{{url('/')}}/register" method="post">
            @csrf
            <div class="mb-3">
              <label for="name" class="form-label">Name</label>
              <input type="text" class="form-control" name="name" id="name" 
        aria-describedby="emailHelpId" placeholder="Name" value="{{old('name')}}">
              <span class="text-danger">
                @error('name')
                    {{$message}}
                @enderror
              </span>
            </div>
            <div class="mb-3">
              <label for="email" class="form-label">Email</label>
              <input type="email" class="form-control" name="email" id="email" 
            aria-describedby="emailHelpId" placeholder="abc@mail.com">
              <span class="text-danger">
                @error('email')
                    {{$message}}
                @enderror
              </span>
            </div>
            <div class="mb-3">
              <label for="password" class="form-label">Password</label>
              <input type="password" class="form-control" name="password" 
        id="password" aria-describedby="emailHelpId" placeholder="Password">
              <span class="text-danger">
                @error('password')
                    {{$message}}
                @enderror
              </span>
            </div>
            <div class="mb-3">
              <label for="password" class="form-label">Confirm Password</label>
              <input type="password" class="form-control" name="confirm_password" 
        id="password" aria-describedby="emailHelpId" placeholder="Confirm Password">
              <span class="text-danger">
                @error('confirm_password')
                    {{$message}}
                @enderror
              </span>
            </div>
            <button class="btn btn-primary">Submit</button>
        </form>

First of all you have to create form like above. If you don't idea about above form Follow these steps.

{{old('name')}} is use for retain the same value if form submission gets faild.

Do not need to fill these details again.

Above all errors comming from Apps>Http>Controllers>registrationController.php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class registrationController extends Controller
{
    public function index(){
        return view('form');
    }

    public function register(Request $request){
        $request->validate(
            [
                'name' => 'required',
                'email' => 'required|email',
                'password' => 'required',
                'confirm_password' => 'required|same:password'
            ]
        );
        echo '<pre>';
        print_r($request->all());
    }
}

Now go to Routes>web.php and create get and post functions -

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\registrationController;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/register', [registrationController::class, 'index']);
Route::post('/register', [registrationController::class, 'register']);

Now you can try http://127.0.0.1:8000/register on brower if you have started your localhost. if not started then - follow these steps.

Comments