Laravel验证所需的规则不起作用 [英] Laravel validation required rule not working

查看:117
本文介绍了Laravel验证所需的规则不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果一个字段可用,我需要添加必需的规则.还需要检查它是否为整数和10位数字.所以我添加了如下规则.

I need to add required rule if one field is available. Also need to check if it is an integer and 10 digit. So I added the rule like below.

'id_number' => 'sometimes|required|digits:10|integer'

验证仅在该字段可用时有效.但是这里要求的规则不起作用.即使该字段为空,它也会直接显示integer错误.

Validations works only when the field is available. But here required rule is not working. It directly shows integer error even if the field is empty.

我使用Laravel 5.1

I use Laravel 5.1

推荐答案

最后我明白了!

您需要将所需规则的顺序更改为last.当我添加这样的规则时,它会起作用

You need to change the order of required rule to last. It works when I add rule like this,

'id_number' => 'sometimes|digits:10|integer|required'

这篇关于Laravel验证所需的规则不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆