Skip to content

Commit 0898234

Browse files
committed
Improve register form
1 parent 8971852 commit 0898234

File tree

6 files changed

+93
-90
lines changed

6 files changed

+93
-90
lines changed

src/main/java/com/github/throyer/common/springboot/controllers/app/AppController.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.github.throyer.common.springboot.controllers.app;
22

33
import com.github.throyer.common.springboot.domain.repositories.UserRepository;
4-
import com.github.throyer.common.springboot.domain.services.security.SecurityService;
4+
import static com.github.throyer.common.springboot.domain.services.security.SecurityService.authorized;
55
import org.springframework.beans.factory.annotation.Autowired;
66
import org.springframework.stereotype.Controller;
77
import org.springframework.ui.Model;
@@ -17,10 +17,10 @@ public class AppController {
1717

1818
@GetMapping
1919
public String index(Model model) {
20-
SecurityService.authorized()
21-
.ifPresent(session -> repository.findById(session.getId())
22-
.ifPresent(user -> model.addAttribute("name", user.getName()))
23-
);
20+
authorized()
21+
.ifPresent(session -> repository.findNameById(session.getId())
22+
.ifPresent(name -> model.addAttribute("name", name)));
23+
2424
return "app/index";
2525
}
2626
}

src/main/java/com/github/throyer/common/springboot/domain/repositories/UserRepository.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ default void deleteAll(Iterable<? extends User> entities) {
5252
public Boolean existsByEmail(String email);
5353

5454
public Optional<User> findOptionalByIdAndDeletedAtIsNull(Long id);
55+
56+
@Query("""
57+
SELECT user.name FROM #{#entityName} user
58+
WHERE user.id = ?1
59+
""")
60+
public Optional<String> findNameById(Long id);
5561

5662
@Query("""
5763
SELECT user FROM #{#entityName} user

src/main/java/com/github/throyer/common/springboot/domain/services/user/dto/CreateUserApp.java

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,18 @@
1111
import static com.github.throyer.common.springboot.domain.services.user.dto.CreateUserApi.STRONG_PASSWORD_MESSAGE;
1212

1313
import javax.validation.constraints.Email;
14+
import javax.validation.constraints.Min;
1415
import javax.validation.constraints.NotEmpty;
1516
import javax.validation.constraints.Pattern;
17+
import javax.validation.constraints.Size;
1618

1719
import lombok.Data;
1820

1921
import org.springframework.validation.BindingResult;
20-
import org.springframework.validation.ObjectError;
2122

2223
@Data
2324
public class CreateUserApp implements HasEmail {
2425

25-
private static final String CONFIRM_ERROR_MESSAGE = "Valor informado na confirmação de senha invalido.";
26-
2726
@NotEmpty(message = "Por favor, forneça um nome.")
2827
private String name;
2928

@@ -32,17 +31,10 @@ public class CreateUserApp implements HasEmail {
3231
private String email;
3332

3433
@NotEmpty(message = "Por favor, forneça uma senha.")
35-
@Pattern(regexp = STRONG_PASSWORD, message = STRONG_PASSWORD_MESSAGE)
34+
@Size(min = 8, max = 255, message = "A senha deve conter no minimo {min} caracteres.")
3635
private String password;
3736

38-
@NotEmpty(message = "Por favor, confirme a senha.")
39-
private String confirmPassword;
40-
4137
public void validate(BindingResult result) {
42-
if (!getConfirmPassword().equals(getPassword())) {
43-
result.addError(new ObjectError("confirmPassowrd", CONFIRM_ERROR_MESSAGE));
44-
}
45-
4638
validateEmailUniqueness(this, result);
4739
}
4840

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
main.container {
2+
display: flex;
3+
justify-content: center;
4+
align-items: center;
5+
height: 90vh;
6+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<form
2+
novalidate
3+
method="POST"
4+
autocomplete="off"
5+
class="needs-validation shadow-sm p-3 mb-5 bg-body rounded"
6+
th:object="${user}"
7+
th:action="@{/app/register}"
8+
th:fragment="form-register"
9+
>
10+
<div class="row gap-3">
11+
<div class="col-md-12">
12+
<h1 class="fs-3 text-center">
13+
<i class="far fa-user"></i>
14+
Register
15+
</h1>
16+
</div>
17+
<div class="col-md-12">
18+
<input
19+
required
20+
type="text"
21+
autocomplete="name"
22+
placeholder="Name"
23+
id="input_name"
24+
class="form-control form-control-sm"
25+
th:field="*{name}"
26+
>
27+
<div class="invalid-feedback">
28+
Por favor, informe o nome.
29+
</div>
30+
</div>
31+
<div class="col-md-12">
32+
<input
33+
required
34+
type="email"
35+
autocomplete="email"
36+
placeholder="E-mail"
37+
id="input_email"
38+
class="form-control form-control-sm"
39+
th:field="*{email}"
40+
>
41+
<div class="invalid-feedback">
42+
Por favor, informe o e-mail.
43+
</div>
44+
</div>
45+
<div class="col-md-12">
46+
<input
47+
required
48+
type="password"
49+
autocomplete="new-password"
50+
placeholder="Password"
51+
class="form-control form-control-sm"
52+
id="input_password"
53+
th:field="*{password}"
54+
>
55+
<div class="invalid-feedback">
56+
Por favor, informe a senha.
57+
</div>
58+
</div>
59+
</div>
60+
<div class="row">
61+
<div class="col-md-12 mt-4 d-grid gap-2">
62+
<button class="btn btn-primary" type="submit">Submit</button>
63+
</div>
64+
</div>
65+
</form>
Lines changed: 8 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,13 @@
1-
<layout th:replace="~{app/fragments/layout :: layout(~{::title}, ~{::link}, ~{::main}, ~{::script}, ~{})}">
1+
<layout th:replace="~{app/fragments/layout :: layout(~{::title}, ~{::links}, ~{::main}, ~{::script}, ~{})}">
22
<title>Register</title>
3-
<link rel="stylesheet" type="text/css" th:href="@{/css/forms.css}">
3+
<links>
4+
<link rel="stylesheet" type="text/css" th:href="@{/css/forms.css}">
5+
<link rel="stylesheet" type="text/css" th:href="@{/css/register.css}">
6+
</links>
47
<main class="container">
5-
<i class="fas fa-user fa-6x mb-2"></i>
6-
<h1>Register</h1>
7-
<form th:object="${user}" class="needs-validation" novalidate th:action="@{/app/register}" method="POST">
8-
<div class="row">
9-
<div class="col-md-12 mt-4">
10-
<label for="input_name" class="form-label">Name</label>
11-
<input
12-
th:field="*{name}"
13-
type="text"
14-
class="form-control"
15-
id="input_name"
16-
autocomplete="name"
17-
required
18-
>
19-
<div class="invalid-feedback">
20-
Por favor, informe o nome.
21-
</div>
22-
</div>
23-
</div>
24-
<div class="row">
25-
<div class="col-md-12 mt-4">
26-
<label for="input_email" class="form-label">E-mail</label>
27-
<input
28-
th:field="*{email}"
29-
type="email"
30-
autocomplete="email"
31-
class="form-control"
32-
id="input_email"
33-
required
34-
>
35-
<div class="invalid-feedback">
36-
Por favor, informe o e-mail.
37-
</div>
38-
</div>
39-
</div>
40-
<div class="row">
41-
<div class="col-md-12 mt-4">
42-
<label for="input_password" class="form-label">Password</label>
43-
<input
44-
th:field="*{password}"
45-
type="password"
46-
autocomplete="new-password"
47-
class="form-control"
48-
id="input_password"
49-
required
50-
>
51-
<div class="invalid-feedback">
52-
Por favor, informe a senha.
53-
</div>
54-
</div>
55-
</div>
56-
<div class="row">
57-
<div class="col-md-12 mt-4">
58-
<label for="input_confirm_password" class="form-label">Confirm password</label>
59-
<input
60-
th:field="*{confirmPassword}"
61-
type="password"
62-
class="form-control"
63-
id="input_confirm_password"
64-
required
65-
>
66-
<div class="invalid-feedback">
67-
Por favor, confirme a senha.
68-
</div>
69-
</div>
70-
</div>
71-
<div class="row">
72-
<div class="col-md-12 mt-4 d-grid gap-2">
73-
<button class="btn btn-primary" type="submit">Submit</button>
74-
</div>
75-
</div>
8+
<form
9+
th:replace="~{__${execInfo.templateName}__/../fragments/form-register :: form-register}">
7610
</form>
7711
</main>
7812
<script language="javascript" th:src="@{/js/forms.js}"></script>
79-
</layout>
13+
</layout>

0 commit comments

Comments
 (0)