【问题标题】:PHP check for empty inputs in form (both file and text)PHP 检查表单中的空输入(文件和文本)
【发布时间】:2017-06-14 08:36:28
【问题描述】:

我有一些 trubbel 试图制作一个 PHP 脚本来检查任何文本(或其他)输入是否为空,同时检查任何文件输入是否为空。

代码像某些框架一样使用 $rule 来编写输入规则

示例:

$rule = array(
    "title" => "required|minLength:5",
    "myFile" => "required"
);

我有这个函数,每个输入都有循环

foreach ($rules as $item => $ruleset) {
    // required|email|min:8
    $ruleset = explode('|', $ruleset);

    foreach ($ruleset as $rule) {

        $pos = strpos($rule, ':');
        if ($pos !== false) {
            $parameter = substr($rule, $pos + 1);
            $rule = substr($rule, 0, $pos);
        }
        else {
            $parameter = '';
        }

        // Example of this output: validateEmail($item, $value, $param)
        $methodName = 'validate' . ucfirst($rule);
        $value = isset($data[$item]) ? $data[$item] : NULL;

        if (method_exists($this, $methodName)) {
            $this->$methodName($item, $value, $parameter) or $valid = false;
        }
    }
}


// Validate the $value of $item to see if it is present and not empty
private function validateRequired ($item, $value)
{       
    if (empty($value))
    {
        $this->errors[$item][] = 'The ' . $item . ' field is required';
        return false;
    }   
    return true;
}

它可以工作,但我们知道,需要使用 $_FILES 检查文件输入,

我能做什么???

【问题讨论】:

  • 甚至没有接近解决方案。太宽泛了

标签: php forms validation file-upload


【解决方案1】:

您的问题很不清楚您是如何尝试将所有这些代码片段一起使用的,尤其是validateRequired() 函数。但是考虑到目前的情况,你也应该在$rules数组中添加type属性值。

$rules = array(
    "title" => "required|minLength:5|type:text",
    "myFile" => "required|type:file"
);

并根据字段的type 处理您的表单字段。但是,我建议您使用以下 $rules 数组重构您的业务逻辑,

$rules = array(
    'title' => array(
        'required' => true,
        'minLength' => 5,
        'type' => 'text'
    ),
    'myFile' => array(
        'required' => true,
        'type' => 'file'
    )
);

使用上面的$rules 数组,您可以更轻松地处理表单输入字段。所以你的foreach 循环会是这样的:

foreach($rules as $item => $rule){
    switch($rule['type']){
        case 'text':
            if($rule['required']){
                if(!empty($_POST[$item]) && strlen($_POST[$item]) >= $rule['minLength']){
                    // SUCCESS: The text field complies with required and minLength conditions
                }else{
                    // ERROR: The text field doesn't comply with required and minLength conditions
                }
            }else{
                // Proceed with your logic
            }
            break;
        case 'file':
            if($rule['required']){
                if(is_uploaded_file($_FILES[$item]['tmp_name'])){
                    // SUCCESS: The file field complies with required condition. User has uploaded a file
                }else{
                    // ERROR: The file field doesn't comply with required condition. User hasn't uploaded any file
                }
            }else{
                // Proceed with your logic
            }
        case 'checkbox':
            // your code


    }
}

【讨论】:

    猜你喜欢
    • 2021-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-24
    • 1970-01-01
    • 2018-03-12
    相关资源
    最近更新 更多