I am trying to add custom validation in my controller.
<?php
namespace App\Http\Controllers;
use Auth;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Inertia\Inertia;
use Redirect;
use Response;
use Validator;
class MyController extends Controller
{
public function store(Request $req)
{
$v = Validator::make($req->all(), [
'contract_ref' => 'required'
]);
$v->after(function($v) use(&$req){
dd('custom validations');
// list of custom validations
if ($req->div_id == '') {
$validator->errors()->add('div_id', 'Please select a Division');
};
});
dd('NO!');
if ($v->fails()) {
//
}
$v->validate();
}
}
However, for some reason I don't understand. Nothing in the -after closure is being done. In example above, I get "NO!" dumped instead of the expected "custom validations"
This ->after has worked for me previously and I don't get why it is not working here.
fails(which callspasses) is called here, so theafterclosure wouldn't be called before yourddas validation hasn't happened yet