mardi 24 décembre 2019

Laravel 5.1 Validation MessageBag contains errors however not showing up in blade file

I am trying to show any validation error in the blade file.

Below I have provided how my controller looks like :

$rules =array(
    'mobile' => 'required_without:landline',
    'landline' => 'required_without:mobile'
);
$validator = Validator::make($request->all(), $rules);
//process
if($validator->fails()){
    //$fieldsWithErrorMessagesArray = $validator->messages();
    //dd($fieldsWithErrorMessagesArray);

    return Redirect::back()->withErrors($errors)->withInput();
}

This is how my blade file looks like :

<?php
      echo print_r($errors,true);
?>

@if ($errors->has('mobile'))
     @foreach($errors->get('mobile') as $error)
             <span class="validation_error"></span>
     @endforeach
@endif

@if ($errors->has('landline'))
     @foreach($errors->get('landline') as $error)
             <span class="validation_error"></span>
     @endforeach
@endif

When the validation failed in the controlled, I had a 2 lines which I commented out (used for testing to see the error output).

This is what that dd($fieldsWithErrorMessagesArray); generated :

MessageBag {#218 ▼
  #messages: array:21 [▼
    "mobile" => array:1 [▼
      0 => "The mobile field is required when landline is not present."
    ],
    "landline" => array:1 [▼
      0 => "The landline field is required when mobile is not present."
    ]
  ]
}

However when I tried echoing out in the blade file, I am getting an empty array

I am using Laravel 5.1. I am not sure why this is happening any help would be appreciated.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire