I'm building a small validator for my form with laravel 7 and i needed a custom logic to be added after the standard validation so I used the after validation hook as in doc. My issue is not with the logic of the controller but the fact that after the validation occur and I add the custom error message i don't get redirected to the original form. What I'm missing?
public function store(Request $request)
{
//
$data = $request->all();
$newSpn = new Sponsorization;
$userId = Auth::id();
$paymentPlanId = $data["payment_plan_id"];
$apartmentId = $data["apartment_id"];
$payPlanInfo = PaymentPlan::find($paymentPlanId)->hours_duration;
$alreadyActive = $this->alreadyActive($apartmentId);
$userApartment = DB::table('apartments')
->where('user_id', $userId)
->pluck('id');
$validator = Validator::make($request->all(),[
'payment_plan_id' => "required",
'appartment_id' => [
'required',
Rule::in($userApartment)
]
]);
$validator->after(function ($validator) use ($alreadyActive){
if ($alreadyActive) {
$validator->errors()->add('apartment_promo', 'A promo is already active on this apartment!');
}
});
if ($validator->fails()) {
//
}
$newSpn->apartment_id = $apartmentId;
$newSpn->payment_plan_id = $paymentPlanId;
$newSpn->start_date = date("Y-m-d H:m:s");
$newSpn->end_date = date("Y-m-d H:m:s",strtotime("+{$payPlanInfo} hours"));
$newSpn->save();
}