1

Consider the manual authentication. If the order ID has not been found in database, we redirect user to page with input fields and with error 'wrongOrderId':

public function login(Request $request) {

    $inputted_orderId = $request->input('order_id');       
    $orderIdInDB = DB::table(self::SITE_IN_DEVELOPMENT_TABLE_NAME)
            ->where(self::ORDER_ID_FIELD_NAME, $inputted_orderId)->first();

   if (is_null($orderIdInDB)) {
       return Redirect::route('loginToSitesInDevelopmentZonePage')->withErrors('wrongOrderId');
   }               
}

In this example, we don't need to pass the error message: the message box is already exists in View; all we need is to display this message box when user has been redirected with error 'wrongOrderId':

@if (!empty($errors->first('wrongOrderId'))) 
    <div class="signInForm-errorMessagebox" id="invalidID-errorMessage">
        <!-- ... -->
    </div>
@endif

All above code is working without laravel/php errors; I get into is_null($orderIdInDB) if-block when input wrong order number, however the error message box don't appears. I tried a lot of ways, and (!empty($errors->first('wrongOrderId'))) is just last one. What I doing wrong?

3 Answers 3

1

Did you try printing {{$errors->first()}}?

first(string), works as a key value pair, it invokes its first key VALUE

try this,

 @if($errors->any())
    <div class="signInForm-errorMessagebox" id="invalidID-errorMessage">
        <!-- ... -->
    </div>
 @endif
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the answer! I successfully printed $errors->first(). ` @if($errors->any())` also works. How to check the specific error, I learned in user2486's answer.
0

If you want to get specific error from validator error, use the get() method.

$errors->get('wrongOrderId'); get all the wrongOrderId errors.

$errors->first('wrongOrderId'); get first error of wrongOrderId errors

2 Comments

Thank you for the answer. Hmm... When I try $errors->first('wrongOrderId');, the error don't appears yet. Is ->withErrors('wrongOrderId'); OK with your solution? Also, when I tried $errors->get('wrongOrderId');, I got the strange exception: htmlspecialchars() expects parameter 1 to be string, array given. {{$errors->first()}} is working: wrongOrderId is displaying.
Use this code in your blade php, not controller. add in the {{ }} or {!! !!}
0

I explored that $errors->first('wrongOrderId') has non-displayable but non-null value. So @if ( $errors->first('wrongOrderId') !== null ) will work.

Something more elegantly like @if ($errors->first('wrongOrderId'))? Unfortunately just one this check is not enough: even if we define

return Redirect::route('loginToSitesInDevelopmentZonePage')->withErrors(['wrongOrderId' => true]);

php will convert true to 1. So, @if ( $errors->first('wrongOrderId') == true ) will working, but @if ($errors->first('wrongOrderId')) will not.

However, we can to cast $errors->first('wrongOrderId') to bool, so the most elegant solution will be

@if ((bool) $errors->first('wrongOrderId'))

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.