so lets say i have 2 functions , i send a ajax request to init
function and init function calls check
function
function init(Request $request){
$this->check($request);
}
function check(Request $request){
$request->validate(['something' => 'required']);
}
in this scenario if validation fails laravel returns a response to ajax call with Status Code: 422
, and i dont need to return any response
now if i want to check something else and if that fails i want to response just like validation fail response
function cehck(Request $request){
if($somethign_else_failes)
{
return response()->json(['errors' => ['email' => ['The email is invalid.']]], 422);
}
}
but this wont return back the ajax call response ... it will just return it to init
function and i need to add another return in the init
function
basically i dont want to have multiple returns in nested functions , if it failes at any level i want to return the ajax response at that level
i would like
function init(Request $Request){
$this->cehck();
// do the rest of code
}
function check(){
if($something_wrong)
//abort with reponse message
}
instead of something like this
function init(Request $Request){
$response = $this->cehck();
if( $response == failed )
{
//abort with reponse message
}
// do the rest of code
}
function check(){
if($something_wrong)
return failed flag ;
}
source https://stackoverflow.com/questions/70425795/returning-validation-fail-like-response-in-none-validation-scenario
Comments
Post a Comment