doujiao1814 2013-06-15 14:40
浏览 34
已采纳

自定义验证类仅测试最后一个元素

I have been trying to make a custom validation class, but it is bothering me that it only is test thing last element it sees. I need it to test everything I put into it.

<?php

class Valid {

function validate(array $args){

    $errors = NULL;

    //********************************
    //Find each key validation.
    //********************************
    foreach ($args as $key => $value){


        //*********************************
        //Check for letters only, a-z, A-Z.
        //*********************************
        if ($key == 'letters'){

            $letters = preg_match('/^\pL+$/u', $value) ? TRUE : FALSE;
            $letters_value = $value;
            $letters_array[] = $letters;

        }

        //*********************************
        //Check for numbers only, 0-9.
        //*********************************
        if ($key == 'numbers'){

            $numbers = preg_match('/^[0-9]+$/', $value) ? TRUE : FALSE;
            $numbers_value = $value;
            $numbers_array[] = $numbers;

        }

        //*********************************
        //Check for vaild email address.
        //*********************************
        if ($key == 'email'){

            $email = filter_var($value, FILTER_VALIDATE_EMAIL) ? TRUE : FALSE;
            $email_value = $value;
            $email_array[] = $email;

        }

        //*********************************
        //Check for empty string.
        //*********************************
        if ($key == 'empty'){

            $empty_it = (trim($value) == FALSE) ? TRUE : FALSE;
            $empty_array[] = $empty_it;

        }


    }

    //********************************
    //Check if you failed letters only.
    //********************************
    if ( count($letters_array) != count(array_filter($letters_array)) ) $errors .= 'You can only enter letters a-z or A-Z.<br />';


    //********************************
    //Check if you failed numbers only.
    //********************************
    if ( count($numbers_array) != count(array_filter($numbers_array)) ) $errors .= 'You can only enter numbers 0-9.<br />';


    //*************************************
    //Check if you failed email validation.
    //*************************************
    if ( count($email_array) != count(array_filter($email_array)) ) $errors .= 'You must enter a vaild e-mail address.<br />';


    //*************************************
    //Check if you empty string.
    //*************************************
    if ( count($empty_array) != count(array_filter($empty_array)) ) $errors .= 'You must enter a value for each required field.<br />';



    //********************
    //Display the errors.
    //********************
    if ($errors) echo '<h3>Oops..</h3>'. $errors; var_dump($args); die;

}

}

And then I call it like this:

$Validate->validate( array('empty' => $display_name, 'empty' => $password, 'empty' => $password_again, 
'empty' => $gender, 'empty' => $month, 'empty' => $day, 'empty' => $year, 'email' => $email, 'letters' => $gender,
'numbers' => $month, 'numbers' => $day, 'numbers' => $year) );

But my result is this:

array(4) { ["empty"]=> string(4) "2013" ["email"]=> string(0) "" ["letters"]=> string(0) "" ["numbers"]=> string(4) "2013" }

Any help?

  • 写回答

2条回答 默认 最新

  • dty47696 2013-06-15 14:46
    关注

    You cannot reuse keys in arrays. The last key - value pair will overwrite all the previous.

    Write an array like:

    $Validate->validate(array(
        'empty' => array($display_name, $password, $password_again, $gender, $month, $day, $year, $email),
        'letters' => array($gender, $month),
        'numbers' => array($day, $year).
    ));
    

    and parse it appropriately:

    if (isset($args["key"])) {
         foreach ($args["key"] as $value) {
             // ...
         }
    }
    // and this for every method to check
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度