简体   繁体   中英

Slim Framework Request Validation

Hello so I am using slim framework, and I have a code that check if all of the inputs are not empty, here is the code:

$request = \Slim\Slim::getInstance()->request()->post();
    if($request['txt_1'] != "" AND $request['txt_2'] != "" AND $request['txt_3'] != "" AND $request['txt_4'] != "" AND $request['txt_5'] != "" AND $request['txt_6'] != "" AND $request['txt_7'] != "" AND $request['txt_8'] != "" AND $request['txt_21'] != "" AND $request['txt_22'] != "" AND $request['txt_23'] != "" AND $request['txt_24'] != "" AND $request['txt_31'] != "" AND $request['txt_32'] != "" AND $request['txt_41'] != "") {
        $status = "0";
    } else {
        $status = "1";
    }

What I wanted to do is instead of coding all request inputs in the condition, I only want a single variable to check all of the request inputs if not empty. Is there a way? Like (!empty($allFields)) ? Thank you in advance.

You could loop through your $request array and set $status if one of the values is not empty.

Or you could implode the array over an empty string I guess.

$status = implode ('', $request) !== '';

Look at the following function. It takes two arguments. $requestData will contain all your parameters received via request. $parameters will be an array where you will write all the parameters you want to make sure that you have received in request.

The call to function can be something like this:

checkParameters($requestData,array('txt_1','txt_2','txt_3','txt_4')) ;

In case you are having lots of parameters, you may also make a separate array of parameters and then pass it as an argument.

 function checkParameters($requestData,$parameters) 
        {   

            $check=0;
            if ((count($requestData) >= count($parameters)) 
            {
                $check=0;
                foreach ($requestData as $key=>$value) 
                {
                    if (in_array($key,$parameters, TRUE)) 
                    {
                        $check=1;
                        unset($parameters[array_search($key,$parameters)]);
                    }
                    else
                    {
                            return 0;
                    }
                }
            }
            if($check)
            {   
                if (count($parameters) == 0) 
                {
                    return 1;
                }
                else
                {
                    return 0;
                }
            }
            else
            {
                return 0;
            }
        }

Return 1 stands for good to go while 0 stands for some error.

Use below function to check all param for validation and it works with all type of request methods(put/post/get/...). param should be array of fields like: $required_fields=array('field_1','field_2');

function verifyRequiredParams($required_fields) {
    $error = false;
    $error_fields = "";
    $request_params = array();
    $request_params = $_REQUEST;
    // Handling PUT request params
    if ($_SERVER['REQUEST_METHOD'] == 'PUT') {
        $app = \Slim\Slim::getInstance();
        parse_str($app->request()->getBody(), $request_params);
    }
    foreach ($required_fields as $field) {
        if (!isset($request_params[$field]) || strlen(trim($request_params[$field])) <= 0) {
            $error = true;
            $error_fields .= $field . ', ';
        }
    }

    if ($error) {
        // Required field(s) are missing or empty
        // echo error json and stop the app
        $response = array();
        $app = \Slim\Slim::getInstance();
        $response["error"] = true;
        $response["message"] = 'Required field(s) ' . substr($error_fields, 0, -2) . ' is missing or empty';
        echoResponse(400, $response);
        $app->stop();
    }
}

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