1

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

3
  • 1
    Looks very workable to me. I'd like to point out that while it's similar, you are performing two tasks actually. You could separate empty() checks and format validation, then turn it into a loop. -- Also offtopic: try transitioning to gettext-style translations, not MSG_ID lookups. Commented Nov 15, 2010 at 16:38
  • thanks mario :), but lets see what others awesome guys have this problem done. about gettext-style translations, is there a link or an example ? is it like the $fields below ? i mean like $lang = array () ? Commented Nov 16, 2010 at 5:09
  • php.net/manual/en/book.gettext.php found it. Commented Nov 16, 2010 at 5:21

2 Answers 2

1

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...

Sign up to request clarification or add additional context in comments.

2 Comments

map field names onto validation ? can you explain that one more ?
@Adam Ramadhan: For the "name" field above code maps to the $validation->valid_NAME() method. However since you have a "company" field, but a ->valid_COMPANY_NAME() method, you might need a mapping for that. (Or otherwise just follow the simpler method naming scheme.)
1

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.

1 Comment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.