Posted on 16 Aug 2022, this text provides information on Technology & Software related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.
My project is a spring mvc project.In my project i have a domain Technology which has foreign key reference.When i validating on form submit it threw error....For view part(jsp),i using form:select for viewing department in technology. How can i validate a foreign reference????? i tried below code
Here you have to implement validator for Technology object
classTechnologyValidatorextendsValidator{publicboolean supports(Class> cls){returnTechnology.class.equals(cls);}publicvoid validate(Object target,Errors errors){super.validate(target, errors);Technology tecObj=(Technology) target;//here i am assuming Technology name is REQUIRED and //NotEmpty.technology.name is in message.propertiesValidationUtils.rejectIfEmptyOrWhitespace(errors,"name","NotEmpty.technology.name");Department dept = tecObj.getDepartment();//as from from your are binding department IDif(dept==null|| dept.getId()==null|| dept.getId()==0L){
errors.rejectValue("department.id","NotEmpty.technology.department");}}}
Here you have to implement validator for Technology object
classTechnologyValidatorextendsValidator{publicboolean supports(Class> cls){returnTechnology.class.equals(cls);}publicvoid validate(Object target,Errors errors){super.validate(target, errors);Technology tecObj=(Technology) target;//here i am assuming Technology name is REQUIRED and //NotEmpty.technology.name is in message.propertiesValidationUtils.rejectIfEmptyOrWhitespace(errors,"name","NotEmpty.technology.name");Department dept = tecObj.getDepartment();//as from from your are binding department IDif(dept==null|| dept.getId()==null|| dept.getId()==0L){
errors.rejectValue("department.id","NotEmpty.technology.department");}}}
No matter what stage you're at in your education or career, TuteeHUB will help you reach the next level that
you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice
sessions to improve your knowledge and scores.
manpreet
Best Answer
2 years ago
My project is a spring mvc project.In my project i have a domain Technology which has foreign key reference.When i validating on form submit it threw error....For view part(jsp),i using form:select for viewing department in technology. How can i validate a foreign reference????? i tried below code
domain
message.properties
Technology.jsp
controller
ERROR