如何验证 Laravel PHP 中的下拉列表?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24522639/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 09:44:36  来源:igfitidea点击:

How do I validate a drop down list in Laravel PHP?

phplaravel

提问by zack_falcon

The Situation:

情况:

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

在模型下,我有一个user.php处理关于将用户添加到网站的所有验证。

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_roleis the ID of the dropdown list, seen here:

user_role是下拉列表的 ID,见此处:

<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 --.

基本上,上面发生的事情是下拉列表充满了用户类型,无论它们是什么。但它总是有第一个“选项” -- 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.

问题是,用户可以使用该选项并添加用户。我有一个 javascript 代码可以阻止它并在弹出窗口中输出错误消息,但它很丑陋,并且与网站的其余错误消息不一致。

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.

所以我把它添加到规则中。它需要经过验证,以便它只接受默认-- Choose User Type --选项以外的任何内容。

What I Tried:

我试过的:

not_indid not work, unfortunately.

not_in没有工作,不幸的是。

Can I get some help on this?

我能得到一些帮助吗?

Thanks.

谢谢。

采纳答案by CleverCookie

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

您的选择名为“user_type_id”,但您正在尝试验证名为“user_role”的字段

回答by Kemal Fadillah

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

你没有使用not_in正确的方法。您应该传入不允许的值,而不是这些值的表示。

'user_role' => 'required|not_in:0'

回答by mangonights

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

我认为这种情况需要使用:Validator::extend()

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

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.

这可能是一个方便的要点。制定一个规则来验证编码器提供的任何字符串。