2

I have problem with Laravel validation when validation fails it also call block of code where it should be successful...

I am trying to check for some user id if his admin_id field equal with user which is currently logged.

Here is code:

$auth = Auth::user()->id;
$inputs = array(
    'id' => Input::get('id')
);

$rules = array(
    'id' => "required|exists:users,id,admin_id,$auth"
);

$validate = Validator::make($inputs, $rules);

if ($validate->fails()) {
    return $validate->messages()->all();
} else {
    return 'succes';
}
2
  • 1
    Can you check with a static admin_id? Like "required|exists:users,id,admin_id,1" Commented Apr 1, 2015 at 17:45
  • It was problem with testing trought Chrome.Code above works fine. Commented Apr 2, 2015 at 12:40

2 Answers 2

1

Try doing this:

$rules = array(
  'id' => "required|exists:users,id,admin_id," . $auth
);
Sign up to request clarification or add additional context in comments.

Comments

0

You can do this without validation.

$auth = Auth::user()->id;
$input = Input::get('id');

if($auth != $input){
    return 'your custom error message';
}else{
    return 'success';
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.