开发者

error logic and display in php?

ok im makeing an application im going to tell how i do error logic and displaying in my application

// ERROR CHECK
if ( !empty( $user_status ) )
    $errors[] = language( 'ERROR_USERNAME_USED' );

if ( empty( $_POST['username'] ) )
    $errors[] = language( 'ERROR_USERNAME_NULL' );

if ( !( $validation->valid_uid( $_POST['username'] ) ) && !empty( $_POST['username'] ) )
    $errors[] = language( 'ERROR_USER_NAME_NOT_VALID' );

if ( empty( $_POST['password'] ) )
    $errors[] = language( 'ERROR_USER_PASS_NULL' );

if ( empty( $_POST['name'] ) )
    $errors[] = language( 'ERROR_USER_REALNAME_NULL' );

if ( !( $validation->valid_name( $_POST['name'] ) ) && !empty( $_POST['name'] ) )
    $errors[] = language( 'ERROR_USER_开发者_开发问答REALNAME_NOT_VALID' );

if ( empty( $_POST['company'] ) )
    $errors[] = language( 'ERROR_COMPANY_NAME_NULL' );

if ( !( $validation->valid_company_name( $_POST['company'] ) ) && !empty( $_POST['company'] ) )
    $errors[] = language( 'ERROR_COMPANY_NAME_NOT_VALID' );

if ( empty( $_POST['phone'] ) )
    $errors[] = language( 'ERROR_USER_PHONE_NULL' );

if ( !( $validation->valid_phone( $_POST['phone'] ) ) && !empty( $_POST['phone'] ) )
    $errors[] = language( 'ERROR_USER_PHONE_NOT_VALID' );

// Database
if ( !sizeof( $errors ) ) {
    // do something here.
    }

here is my function

$errors = array(); 
function error_display($handle, $title = null, $type = null) 
{
    if (sizeof($handle)) 
    {
        echo '<div id="red-error-box"><ul><h3>' . count($handle) . ' ' . $title . ' Error!</h3>';

        if (is_null($type)) {
            foreach ($handle as $key => $value) 
            {
                echo '<li class="error-list">' . $value . '</li>';
            }
        }
        if ($type == 1)
        {
             echo '<li class="error-list">' . $handle[0] . '</li>';
        }

        echo '</div>';
    }
}

then i display them with error_display($errors); at the above of the html,

is there a better way of doing this ?

Thanks for you time helping

Adam Ramadhan


As said, the $error[] list seems a good approach. But just as example, you could simplify your code into a validation list and loop:

$fields = array(
     "username" => language( 'ERROR_USERNAME_NULL' ),
     "password" => language( 'ERROR_USER_PASS_NULL' ),
     "name" => language( 'ERROR_USER_REALNAME_NOT_VALID' ),
     ...
);

foreach ($fields as $field=>$error_msg) {

     if (empty($_POST[$field])
      || method_exists($validation, "valid_$field")
      && !$validation->{"valid_$field"}($_POST["field"]))
     {
          $errors[] = $error_msg;
     }
}

Obviously you need a better description if you really need to split up _NULL and _NOT_VALID tests. I'd personally skip that. But maybe your validation class could handle this. Make one verify_fields_not_empty() method, and a second test_valid_format($per_list). You might need a more clever way to map field names onto validation methods, but it's just a matter of abstracting it away.

But more importantly you could adapt the $error[] collection in a central place. I'm not sure it's needed however. Your error array can already be used for printing and logging. It's only if a second application module would depend on an object format there...


Probably not the best answer, but I do a slightly more lazy method that has worked great for me. I just use JavaScript for error reporting, but do data checking prior to database changes. So that way if a malicious user wants to circumvent anything I'm still covered and the form still looks nice. There is also the problem where a user doesn't have JavaScript enabled. For these users I simple let them know its required. Makes my life a lot easier.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜