我想实现以下功能:如果用户写“错误”的pid,警报应该显示为:你真的用无效格式保存这个pid吗?如果用户说是,那么整个“playermber”(如果其他字段是正确的)将被保存到数据库中(即使pid的格式不正确),那么我该如何实现呢?我想补充一点 @Patern(with right regex) and message
-由于下面的模板,此消息将显示给用户,但如何添加此类对话框(是-使用无效pid保存表单,否-不保存它)。使用Spring Boot和百里香叶。
控制器中的方法
@PostMapping("/create")
public String createPlayer(@Valid @ModelAttribute("playerForm") PlayerForm playerForm,
BindingResult bindingResult,
Model model,
RedirectAttributes redirectAttributes) {
// Check for errors
if (bindingResult.hasErrors()) {
formViewPreparer.prepare(model, new PlayerFormViewPreparer.ParamObj(true, null));
return "admin/players/create";
}
// Prepare object for service layer
CreatePlayerIn createPlayerIn = CreatePlayerInMapper.fromPlayerForm(playerForm);
// Call service layer
playerService.createPlayer(createPlayerIn);
redirectAttributes.addFlashAttribute("message", "player.toast.created");
return "redirect:/admin/players";
}
球员形态类
@Getter
@Setter
public class PlayerForm {
@NotBlank
private String firstName;
@NotBlank
private String lastName;
@NotNull
private GenericState playerState;
@DateTimeFormat(pattern = "yyyy-MM-dd")
private LocalDate birthDate;
private String PID;
private Long schoolId;
private Long teamId;
private String street;
private String city;
private String zipCode;
private String phoneNumber;
private String email;
private String motherPhoneNumber;
private String motherEmail;
private String fatherPhoneNumber;
private String fatherEmail;
@DateTimeFormat(pattern = "yyyy-MM-dd")
private LocalDate validityOfMedicalExam;
@NumberFormat(style = NumberFormat.Style.NUMBER)
@Min(0)
private Integer annualFee;
private String annualFeeNote;
@NumberFormat(style = NumberFormat.Style.NUMBER)
@Min(0)
private Integer firstSemiAnnualFee;
private String firstSemiAnnualFeeNote;
@NumberFormat(style = NumberFormat.Style.NUMBER)
@Min(0)
private Integer secondSemiAnnualFee;
private String secondSemiAnnualFeeNote;
创建.html模板
<form th:object="${playerForm}"
th:action="@{/admin/players/create}"
novalidate
enctype="multipart/form-data"
method="post">
<div class="form-group col-md-6">
<input type="text" th:field="*{PID}" class="form-control" th:errorclass="is-invalid"
th:placeholder="#{player.input.PID}"/>
<label for="PID" th:text="#{player.input.PID}" class="form-control-placeholder"></label>
<div th:if="${#fields.hasErrors('PID')}" class="invalid-feedback"
th:errors="*{PID}"></div>
</div>
暂无答案!
目前还没有任何答案,快来回答吧!