12

Assume that I have this Exception Message

catch (Cartalyst\Sentry\Users\LoginRequiredException $e) { echo 'Login field is required.'; } 

How can I pass this message Login field is required using withErrors()?

return Redirect::to('admin/users/create')->withInput()->withErrors(); 
0

2 Answers 2

30
return Redirect::to('admin/users/create') ->withInput() ->withErrors(array('message' => 'Login field is required.')); 
Sign up to request clarification or add additional context in comments.

5 Comments

A little bit of explanation would be useful.
@cartbeforehorse to be honest, I think it's self explanatory, but I might be wrong. As method name implies, it redirect you to the page with the specific name in the argument, with the input from the previous form, submission, along with the error that is specified in the argument.
I think on a forum like Stack, you should never assume anything is self-explanatory. If it was, the question would never have needed to be asked in the first place. People come to Stack not only to have their questions answered, but to learn the principles behind the question and/or the technology. Your answer isn't going to be read only by the questioner.
@cartbeforehorse upvotes on the question shows otherwise, but I agree with you, you have a valid point. I can't explain though the entire redirecting process of the framework, that's why the documentation exists and it's a prerequisite to have some knowledge on the Http protocol (POST method).
@cartbeforehorse just so my comments on the other answer, well this is called karma.
3

This depends on where you are catching the exception.

Sentry does not use the Validator class. So if you want to return an error message the Laravel way, you should create a separate Validator object and validate first, then only pass to Sentry after your validation has passed.

Sentry will only be able to pass 1 error back as it is catching a specific exception. Furthermore, the error will not be of the same type as the error in the validation class.

Also, if Sentry does catch the exception, then your Validation is clearly not working.

Code below is not how you should do it, but more to show a combination of what I believe shows ways of working with Laravel / Sentry

Example User model

class User extends Eloquent { public $errors; public $message; public function registerUser($input) { $validator = new Validator::make($input, $rules); if $validtor->fails() { $this->errors = $validator->messages(); return false; } try { // Register user with sentry return true; } catch (Cartalyst\Sentry\Users\LoginRequiredException $e) { $this->message = "failed validation"; return false; } } } } 

UserController

class UserController extends BaseController { public function __construct (User $user) { $this->user = $user; } public function postRegister() { $input = [ 'email' => Input::get('email'), 'password' => Input::get('password'), 'password_confirmation' => Input::get('password_confirmation') ]; if ($this->user->registerUser($input)) { Session::flash('success', 'You have successfully registered. Please check email for activation code.'); return Redirect::to('/'); } else { Session::flash('error', $this->user->message); return Redirect::to('login/register')->withErrors($this->user->errors)->withInput(); } } 

6 Comments

Was typed out ages ago and was typed up freehand - not copy and pasted from running code. Was written as proof of concept.
Sorry but this is no excuse ,it's the same (or worse) as providing no code. Wrong code discourage people who just want something working. It's not ages, it's 5 months ago.
People should not be looking for quick fixes when it comes to security and logins. As per answer... clearly states This is not how to do it, but a way to work with Laravel / Sentry. As such, I have given enough food for thought for somebody to pick up on the idea and build on it. Also, it answers the question and although not syntactically correct, provides a more detailed and useful answer than yours. Further, if SO had a php debugger / ide built in, then fine - but it doesnt - it's just a text box.
if $validtor->fails() { How valid is this?
@giannischristofakis the purpose of this site is not to do the work of other developers but to give them a helping hand.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.