|
11 | 11 | import javafx.fxml.FXML; |
12 | 12 | import javafx.scene.control.*; |
13 | 13 |
|
14 | | - |
15 | 14 | public class UserCreateFormController extends Controller { |
16 | 15 |
|
17 | | - @FXML |
18 | | - private TextField emailField; |
19 | | - @FXML private PasswordField passwordField; |
20 | | - @FXML private Label errorLabel; |
21 | | - @FXML private Button submitButton; |
22 | | - @FXML private ChoiceBox<String> rolesChoiceBox; |
23 | | - @FXML private TextField firstNameField; |
24 | | - @FXML private TextField lastNameField; |
25 | | - |
26 | | - @FXML |
27 | | - public void initialize() { |
28 | | - // Способ 1: через setItems |
29 | | - rolesChoiceBox.setItems(FXCollections.observableArrayList( |
30 | | - Role.USER.toString(), |
31 | | - Role.MODER.toString(), |
32 | | - Role.ADMIN.toString() |
33 | | - )); |
34 | | - // Установить значение по умолчанию (опционально) |
35 | | - rolesChoiceBox.setValue(Role.USER.toString()); |
36 | | - |
37 | | - System.out.println(rolesChoiceBox.getValue()); |
38 | | - |
| 16 | + @FXML private TextField emailField; |
| 17 | + @FXML private PasswordField passwordField; |
| 18 | + @FXML private Label errorLabel; |
| 19 | + @FXML private Button submitButton; |
| 20 | + @FXML private ChoiceBox<String> rolesChoiceBox; |
| 21 | + @FXML private TextField firstNameField; |
| 22 | + @FXML private TextField lastNameField; |
| 23 | + |
| 24 | + @FXML |
| 25 | + public void initialize() { |
| 26 | + // Способ 1: через setItems |
| 27 | + rolesChoiceBox.setItems( |
| 28 | + FXCollections.observableArrayList( |
| 29 | + Role.USER.toString(), Role.MODER.toString(), Role.ADMIN.toString())); |
| 30 | + // Установить значение по умолчанию (опционально) |
| 31 | + rolesChoiceBox.setValue(Role.USER.toString()); |
| 32 | + |
| 33 | + System.out.println(rolesChoiceBox.getValue()); |
| 34 | + } |
| 35 | + |
| 36 | + @FXML |
| 37 | + private void onSubmit(ActionEvent event) { |
| 38 | + String email = emailField.getText() == null ? "" : emailField.getText().trim(); |
| 39 | + String password = passwordField.getText() == null ? "" : passwordField.getText(); |
| 40 | + String roleStirng = rolesChoiceBox.getValue() == null ? "" : rolesChoiceBox.getValue(); |
| 41 | + String firstName = firstNameField.getText() == null ? "" : firstNameField.getText().trim(); |
| 42 | + String lastName = lastNameField.getText() == null ? "" : lastNameField.getText().trim(); |
| 43 | + |
| 44 | + if (email.isBlank() |
| 45 | + || password.isBlank() |
| 46 | + || roleStirng.isBlank() |
| 47 | + || firstName.isBlank() |
| 48 | + || lastName.isBlank()) { |
| 49 | + showError("Заполните все поля"); |
| 50 | + return; |
39 | 51 | } |
40 | 52 |
|
41 | | - @FXML |
42 | | - private void onSubmit(ActionEvent event) { |
43 | | - String email = emailField.getText() == null ? "" : emailField.getText().trim(); |
44 | | - String password = passwordField.getText() == null ? "" : passwordField.getText(); |
45 | | - String roleStirng = rolesChoiceBox.getValue() == null ? "" : rolesChoiceBox.getValue(); |
46 | | - String firstName = firstNameField.getText() == null ? "" : firstNameField.getText().trim(); |
47 | | - String lastName = lastNameField.getText() == null ? "" : lastNameField.getText().trim(); |
48 | | - |
49 | | - if (email.isBlank() || password.isBlank() || roleStirng.isBlank() || firstName.isBlank() || lastName.isBlank()) { |
50 | | - showError("Заполните все поля"); |
51 | | - return; |
52 | | - } |
53 | | - |
54 | | - if (!(roleStirng.equals(Role.USER.toString()) || roleStirng.equals(Role.MODER.toString()) |
55 | | - || roleStirng.equals(Role.ADMIN.toString()))) { |
56 | | - showError("Роль не определена"); |
57 | | - return; |
58 | | - } |
59 | | - |
60 | | - |
61 | | - |
62 | | - Role role = Role.valueOf(roleStirng); |
63 | | - |
64 | | - String emailError = EmailValidator.validate(email); |
65 | | - if (emailError != null) { |
66 | | - showError(emailError); |
67 | | - return; |
68 | | - } |
69 | | - |
70 | | - try { |
71 | | - RegisterData registerData = new RegisterData(email, password, role, firstName, lastName); |
72 | | - User user = services.getUsersService().create(registerData); |
73 | | - if (user == null) { |
74 | | - return; |
75 | | - } |
76 | | - hideError(); |
77 | | - navigator.goTo("/fxml/UserList.fxml"); |
78 | | - } catch (ServiceException e) { |
79 | | - showError(e.getMessage()); |
80 | | - } |
| 53 | + if (!(roleStirng.equals(Role.USER.toString()) |
| 54 | + || roleStirng.equals(Role.MODER.toString()) |
| 55 | + || roleStirng.equals(Role.ADMIN.toString()))) { |
| 56 | + showError("Роль не определена"); |
| 57 | + return; |
81 | 58 | } |
82 | 59 |
|
83 | | - @FXML |
84 | | - void onCancel(ActionEvent event) { |
85 | | - navigator.goTo("/fxml/UserList.fxml"); |
86 | | - } |
| 60 | + Role role = Role.valueOf(roleStirng); |
87 | 61 |
|
88 | | - private void showError(String message) { |
89 | | - errorLabel.setText(message); |
90 | | - errorLabel.setVisible(true); |
91 | | - errorLabel.setManaged(true); |
| 62 | + String emailError = EmailValidator.validate(email); |
| 63 | + if (emailError != null) { |
| 64 | + showError(emailError); |
| 65 | + return; |
92 | 66 | } |
93 | 67 |
|
94 | | - private void hideError() { |
95 | | - errorLabel.setText(""); |
96 | | - errorLabel.setVisible(false); |
97 | | - errorLabel.setManaged(false); |
| 68 | + try { |
| 69 | + RegisterData registerData = new RegisterData(email, password, role, firstName, lastName); |
| 70 | + User user = services.getUsersService().create(registerData); |
| 71 | + if (user == null) { |
| 72 | + return; |
| 73 | + } |
| 74 | + hideError(); |
| 75 | + navigator.goTo("/fxml/UserList.fxml"); |
| 76 | + } catch (ServiceException e) { |
| 77 | + showError(e.getMessage()); |
98 | 78 | } |
| 79 | + } |
| 80 | + |
| 81 | + @FXML |
| 82 | + void onCancel(ActionEvent event) { |
| 83 | + navigator.goTo("/fxml/UserList.fxml"); |
| 84 | + } |
| 85 | + |
| 86 | + private void showError(String message) { |
| 87 | + errorLabel.setText(message); |
| 88 | + errorLabel.setVisible(true); |
| 89 | + errorLabel.setManaged(true); |
| 90 | + } |
| 91 | + |
| 92 | + private void hideError() { |
| 93 | + errorLabel.setText(""); |
| 94 | + errorLabel.setVisible(false); |
| 95 | + errorLabel.setManaged(false); |
| 96 | + } |
99 | 97 | } |
0 commit comments