9

The Situation:

Under models, I've a user.php which handles all validation regarding adding a user to a website.

This is (part of) the code:

public static $add_rules = array(
   'last_name'             => 'required',
   'first_name'            => 'required',
   'email'                 => 'required|unique:users,email',
   'username'              => 'required|alpha_num|min: 5|unique:users,username',
   'password'              => 'required|min: 4|same:password_confirmation',
   'password_confirmation' => 'required',
   'user_role'             => 'required|not_in:-- Choose User Type --'
);

user_role is the ID of the dropdown list, seen here:

<select name="user_type_id" class="form-control" id="user_role">
<option value="0">-- Choose User Type --</option>
@if(Session::get("user_type") == "superuser")
     {
     @foreach($user_types as $ut)
         <option value="{{$ut['id']}}">
             {{ ucwords($ut["user_type"]) }}
         </option>
     @endforeach
     }
@else{
     <option value="Regular">Regular</option>
}@endif
</select>

Basically, what happens up there is that the drop down list is filled with user types, whatever they are. But it always has the first 'option' of -- Choose User Type --.

The Problem:

Problem is, the user can go with that option and add a user. I have a javascript code that blocks this and outputs an error message in a pop-up window, but it's ugly, and not consistent with the rest of the website's error messages.

So I added it to the rules. It needs to be validated such that it will only accept anything other than the default -- Choose User Type -- option.

What I Tried:

not_in did not work, unfortunately.

Can I get some help on this?

Thanks.

2
  • 4
    Set the value attribute for the -- Choose User Type -- option to a null value, i.e. value="". If anyone selects -- Choose User Type -- it will fail the required validation rule as no value will be passed. Commented Jul 2, 2014 at 4:07
  • That did it, after figuring out that it takes the name and not the id. D'oh. Commented Jul 2, 2014 at 4:23

3 Answers 3

13

You're not using not_in the right way. You're supposed to pass in the values that are not allowed, not the representation of those values.

'user_role' => 'required|not_in:0'
Sign up to request clarification or add additional context in comments.

Comments

5

You have your select named "user_type_id" but you are trying to validate a field named "user_role"

2 Comments

D'oh. I was under the impression that it used the ID instead of the name due to my other stuff having the same name and ID. This fixed it, along with Jeemusu's comment above. Thanks guys.
You're welcome. It has happened to me countless times, so it's the first thing I look for.
0

I think this situation calls for using: Validator::extend()

http://laravel.com/docs/validation#custom-validation-rules

This would probably be a handy gist. Make a rule that validates whatever string the coder provides.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.