Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

shahzadyounas's avatar

"Date" Field Validation Error

i have two question.

  1. I put data in both field "Department Name" and "Starting Date" but validation message occur that "The depjdate field is required." Snapshot as following https://prnt.sc/jqhkqs
  2. as you showing in picture the validation show message "The depjdate field is required." but i want to show the message "The Starting Date field is required." addDepartment.blade.php
<form method="POST" action="{{ route('department.store') }}">
  @csrf

<div class="card-body row">

   <div class="col-lg-6 p-t-20"> 
    <div class = "mdl-textfield mdl-js-textfield mdl-textfield--floating-label txt-full-width">
          <input class = "mdl-textfield__input{{ $errors->has('name') ? ' is-invalid' : '' }}" 
                   name="departmentname" type = "text" id = "txtDepName" value="{{ 
                    old('departmentname') }}">
         @if ($errors->has('departmentname'))
                                    <span class="invalid-feedback">
                                        <strong>{{ $errors->first('departmentname') }}</strong>
                                     </span>
                                   @endif
    <label class = "mdl-textfield__label">Department Name</label>
         </div>
      </div>
                                     

                                        


                                     
<div class="col-lg-6 p-t-20"> 
       <div class = "mdl-textfield mdl-js-textfield mdl-textfield--floating-label txt-full-width">
              <input class = "mdl-textfield__input{{ $errors->has('depstartdate') ? ' is-invalid' : '' }}" name="depstartdate" type = "text" id = "date" value="{{ old('depstartdate') }}" >
                                               
@if ($errors->has('depstartdate'))
                                    <span class="invalid-feedback">
                                        <strong>{{ $errors->first('depstartdate') }}</strong>
                                    </span>
                                @endif


          <label class = "mdl-textfield__label" >Department Start Date</label>
           </div>
      </div>



        @if ($errors->any())
   <div class="col-md-12 alert alert-danger">
       <ul>
           @foreach ($errors->all() as $error)
               <li>{{ $error }}</li>
           @endforeach
       </ul>
   </div>
 @endif
                                       
                                        
   <div class="col-lg-12 p-t-20 text-center"> 
            <button type="submit" class="mdl-button mdl-js-button mdl-button--raised mdl-js-ripple-effect m-b-10 m-r-20 btn-pink">Submit</button>
            <button type="reset" class="mdl-button mdl-js-button mdl-button--raised mdl-js-ripple-effect m-b-10 btn-default">Reset</button>
      </div>
 </form>

The DepartmentController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Department;

class DepartmentController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $depart= Department::all();
        return view('admin.allDepartment', compact('depart'));

    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.adddepartment');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $this->validate($request, [
        'departmentname' => 'required',
        'depjdate' => 'required',
        ]);

        $s = new Department();
        $s->departmentname = $request->departmentname;
        $s->depstartdate = $request->depstartdate;
       
        $s->save();
        return redirect("department");
    }


}


And The table code as following:

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class AddDepartmentToTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
         Schema::create('department', function (Blueprint $table) {

            $table->increments('depid');
            $table->string('departmentname')->unique();
            $table->date('depstartdate');
            $table->rememberToken();
            $table->timestamps();
            });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('department', function (Blueprint $table) {
    
        });
    }
}
0 likes
12 replies
yusuf128's avatar

you can set the custom validation message like

$messages = array( 'depjdate.required' => 'The Starting Date field is required.', );

$validator = Validator::make($input, $rules, $messages);

2 likes
Cronix's avatar
Cronix
Best Answer
Level 67

In store() method

$this->validate($request, [
    'departmentname' => 'required',
    'depjdate' => 'required',
],
[
    'depjdate.required' => 'The Starting Date field is required.',
]);
1 like
shahzadyounas's avatar

what is answer of 1st question,,, i put date in field but validation occur that enter date ??? why

Snapey's avatar

You don't have depjdate on your form? :-/

1 like
shahzadyounas's avatar

if i enter duplicate data then how to validate it in store function

Snapey's avatar

duplicate in what way?

You mean that the value should be unique in the database?

shahzadyounas's avatar

when i enter duplicate data then it show laravel error

"SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'Computer Science' for key 'department_departmentname_unique' (SQL: insert into `department` (`departmentname`, `depstartdate`, `updated_at`, `created_at`) values (Computer Science, 04-06-2018, 2018-06-04 07:13:15, 2018-06-04 07:13:15))

but i want to show alert msg

shahzadyounas's avatar

@Cronix you answer is right bit when i use


 'departmentname' => 'required|unique:department|max:255',

the it show alert "The departmentname has already been taken." but i want to show "The Department Name has already exist"

Snapey's avatar

Please read the documentation. Its all there under validation.

Cronix already showed you how to add custom messages

$this->validate($request, [
    'departmentname' => 'required|unique:department|max:255',
    'depjdate' => 'required',
],
[
    'depjdate.required' => 'The Starting Date field is required.',
    'departmentname.unique' => 'The Department Name has already exist',
]);

Please or to participate in this conversation.