1

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;
    }
1
  • What's your question? I hope you don't want us to complete your code for you. Commented Feb 15, 2016 at 3:56

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.