MasterACE14 Posted July 29, 2011 Share Posted July 29, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/243153-maintainable-form-validation/ Share on other sites More sharing options...
Psycho Posted July 29, 2011 Share Posted July 29, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/243153-maintainable-form-validation/#findComment-1248904 Share on other sites More sharing options...
MasterACE14 Posted July 29, 2011 Author Share Posted July 29, 2011 okay, I will run with the functions idea, and I didn't realise I had slipped up with the optimisations and basic logic, thanks for pointing that out. Cheers, Ace Quote Link to comment https://forums.phpfreaks.com/topic/243153-maintainable-form-validation/#findComment-1248907 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.