Since recently I am building web application in PHP. In purpose of learning I am building it from scratch (without using a framework).
I have a question regarding form validation (registration and login form to be specific).
Is it OK to use jQuery to validate form fields (e.g. check if all fields are full, check if email is correctly written,...) and then, if everything is right, submit form to my controller?
class RegistrationController extends Controller {
public function __construct($data = array()) {
userService = new UserService();
parent::__construct($data);
}
public function index() {
// form fields are correctly filled in
if($_SERVER['REQUEST_METHOD'] == 'POST') {
$firstName = $_POST['reg-form-first-name'];
$lastName = $_POST['reg-form-last-name'];
$email = $_POST['reg-form-email'];
$password = $_POST['reg-form-password'];
userService->createNewUser($firstName,...);
}
}
}
Or should I validate form inside my model in PHP? If that is the right way, can you please explain how should I print out error messages beneath the form fields if something went wrong?
What is the best practice in this situation? What provides best UX?