Laravel form Validation
output
Steps To Follow👇
.env file
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=forms
DB_USERNAME=root
DB_PASSWORD=toor
resources/views/layouts/app.blade.php
<!DOCTYPE html>
<html>
<head>
<title>My Form - @yield('title')</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
</head>
<body>
@section('sidebar')
<h2 class="text-center bg-success">This is the master sidebar.</h2>
@show
<div class="container">
@yield('content')
</div>
</body>
</html>
@extends('layouts.app')
@section('title','my first form')
@section('sidebar')
@parent
<div class="navbar ">
<p class="glyphicon-align-middle text-center text-primary">Hi this sde bar...Look below</p>
</div>
@endsection
@section('content')
<div class="form-horizontal " >
{{-- this will display error message if any error found--}}
@if ($errors->any())
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
{{-- this is form--}}
<form action={{ url('/form') }} method="post">
@csrf
<div class="form-group">
<labale for="c_name">Name</labale>
<input type="text" name="c_name" class="form-control @error('c_name') is-invalid @enderror"
placeholder="please enter name here"><br>
{{-- this will run if user inputs a invalid data--}}
@error('c_name')
<div class="alert alert-danger">{{ $message }}</div>
@enderror
</div>
<div class="form-group">
<labale for="c_address">address</labale>
<input type="text" id="c_province" name="c_address"
class="form-control @error('c_address') is-invalid @enderror"
placeholder="please enter address"><br>
{{-- this will run if user inputs a invalid data--}}
@error('c_address')
<div class="alert alert-danger">{{ $message }}</div>
@enderror
</div>
<div class="form-group">
<labale for="c_province">province</labale>
<input type="text" id="c_province" name="c_province" class="form-control"
placeholder="please enter the province"><br>
{{-- this will run if user inputs a invalid data--}}
@error('c_province')
<div class="alert alert-danger">{{ $message }}</div>
@enderror
</div>
<div class="form-group">
<input type="submit" value="Enter!" name="sub_btn" class="btn btn-default">
</div>
</form>
</div>
@endsection
app/Http/controllers/MyController.php
<?php
namespace App\Http\Controllers;
use App\Models\Myform;
use Illuminate\Http\Request;
class MyController extends Controller
{
//this is for show the form
public function show_form(){
return view('form1');
}
// this method/function for save data
public function save_data(Request $request){
//this is validation of input data set
$this->validate($request,[
'c_name'=>'required',
'c_address'=>'required',
'c_province'=>'required'
]);
//this is for insert data in to database
Myform::create($request->all());
return back();
}
}
routes/web.php
<?php
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| 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('/', function () {
return view('welcome');
});
//routes for form
Route::get('/form','App\Http\Controllers\MyController@show_form');
Route::post('/form','App\Http\Controllers\MyController@save_data');
Thank you..
No comments:
Post a Comment