Kohana 3.0 - 验证:如果字段之一不为空,则会出错
我有四个领域。我们称它们为 a
、b
、c
和 d
。我需要验证它们。
错误是:
- 一到三个字段不为空;
错误为 not 时:
- 所有字段不为空、
- 所有字段均为空;
这里有什么巧妙的解决方案吗?谢谢指教。
编辑:
唯一的关系是所有四个变量都以 event_
为前缀。它给了我 event_name
、event_description
等。
编辑 #2:
目前我有类似的东西......
if (
!empty($values['event_date'])
&& !empty($values['event_time'])
&& !empty($values['event_name'])
&& !empty($values['event_description'])
) {
它检查所有字段是否都是填满,然后,如果这是真的,则添加事件。
正如我之前所说,当某些字段未填写时(例如,用户忘记输入描述),我需要显示用户友好的错误。无论如何,当所有字段都被填写时(这意味着 - 一切都好)或当没有字段被填写时(这意味着 - 用户忽略事件添加并且不想添加一个) - 不应显示任何错误。
我可以编写包含 16 个“if”语句的代码,但是没有更好的方法吗? :)
I have four fields. Lets call them a
, b
, c
and d
. I need to validate them.
Error is when:
- One til three fields are not empty;
Error is not when:
- All fields are not empty,
- All fields are empty;
Any neat solution here? Thanks in advice.
Edit:
Only relationships are that all four variables are prefixed with event_
. It gives me event_name
, event_description
etc..
Edit #2:
At the moment I have something like...
if (
!empty($values['event_date'])
&& !empty($values['event_time'])
&& !empty($values['event_name'])
&& !empty($values['event_description'])
) {
It checks that all fields are filled up and then, if that's true, adds event.
As I said before, I need to display user-friendly error when some field isn't filled up (for example, user had forgot to enter description). Anyway, when all fields are filled up (it means - all okay) or when no fields are filled up (it means - user ignores event adding and don't want to add one) - no error should be displayed.
I could write code with 16 'if' statements, but isn't there any better way? :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这并不美观,但只要您想要检查的字段有一些独特的东西(例如“event_...”),您就可以循环遍历变量数组($values、$_POST 等)并检查只有重要的领域。然后,您可以轻松检查全有或全无的情况。
这是一个简单的例子:
This isn't beautiful, but as long as you have something unique about the fields you want to check (such as "event_..."), you could loop through the variable array ($values, $_POST, etc) and check only the fields that matter. Then, you can easily check for an all or none situation.
Here is a quick example:
输入的值之一与未输入的值之间是否存在关系?
你能把它解析为空值吗?
Is there a relationship between one of the entered values and the none entered values??
could you just parse it as an empty value?