0

I am working with a form request file like this: ProjectCreateRequest.php

public function rules()
  {
    $project_name = $this->project_name;
    $meta_activity = $this->meta_activity;
    return [
      'project_name' => 'required|max:255|unique:projects',
      'customer_name' => 'required|max:255',
      'otl_project_code' => 'sometimes|max:255|unique:projects,otl_project_code,NULL,id,meta_activity,'.$meta_activity,
      'estimated_start_date' => 'date',
      'estimated_end_date' => 'date',
      'LoE_onshore' => 'numeric',
      'LoE_nearshore' => 'numeric',
      'LoE_offshore' => 'numeric',
      'LoE_contractor' => 'numeric',
      'revenue' => 'numeric',
      'win_ratio' => 'integer'
    ];
  }

There is the otl_project_code that must be unique with the meta_activity.

In case someone enters a pair of otl_project_code and meta_activity that already exists, it goes back to the create page with the error written below.

I would like to get instead that in the controller, I can catch this information, do something on the database then redirect to an update url.

Because I am working with a form validation request file, everything is entered in my controller like this:

public function postFormCreate(ProjectCreateRequest $request)

and I don't know how to catch this specific error in my controller to execute some actions with all the fields I submitted and not go back to the create page. Of course, this needs to happen only when there is the specific error I mentionned above.

1 Answer 1

1

Override the FormRequest response function in your ProjectCreateRequest:

/**
 * Get the proper failed validation response for the request.
 *
 * @param  array  $errors
 * @return \Symfony\Component\HttpFoundation\Response
 */
public function response(array $errors)
{
    if ($this->expectsJson()) {
        return new JsonResponse($errors, 422);
    }

    return $this->redirector->to($this->getRedirectUrl())
                                    ->withInput($this->except($this->dontFlash))
                                    ->withErrors($errors, $this->errorBag);
}

That's the public response on the FormRequest class so you can write your own logic to perform DB queries and redirect where needed.

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.