Laravel 表单验证

标签 laravel laravel-5.1

我正在验证下面的数字字段,代码工作正常。它正在检查字符不应超过 2 个或更少,这对我来说很好。

/**
 * Get the validation rules that apply to the request.
 *
 * @return array
 */
public function rules()
{
    return [
        'id'    => 'required|min:2|max:2',
        'title' => 'required'
    ];
}

但是当我添加更多验证规则(例如(数字))时,最小和最大验证规则发生更改,现在正在检查数字不应大于 2...为什么?这是BUG吗?

/**
 * Get the validation rules that apply to the request.
 *
 * @return array
 */
public function rules()
{
    return [
        'id'    => 'required|min:2|max:2|numeric',
        'title' => 'required'
    ];
}

最佳答案

最小和最大规则取决于字段的类型。如果它是一个字符串,它将检查长度。如果它是一个数字,它将检查该值。

因此,如果您只想使用两个字符集的数字

'id'    => 'required|min:10|max:99|numeric',

The field under validation must have a size matching the given value. For string data, value corresponds to the number of characters. For numeric data, value corresponds to a given integer value. For files, size corresponds to the file size in kilobytes.

关于Laravel 表单验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35223676/

相关文章:

Laravel 对created_at 日期字段的强制转换未应用

laravel-5.1 - 命令行语句 php artisan tinker 不工作

php - Laravel 5.8 自定义表单请求未按预期工作?

javascript - 如何在 axios 调用后立即更新 Vue?

laravel - 流明 (5.1.6) 每日日志

javascript - 随机化表单中 div 的顺序

mysql - Laravel - 使用 `SQL_CALC_FOUND_ROWS` 与急切加载关系返回错误计数

php - 向服务器发送多个请求

php - 如何在 Laravel 5.1 中观察所有实现接口(interface)的模型?

javascript - Laravel 使用 Ajax 从购物车中删除