简体   繁体   中英

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. 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. 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

I believe your POST data would be something like this:

$_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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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