PHP检查表单中的空输入(文件和文本)

时间:2017-01-28 20:50:16

标签: php forms validation file-upload

我有一些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检查文件输入,

我该怎么办?

1 个答案:

答案 0 :(得分:0)

您的问题很清楚,您是如何尝试将所有这些代码组合使用的,尤其是<td><a rel="<?php echo md5($row['id'].'p');?>" class="class1 sp btn btn-warning"><i class="fa <?php echo $icon;?> "> <?php echo $status;?></i></a></td> 函数。但鉴于当前情况,您还应在$('a.class1').click(function(e){ var status=$(this).text(); console.log(status); if(status==='X'){ $( "#dialog-confirm" ).dialog({ resizable: false, height: "auto", width: 400, modal: true, buttons: { "Si": function() { $( this ).dialog( "close" ); $.ajax({ }) .done(function(data) { if (data==1){ $( "#dialog-message" ).dialog({ modal: true, buttons: { Ok: function() { $( this ).dialog( "close" ); } } }); }else{ $('#msj_error').css("background-color","#7A0C0C").html('<h4 style="color:#fff;">Error recibiendo datos</h4>').fadeIn('fast').delay(4000).fadeOut('slow'); } }) .fail(function(msg) { alert("Fallo Conexion!!! \n"+msg); //$("#Cargando").fadeOut("fast"); }); }, "No": function() { $( this ).dialog( "close" ); } } }); }else{ $( "#dialog-message2" ).dialog({ modal: true, buttons: { Ok: function() { $( this ).dialog( "close" ); } } }); } }); 数组中添加validateRequired()属性值。

type

根据字段的$rules处理表单字段。不过,我建议您使用以下$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
相关问题