form validate code refactoring