Answer the question
In order to leave comments, you need to log in
How to duplicate field for comparison in thymeleaf?
hello i have a registration form
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Register</title>
</head>
<body>
<!-- Include _menu.html -->
<th:block th:include="/_menu"></th:block>
<h1>Create a Person:</h1>
<form th:action="@{/register}"
th:object="${personForm}" method="POST">
Login:
<input type="text" th:field="*{name}" />
<br/>
Email:
<input type="text" th:field="*{email}" />
<br/>
Password:
<input type="text" th:field="*{password}">
<br/>
Confirm password:
<input type="text" th:field="*{doublePassword}">
<input type="submit" value="Create" />
</form>
</body>
</html>
@RequestMapping(value = { "/register" }, method = RequestMethod.GET)
public String showAddPersonPage(Model model) {
UserForm personForm = new UserForm();
model.addAttribute("personForm", personForm);
return "register";
}
@RequestMapping(value = {"/register"} , method = RequestMethod.POST)
public String savePerson(Model model, @ModelAttribute("personForm") UserForm personForm) {
if(personForm.checkPassword() &&
userRepository.findByEmail(personForm.getEmail()) == null &&
userRepository.findByName(personForm.getName()) == null) {
AppUser user = new AppUser(personForm.getName(),
personForm.getEmail(),
personForm.getPassword());
user.setEnabled(true);
user.setRoles(Collections.singleton(Role.USER));
userRepository.save(user);
return "home";
}
return "register";
}
Answer the question
In order to leave comments, you need to log in
Hello!
You are very confused, to be honest ... even Spring has nothing to do with it, but more basic knowledge of html + js
Here, your form:
<form th:action="@{/register}"
th:object="${personForm}" method="POST">
Login:
<input type="text" th:field="*{name}" />
<br/>
Email:
<input type="text" th:field="*{email}" />
<br/>
Password:
<input type="text" th:field="*{password}">
<br/>
Confirm password:
<input type="text" th:field="*{doublePassword}">
<input type="submit" value="Create" />
</form>
<form th:action="@{/register}"
th:object="${personForm}" method="POST">
Login:
<input type="text" th:field="*{name}" />
<br/>
Email:
<input type="email" th:field="*{email}" />
<br/>
Password:
<input type="password" th:field="*{password}">
<br/>
Confirm password:
<input type="password">
<input type="submit" value="Create" />
</form>
model.addAttribute()
and display in the template@RequestMapping(value = {"/register"} , method = RequestMethod.POST)
public String savePerson(Model model, @ModelAttribute("personForm") UserForm personForm) {
if(!personForm.getPassword.equals(personForm.getPasswordConfirmation)) {
model.addAttribute("passwordIncorrect", "Вы ввели некорректный пароль");
return "register";
}
if(personForm.checkPassword() &&
userRepository.findByEmail(personForm.getEmail()) == null &&
userRepository.findByName(personForm.getName()) == null) {
AppUser user = new AppUser(personForm.getName(),
personForm.getEmail(),
personForm.getPassword());
user.setEnabled(true);
user.setRoles(Collections.singleton(Role.USER));
userRepository.save(user);
return "home";
}
return "register";
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question