php - Laravel Session Array Values -
i've started test out laravel. i'm using form fields , trying validate inputs using laravel's built-in validator class.
$input = input::all(); $rules = array( 'fname' => 'required|max:100', 'lname' => 'required|max:100', 'email' => 'required|email', ); $validation = validator::make($input, $rules); if ($validation->fails()){ return redirect::to('inputform') ->with('input_errors', $validation->errors); }
everything goes well, , validation check works. when validation fails, put errors in session variable called input_errors
, pass view. problem can't seem display errors. tried foreach
loop using blade templating engine
given below:
@foreach (session::get('input_errors') $message) {{ should put here? }} @endforeach
how can display errors being returned array. tried referencing $message[0][0]
didn't work.
thanks.
edit: sorry, forgot mention i'm using laravel 3
the correct syntax getting errors is...
$messages= $validation->messages();
that alone, unfortunately, not going return messages. it's going return messagebag
instance. allows pull out specific messages want or all.
if want messages, can do...
$errors = $messages->all();
that return array loop through in view display errors. there methods getting errors on specific field such as...
$firstnameerror = $messages->first('fname');
or
$firstnameerrors = $messages->get('fname');
i suggest when sending error messages view, use...
->with_errors($validation);
that flash errors session , automatically assume sending them $errors
variable. may display errors in view with.
{{ $errors->first('fname') }} // blade approach <?php echo $errors->first('email'); ?> // non-blade approach
this way, don't have add logic views trying determine if variable exists before should try , echo it.
Comments
Post a Comment