Algunas validaciones

parent 37797605
...@@ -47,6 +47,16 @@ ...@@ -47,6 +47,16 @@
<artifactId>spring-boot-devtools</artifactId> <artifactId>spring-boot-devtools</artifactId>
<optional>true</optional> <optional>true</optional>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.0.13.Final</version>
</dependency>
</dependencies> </dependencies>
<build> <build>
......
...@@ -4,19 +4,22 @@ import java.text.SimpleDateFormat; ...@@ -4,19 +4,22 @@ import java.text.SimpleDateFormat;
import java.util.Date; import java.util.Date;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.validation.ConstraintViolationException;
import com.roshka.modelo.Experiencia; import com.roshka.modelo.Experiencia;
import com.roshka.modelo.Postulante; import com.roshka.modelo.Postulante;
import com.roshka.repositorio.PostulanteRepository; import com.roshka.repositorio.PostulanteRepository;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
@Controller @Controller
public class PostulanteController { public class PostulanteController {
@Autowired @Autowired
PostulanteRepository post; PostulanteRepository post;
...@@ -42,4 +45,20 @@ public class PostulanteController { ...@@ -42,4 +45,20 @@ public class PostulanteController {
return "redirect:/"; return "redirect:/";
} }
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler({MethodArgumentNotValidException.class})
public ResponseEntity<String> handleValidationExceptions(
MethodArgumentNotValidException ex) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST)
.body(ex.getMessage());
}
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler({ConstraintViolationException.class})
public ResponseEntity<String> handleValidationExceptions2(
ConstraintViolationException ex) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST)
.body(ex.getMessage());
}
} }
package com.roshka.modelo; package com.roshka.modelo;
import javax.persistence.*; import javax.persistence.*;
import javax.validation.constraints.*;
import java.text.ParseException; import java.text.ParseException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
...@@ -17,36 +18,52 @@ public class Postulante { ...@@ -17,36 +18,52 @@ public class Postulante {
private long id; private long id;
@Column(name="nombre") @Column(name="nombre")
@NotBlank
@Size(max = 255)
private String nombre; private String nombre;
@Column(name = "apellido") @Column(name = "apellido")
@NotBlank
@Size(max = 255)
private String apellido; private String apellido;
@Column(name = "ci") @Column(name = "ci")
@NotBlank
@Size(max = 120)
private String ci; private String ci;
@Column(name = "correo") @Column(name = "correo")
@NotBlank
@Email(message = "Formato incorrecto de correo")
private String correo; private String correo;
@Column(name = "ciudad") @Column(name = "ciudad")
@NotBlank
@Size(max = 120)
private String ciudad; private String ciudad;
@Column(name = "telefono") @Column(name = "telefono")
@NotBlank
private String telefono; private String telefono;
@Column(name = "fecha_nacimiento") @Column(name = "fecha_nacimiento")
@NotNull
@Past
private Date fechaNacimiento; private Date fechaNacimiento;
@Column(name = "resumen") @Column(name = "resumen")
private String resumen; private String resumen;
@Column(name = "nivel_ingles") @Column(name = "nivel_ingles")
@Min(value = 1)
@Max(value = 5)
private long nivelIngles; private long nivelIngles;
@Column(name = "curriculum") @Column(name = "curriculum")
private String curriculum; private String curriculum;
@Column(name = "modalidad") @Column(name = "modalidad")
@NotNull
private String modalidad; private String modalidad;
@Column(name = "disponibilidad") @Column(name = "disponibilidad")
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment