【问题标题】:Validate custom form field in laravel auth在 laravel auth 中验证自定义表单字段
【发布时间】:2015-08-29 12:42:09
【问题描述】:

我正在使用 laravel 5.1.9 LTS。我正在使用开箱即用的 laravel 的身份验证功能。我在注册中添加了一个新的表单域,但它没有被验证。 我添加的新表单域。

 <div class="controls">
                                <div class="controls first">
                                    <select name="account_type" class="form-control">
                                        <option value="0" >Select the account type</option>
                                        <option value="1">Buyer</option>
                                        <option value="2">Seller</option>
                                        <option value="3">Agent</option>
                                    </select>
                                </div>
                                <p class="help-block">
                                    {{ $errors->first('account_type') }}
                                </p>
                            </div>

验证部分

protected function validator(array $data)
{
    return Validator::make($data, [
        'email' => 'required|email|max:255|unique:users',
        'account_type' => 'required|min:1',
        'password' => 'required|confirmed|min:6',
    ]);
}

我也将 account_type 放入可填充的受保护变量中,但仍然无法正常工作

【问题讨论】:

  • 当你使用print_r($data);会给你一个结果吗?
  • @aldrin27 是的,我做了 dd($data) 它也给了我 account_type 的值

标签: php laravel laravel-5.1


【解决方案1】:

问题是 account_type 的 value 参数作为字符串返回。当您在字符串上调用 "min:1" 时,它将接受最小 lenght 为 1 的字符串,这在您拥有的所有选项中都是如此。

您可以尝试更改此行,使其返回长度为零/空的字符串:

<option value>Select the account type</option>

您可以删除规则“min:1”并将其保留为“必需”。

【讨论】:

    猜你喜欢
    • 2016-09-09
    • 1970-01-01
    • 2023-03-29
    • 1970-01-01
    • 2017-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-26
    相关资源
    最近更新 更多