Form Validation Not work

Posted 6 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.

Controller

<?php

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()){

           $validation->errors()->first();
        }
        $request = RoleType::create([
            'roleName'=> $request->input('txtRole'),
            'created_by'=> Auth::user()->id
        ]);

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




}

View File

@include('admin.includes.header')
@include('admin.includes.top_header')
@include('admin.includes.left_nav')


<!-- Content Wrapper. Contains page content -->
<div class="content-wrapper">
    <!-- Content Header (Page header) -->
    <section class="content-header">
        <h1>
            Dashboard
            {{--<small>Optional description</small>--}}
        </h1>
        {{--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') }}
        </div>
        @endif
    </section>

    <!-- 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>
                </div>
                <!-- /.box-header -->
                <!-- form start -->
                <form class="form-horizontal" action="{{URL::to('/store')}}" method="post">
                    @csrf
                    <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">

                            </div>
                        </div>
                    </div>
                    <!-- /.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>
                    </div>
                    <!-- /.box-footer -->
                </form>
            </div>
            <!-- /.box -->
        </div>
        <div class="col-md-3">
            <div>
                @foreach($errors->all() as $error)
                    <span class="text-danger">{{$error}}</span><br>
                @endforeach
            </div>
        </div>
    </section>
    <!-- /.content -->

</div>
<!-- /.content-wrapper -->
@include('admin.includes.footer')

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

Reply to

Use Markdown with GitHub-flavored code blocks.