#42 method hasErrors didn't work, return to the previous state

This commit is contained in:
T-Midnight 2019-04-10 09:39:07 +04:00
parent e2e10477ac
commit 51c755ac07
2 changed files with 12 additions and 16 deletions

View File

@ -25,7 +25,6 @@ import static org.springframework.util.StringUtils.isEmpty;
import static ru.ulstu.grant.controller.Navigation.GRANTS_PAGE;
import static ru.ulstu.grant.controller.Navigation.GRANT_PAGE;
import static ru.ulstu.grant.controller.Navigation.REDIRECT_TO;
import static ru.ulstu.grant.controller.Navigation.hasErrors;
@Controller()
@ -64,10 +63,12 @@ public class GrantController {
if (grantDto.getDeadlines().isEmpty()) {
errors.rejectValue("deadlines", "errorCode", "Не может быть пустым");
}
// if (grantDto.getLeader().getId().equals(null)) {
// errors.rejectValue("leader", "errorCode", "Укажите руководителя");
// }
hasErrors(errors, GRANT_PAGE);
if (grantDto.getLeaderId().equals(null)) {
errors.rejectValue("leader", "errorCode", "Укажите руководителя");
}
if (errors.hasErrors()) {
return GRANT_PAGE;
}
grantService.save(grantDto);
return String.format(REDIRECT_TO, GRANTS_PAGE);
}
@ -75,14 +76,18 @@ public class GrantController {
@PostMapping(value = "/grant", params = "addDeadline")
public String addDeadline(@Valid GrantDto grantDto, Errors errors) {
filterEmptyDeadlines(grantDto);
hasErrors(errors, GRANT_PAGE);
if (errors.hasErrors()) {
return GRANT_PAGE;
}
grantDto.getDeadlines().add(new Deadline());
return GRANT_PAGE;
}
@PostMapping(value = "/grant", params = "createProject")
public String createProject(@Valid GrantDto grantDto, Errors errors) {
hasErrors(errors, GRANT_PAGE);
if (errors.hasErrors()) {
return GRANT_PAGE;
}
grantService.createProject(grantDto);
return GRANT_PAGE;
}

View File

@ -1,16 +1,7 @@
package ru.ulstu.grant.controller;
import org.springframework.validation.Errors;
public class Navigation {
public static final String REDIRECT_TO = "redirect:%s";
public static final String GRANTS_PAGE = "/grants/grants";
public static final String GRANT_PAGE = "/grants/grant";
public static String hasErrors(Errors errors, String page) {
if (errors.hasErrors()) {
return page;
}
return null;
}
}