1

I am stuck a bit, I have a little form and auth system - Sentry.

1) Get data from Input and validate it. It's okay, I get it, check with rules, set messageBag

<?php
     $validation->getMessageBag()->setFormat(Config::get('project/config.errors_delimiters'));
?>

Then I return page withErrors method. And prints validation errors.

2) Next step will be to login users, for example. This mean, that I need to use a Sentry manual.

But now I have a problem: how to show errors or store errors. I have:

  • Error messages via validation
  • Error messages via Sentry
  • Custom Errors
  • Success messages
  • Info messages

Which the best practice to store this messages? How to send it to a view? And how to parse it in a view? Also how to set delimiters style?

Of course I can create tons of flash, session, and other types of data and then send this into a view, but I think it is ridiculous. I think there is a simple method which can help me.

Gustavo Straube
  • 3,744
  • 6
  • 39
  • 62
Ascelhem
  • 413
  • 3
  • 21

1 Answers1

0

Very good question. This is how I handle it.

Because validation errors uses MessageBag, what I did is

$messages = new MessageBag;

and then if you run into any error, you could do

try {
    ...
} catch (Exception $e) {
    $messages->add("error", $e->getMessage());
}

and if you have other errors returned you can merge it

$messages->merge($other_messages)

which includes validation messages $validation->messages().

Because messagebag accepts keys, you can add other type by $messages->add('info', 'ok')

And then in your template, or controller, you can get messages of that type by

$messages->get('error')

after you checked with

$messages->has('error')
windmaomao
  • 7,120
  • 2
  • 32
  • 36
  • Yeah, I haven't stopped my researches about this problem. I found it in API documentation. And now I have another problem. Here we have three errors: [Error: "Your password is wrong" -> validation], [Warning: "Login limit" -> my_app], [Info: "I'm a cat" ->my_app]. You know bootstraps divs:
    :message
    , but if I'll merge errors into one bag, I can only display one type of errors. I think to create a helper, which will work like this: gets "error|message", parses this string and outputs correct string with styles. Or maybe I should use flashdata? Anyway, I need go deep.
    – Ascelhem Jul 20 '14 at 17:58