Should I validate user input inside or outside of the User object when creating a new account? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Should I validate user input inside or outside of the User object when creating a new account?

When making an account system I have two objects, User & Validator, and I am wondering if it is better to validate the user input outside of the User class or inside the class? For example, should it be: In signup.php: $passwordAuth = new Validator($password); $usernameAuth = new Validator($username); $user = new User; If ($usernameAuth->verify() && $passwordAuth->verify()) { $user->username = $username; $user->password = $password; $user->create(); } Or just take the above validation code and make it apart of the User create function. Then all that would be in signuo.php class is: $user = new User; $user->username = $username; $user->password = $password; $user->create();

20th Feb 2020, 1:47 PM
Colin
Colin - avatar
1 Answer
0
It's user input so it should probably be inside the User class.
21st Feb 2020, 12:14 PM
Matt