Form Validation Not work

Posted 8 months ago by sanjayacloud

I have create a simple form with validation. But my validation not work. my code like below. can you help me to set this validation work.



namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\URL;
use App\RoleType;
use Illuminate\Support\Facades\Auth;

class RoleTypeController extends Controller
    public function __construct()


    public function insertRole()
        return view('admin/role/add_role');

    public function store(Request $request)

        $validation = Validator::make($request->all(),[
           'role'=> 'required',

        if ($validation->fails()){

        $request = RoleType::create([
            'roleName'=> $request->input('txtRole'),
            'created_by'=> Auth::user()->id

        return redirect()->back()->with('success', 'Record has been added!');


View File


<!-- Content Wrapper. Contains page content -->
<div class="content-wrapper">
    <!-- Content Header (Page header) -->
    <section class="content-header">
            {{--<small>Optional description</small>--}}
        {{--breadcrumb if need
        <ol class="breadcrumb">
            <li><a href="#"><i class="fa fa-dashboard"></i> Level</a></li>
            <li class="active">Here</li>
        </ol> --}}
        @if (session()->has('success'))
        <div class="alert alert-success alert-dismissible">
            <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
            <h4><i class="icon fa fa-check"></i> Success Massage!</h4>
            {{ session('success') }}

    <!-- Main content -->
    <section class="content container-fluid">
        <div class="col-md-9">
            <!-- Horizontal Form -->
            <div class="box box-info">
                <div class="box-header with-border">
                    <h3 class="box-title">Add New Role Type</h3>
                <!-- /.box-header -->
                <!-- form start -->
                <form class="form-horizontal" action="{{URL::to('/store')}}" method="post">
                    <div class="box-body">
                        <div class="form-group">
                            <label for="inputEmail3" class="col-sm-2 control-label">Input New Role</label>

                            <div class="col-sm-10">
                                <input type="text" class="form-control" id="role" placeholder="Enter New Role" name="txtRole">

                    <!-- /.box-body -->
                    <div class="box-footer">
                        <button type="reset" class="btn btn-default">Cancel</button>
                        <button type="submit" class="btn btn-info pull-right">Insert</button>
                    <!-- /.box-footer -->
            <!-- /.box -->
        <div class="col-md-3">
                @foreach($errors->all() as $error)
                    <span class="text-danger">{{$error}}</span><br>
    <!-- /.content -->

<!-- /.content-wrapper -->

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.