Jump to content

Recommended Posts

Greetings,

 

I've been searching and searching and I can't find a more maintainable way of doing form validation than what I already have?

$errors = array();

    if(strlen($fields['username']) < USERNAME_MIN_LENGTH)
        $errors[] = "Your <b>username</b> needs to be atleast ".USERNAME_MIN_LENGTH." characters long.";
    if(strlen($fields['username']) > USERNAME_MAX_LENGTH)
        $errors[] = "Your <b>username</b> can not be longer than ".USERNAME_MAX_LENGTH." characters.";
    if(!valid_email($fields['email']))
        $errors[] = "You did not enter a valid <b>email</b> address!";
    if($fields['email'] != $fields['emailconfirm'])
        $errors[] = "Your <b>email</b> addresses did not match!";
    if(strlen($fields['password']) < PASSWORD_MIN_LENGTH)
        $errors[] = "Your <b>password</b> must have a minimum of ".PASSWORD_MIN_LENGTH." characters.";
    if($fields['password'] != $fields['passwordconfirm'])
        $errors[] = "Your <b>passwords</b> do not match!";
    if(strlen($fields['sq']) < 5)
        $errors[] = "You must create a <b>secret question</b> of atleast 5 characters.";
    if(strlen($fields['sqa']) < 5)
        $errors[] = "Your <b>secret question answer</b> must be atleast 5 characters.";

    if(!empty($errors)) {
        foreach($errors as $error)
            $content['errors'] = "<p style=\"color: red;\">".$error."</p>";
    }

 

This is rather tedious creating all this for multiple forms throughout a website. Is there already a tried and proved method of making form validation easier?

 

Kind Regards,

Ace

Link to comment
https://forums.phpfreaks.com/topic/243153-maintainable-form-validation/
Share on other sites

There is no one best method. But, what I do is create functions for different types of validations. For example, I might have a function to check minimum length. I would pass the function the submitted value, the minimum required length. Personally, I think your validation is too overdone. For example, you have one validation to check the minimum length and then a separate validation to check the maximum length. Each with a different error message. Why not ONE validation to validate BOTH the minimum and maximum lengths with a single error message

"Your <b>username</b> needs to be between ".USERNAME_MIN_LENGTH." and ".".USERNAME_MAX_LENGTH." characters.";

 

Also, by using functions to do common validation you greatly reduce the chances of bugs. By rewriting the same validations over and over again you may have a bug in one but not others that goes unnoticed. But, if you are running a single function to do multiple validations the chances of finding the bug are greater and you won't have copy/paste errors - which happens a lot when people copy/paste code and then tweak it for a slightly different outcome. Instead, it is better to create a function that takes some parameters to create those slightly different outcomes (e.g. different min lengths).

 

Lastly, you need to incorporate ifelse conditions in the validation. For example, if the password is not the minimum characters you do not need to test if the confirmation is equal to it. Same goes for your two validations with the email.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.