Attribute th:field replaces the th:name attribute in the field with radio buttons with its value. The problem is that I have a great many groups with radio buttons, and the field in which they enter information via th:field one. And it turns out that you can select only one of all the radio buttons, and not one in each group.
<form action="#" th:action="@{/user/save_answer}" method="post">
<div th:each="question : ${questions}">
<span th:text="${question.getValueQuestion()}">question</span>
<ul>
<li th:each="variant : ${question.getVariants()}">
<input th:type="${question.choiceType}" th:id="${variant.id}" th:name="${question.getValueQuestion()}" th:field="${user.variants}" th:value="${variant.id}" >
<label th:for="${variant.id}" th:text="${variant.getValueVariant()}">variant </label>
</li>
</ul>
</div>
<button type="submit">Complete</button>
</form>
The bottom line is that I have a questionnaire with questions and in each question there are several possible answers. As you can see, I implemented this with a cycle of answer options within a cycle of questions. Due to the fact that th:name = "question name " is ignored and is taken th:field = "array with user answers", which we have for all answer options, radio buttons are obtained with the same field name = "field with answer options". And thus, as if we can choose only one answer from all the options that are present in all the questions.
${user.variants}
- in this field, which belongs to the USER entity, we enter the Variables objects so that later we can see the variants of his answers, the class is described below:
@Data
@Entity
@Table(name = "users")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "email")
private String email;
@Column(name = "password")
private String password;
@Column(name = "first_name")
private String firstName;
@Column(name = "last_name")
private String lastName;
@Enumerated(value = EnumType.STRING)
@Column(name = "role")
private Role role;
@Enumerated(value = EnumType.STRING)
@Column(name = "status")
private Status status;
@ManyToMany(cascade = { CascadeType.ALL })
@JoinTable(
name = "user_variant",
joinColumns = { @JoinColumn(name = "user_id") },
inverseJoinColumns = { @JoinColumn(name = "variant_id") }
)
private Set<Variants> variants = new HashSet<>();
}
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private QuestionnairesRepository questionnairesRepository;
@Autowired
private QuestionsRepository questionsRepository;
@Autowired
private VariantsRepository variantsRepository;
@Autowired
private UserRepository userRepository;
@GetMapping
public String getAllQuestionnaires(Model model){
model.addAttribute("questionnaires",questionnairesRepository.findAll());
return "/user_questionnaires";
}
@GetMapping("/questionnaire/{id}")
// @PreAuthorize("hasAuthority('developers:read')")
public String listQuestion(@PathVariable Long id, Model model){
// получаем авторизированного пользователя
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
String currentPrincipalName = authentication.getName();
Questionnaires questionnaire = questionnairesRepository.findById(id).get();
model.addAttribute("questionnaire", questionnaire);
model.addAttribute("user", userRepository.findByEmail(currentPrincipalName).get());
Iterable<Questions> questionsFromBD = questionsRepository.findAllByQuestionnaire_Id(id);
model.addAttribute("questions", questionsFromBD);
return "/user_variants";
}
@PostMapping("/save_answer")
public String saveAnswer(@ModelAttribute("user") User user, Model model){
System.out.println(user);
return "/success";
}
}
There are no errors. here is the html of the page itself, it shows that all the buttons have the same "name"
What needs to be fixed so that one radio button is selected for each question and when filling out the answer options are entered in the user options field?