Kohana 3.0 - 验证:如果字段之一不为空,则会出错

发布于 2024-11-25 09:15:37 字数 871 浏览 0 评论 0原文

我有四个领域。我们称它们为 abcd。我需要验证它们。

错误是:

  • 一到三个字段不为空;

错误为 not 时:

  • 所有字段不为空、
  • 所有字段均为空;

这里有什么巧妙的解决方案吗?谢谢指教。

编辑:

唯一的关系是所有四个变量都以 event_ 为前缀。它给了我 event_nameevent_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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蒗幽 2024-12-02 09:15:37

这并不美观,但只要您想要检查的字段有一些独特的东西(例如“event_...”),您就可以循环遍历变量数组($values、$_POST 等)并检查只有重要的领域。然后,您可以轻松检查全有或全无的情况。

这是一个简单的例子:

$total = 0;
$filled = 0;
foreach($values as $field => $val) {
    if(strpos($field,'event_') === 0) {
        $total++;
        if( ! empty($val)) {
            $filled++;
        }
    }
}
if($filled == 0 OR $total == $filled) {
    //PASS VALIDATION
} else {
    //FAIL VALIDATION
}

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:

$total = 0;
$filled = 0;
foreach($values as $field => $val) {
    if(strpos($field,'event_') === 0) {
        $total++;
        if( ! empty($val)) {
            $filled++;
        }
    }
}
if($filled == 0 OR $total == $filled) {
    //PASS VALIDATION
} else {
    //FAIL VALIDATION
}
半衾梦 2024-12-02 09:15:37

输入的值之一与未输入的值之间是否存在关系?
你能把它解析为空值吗?

if ( ! isset($post->a) )  $post->a = '';

Is there a relationship between one of the entered values and the none entered values??
could you just parse it as an empty value?

if ( ! isset($post->a) )  $post->a = '';
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文