简体   繁体   English

如何创建带有相关错误的 php 条件检查数组?

[英]How to create array of php conditional checks with associated errors?

I am looking for a way to create a conditional check after submitting a form.我正在寻找一种在提交表单后创建条件检查的方法。 Each field is different (date, name, occupation, etc.) and I want to check a set of conditionals for each field.每个字段都不同(日期、姓名、职业等),我想检查每个字段的一组条件。 What would be the most efficient way of doing so?这样做的最有效方法是什么? The only way I can think of is a lot of if/else statements or a switch.我能想到的唯一方法是很多 if/else 语句或 switch。 I am wondering if there is any way to put all the checks into an array eg我想知道是否有任何方法可以将所有检查放入一个数组中,例如

[field_name conditional_check acceptable_values error_response]

and then loop through each row in the array and return applicable error responses?然后遍历数组中的每一行并返回适用的错误响应? That way I can cut down on the hardcoded things.这样我就可以减少硬编码的东西。

Thanks谢谢

Yeah, you can run multiple if conditions and store the error messages in an array.是的,您可以运行多个 if 条件并将错误消息存储在数组中。 Let me show an example:让我举个例子:

$errors = array();

if(/*condition 1*/) {
    $errors[] = "Message 1";
}

if (/*condition 2*/) {
    $errors[] = "Message 2";
}

if (/*condition 3*/) {
    $errors[] = "Message 2";
}

if (!empty($errors)) {
    $strMsg = "<ul>Please fix the following errors:";
    foreach($errors as $error) {
        $strMsg .= "<li>$error</li>";
    }
    $strMsg .= "</ul>";
    echo $strMsg;
}

Hope this helps.希望这可以帮助。

Peace!和平! xD xD

I believe your POST data would be something like this:我相信您的POST数据将是这样的:

$_POST => array(
    "username" => "",
    "password" => "",
    "confpass" => "",
    "emailadd" => "",
    "phonenum" => "",
    "acceptcn" => ""
);

You can use a complex condition checker like this.您可以使用像这样的复杂条件检查器。

<?php
    $conditions = array(
        "username" => array(
            "required" => true,
            "validate" => function () {
                return (strlen($_POST["username"]) > 5);
            }
        ),
        "password" => array(
            "required" => true,
            "validate" => function () {
                return (strlen($_POST["password"]) > 5);
            }
        ),
        "confpass" => array(
            "required" => true,
            "validate" => function () {
                return ($_POST["password"] == $_POST["confpass"]);
            }
        ),
        "emailadd" => array(
            "required" => true,
            "validate" => function () {
                return is_email($_POST["emailadd"]);
            }
        ),
        "phonenum" => array(
            "required" => false,
            "validate" => function () {
                if ((!empty($_POST["phonenum"]) && $conditions["phonenum"]["required"]) || !$conditions["phonenum"]["required"])
                    return (strlen($_POST["phonenum"]) == 10 || empty($_POST["phonenum"]));
                else
                    return false;
            }
        ),
        "acceptcn" => array(
            "required" => true,
            "validate" => function () {
                return $_POST["acceptcn"];
            }
        )
    );
?>

And then loop through the $_POST array and call the validate function.然后循环遍历$_POST数组并调用validate函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM