Ajaxcbcb
Ajaxcbcb

Reputation: 167

Laravel Controller Validation Date

My controller design for my form is to validate the dateline of project and the dateline start date of the project.

Would like to start of with the validation. As the logic goes. Dateline cant be before start date. if possible to return a custom error message.

public function create(){
        $this -> validate($request, [
            'projectName' => 'required|max:255',
            'projectDescrip' => 'required',
        ]);

        if(startDate > Dateline){

        }

        $project = new Project();
        $project -> project_id = $projects -> id;
        $project -> projectName = $projects -> ProjectName;
        $project -> dateline = $projects -> dateline;
        $project -> startDate = $projects -> startDate;
        $project -> user_id = Auth::user()->id;
    }

Upvotes: 1

Views: 2644

Answers (1)

Sam
Sam

Reputation: 2658

There are many ways to validate, but if you're using a form then the best approach is to use Form Request Validation. It makes for cleaner code. But to demonstrate the date validation, in the controller you can:

$rules = [
    'dateline_start' => 'required|date',
    'dateline_finish' => 'required|date|after:dateline_start',
    //Additional rules go here
];

//Custom error messages
$messages = [
    'dateline_finish.after' => 'Dateline cant be before start date',
    //Additional custom error messages
];


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

if ($validator->fails()) {
    return redirect('where/they/should/go')
        ->withErrors($validator)
        ->withInput();
}

That's one way at it.

Upvotes: 3

Related Questions