dtqqq24248 2012-05-07 23:55
浏览 25
已采纳

从数组中显示表单错误消息

I wanted to incorporate GUMP https://github.com/Wixel/GUMP into my site for server side validation. But unfortunately the documentation is light and I am new to PHP.

Here is my validation code:

//Validation
$gump = new GUMP(); 
$rules = array(
    'dept'     => 'required|numeric',
    'hosp'     => 'required|numeric',
    'subhosp'  => 'required|numeric',
    'user'     => 'required|numeric',
    'gpo'      => 'required|boolean|exact_len,1',
    'clin'     => 'required|valid_name',
    'clinmail' => 'required|valid_email',
    'comp'     => 'required|alpha_dash',
    'cpt'      => 'required|alpha_dash',
    'past'     => 'required|boolean|exact_len,1',
    'latex'    => 'required|boolean|exact_len,1',   
);
$validated = $gump->validate($_POST,$rules);
print_r($validated); // Something went wrong

The output from the above code gives me an array like so when I am looking at my AJAX response in FireBug:

Array
(
    [0] => Array
        (
            [field] => clin
            [value] => .-0
            [rule] => validate_valid_name
        )

    [1] => Array
        (
            [field] => clinmail
            [value] => %$sd
            [rule] => validate_valid_email
        )

)

And I what I need is something like so:

<div class="error-msg">You did not enter a valid email address</div><br>
<div class="error-msg">You did not enter a valid username</div><br>

From the documentation I get:

    if($validated === TRUE)
{
    // Do something, everything went well
}
else
{   
    // This is where I am stuck. Do I have to loop through and put my <div> tags here?
}

My question is how would the community handle outputting error messages with this class? My only thought is that I loop through the results above and output different messages depending on the field and the rule that was broken but that seems tedious. Is there a better way or a better class to use that does standalone PHP input validation? I was using another very easy to use class but it started breaking when I moved away from inline PHP to AJAX.

  • 写回答

3条回答 默认 最新

  • doudinghan8319 2012-05-07 23:59
    关注

    edit your code to:

    $gump = new GUMP(); 
    $rules = array(
    'dept'     => 'required|numeric',
    'hosp'     => 'required|numeric',
    'subhosp'  => 'required|numeric',
    'user'     => 'required|numeric',
    'gpo'      => 'required|boolean|exact_len,1',
    'clin'     => 'required|valid_name',
    'clinmail' => 'required|valid_email',
    'comp'     => 'required|alpha_dash',
    'cpt'      => 'required|alpha_dash',
    'past'     => 'required|boolean|exact_len,1',
    'latex'    => 'required|boolean|exact_len,1',   
    );
    
    $error_texts = array(
    'dept'     => 'You must enter a numeric value',
    'hosp'     => 'You must enter a numeric value',
    'subhosp'  => 'You must enter a numeric value',
    'user'     => 'You must enter a numeric value',
    'gpo'      => 'You must enter a boolean value',
    'clin'     => 'You must enter a valid name',
    'clinmail' => 'You must enter a valid email',
    'comp'     => 'You must enter a valid alpha dash',
    'cpt'      => 'You must enter a valid alpha dash',
    'past'     => 'You must enter 1 char',
    'latex'    => 'You must enter 1 char',
    );
    
    $validated = $gump->validate($_POST,$rules);
    
    if($validated === TRUE)
    {
       echo "Every thing is ok";
    }
    else
    {
        foreach($validated as $key=>$error)
        {
            echo '<div class="error-msg">' . $error_texts["{$error['field']}"] . '</div><br />';
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 QTOF MSE数据分析
  • ¥15 平板录音机录音问题解决
  • ¥15 请问维特智能的安卓APP在手机上存储传感器数据后,如何找到它的存储路径?
  • ¥15 (SQL语句|查询结果翻了4倍)
  • ¥15 Odoo17操作下面代码的模块时出现没有'读取'来访问
  • ¥50 .net core 并发调用接口问题
  • ¥15 网上各种方法试过了,pip还是无法使用
  • ¥15 用verilog实现tanh函数和softplus函数
  • ¥15 Hadoop集群部署启动Hadoop时碰到问题
  • ¥15 求京东批量付款能替代天诚