Add a Form Validation in Laravel 9 | use of {{old('name')}}
on
Get link
Facebook
X
Pinterest
Email
Other Apps
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.
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 '
';
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
Post a Comment