我的表单有一个对象作为字段的问题。表单验证时,仅检测错误字段是字符串,而不是作为对象字段的限制。继承表单的对象也会发生同样的事情。
表格和对象
public class Vehiculo extends Nodo {
@Required(message = "Este campo es requerido")
private Integer capacidad;
@Required(message = "Este campo es requerido")
private boolean disponible;
private String modelo;
@OneToOne
private Propietario propietario;
...
}
public class Propietario {
@Id
private int id;
@Required(message = "Este campo es requerido")
private String nombre;
private Long telefono;
private String direccion;
...
}
field.scala.html
@(elements: helper.FieldElements)
<div class="form-group @if(elements.hasErrors) {error}">
<label class="control-label col-lg-4">@elements.label</label>
<div class="col-lg-8">
@elements.input
@if(elements.hasErrors) { <span class="help-block col-lg-6">@elements.errors.mkString(", ") </span> }
</div>
</div>
视图
@form(action = routes.Administrador.ingresarVehiculo(),'class -> "form-horizontal", 'id -> "inline-validate"){
<fieldset>
@inputText(field=vehiculoForm("propietario.nombre"), '_label -> "Nombre Completo (*)",'class -> "form-control col-lg-6" )
@inputText(field=vehiculoForm("propietario.direccion"), '_label -> "Dirección",'class -> "form-control col-lg-6" )
@inputText(field=vehiculoForm("propietario.telefono"), '_label -> "Telefono",'class -> "form-control col-lg-6" )
@inputText(field=vehiculoForm("modelo"), '_label -> "Modelo",'class -> "form-control col-lg-6" )
@inputText(field=vehiculoForm("capacidad"), '_label -> "Capacidad (*)",'class -> "form-control col-lg-6" )
@select(field = vehiculoForm("disponible"),options = Seq("0"->"No", "1"->"Si"),'_label -> "Disponible")
@inputText(field=vehiculoForm("coordenadas.latitud"), '_label -> "Latitud (*)",'class -> "form-control col-lg-6" )
@inputText(field=vehiculoForm("coordenadas.longitud"), '_label -> "Longitud (*)",'class -> "form-control col-lg-6" )
@inputText(field=vehiculoForm("direccionReferencial"), '_label -> "Dirección Referencial",'class -> "form-control col-lg-6")
<div class="form-actions">
<button type="submit" class="btn btn-success">Registrar</button>
</div>
</fieldset>
}
我尝试将@Required放在表单中,但没有得到任何结果。我能做什么?。感谢。
答案 0 :(得分:1)
尝试在对象字段上放置javax.validation.Valid注释。像:
public class Vehiculo extends Nodo {
@Required(message = "Este campo es requerido")
private Integer capacidad;
@Required(message = "Este campo es requerido")
private boolean disponible;
private String modelo;
@Valid
@OneToOne
private Propietario propietario;
...
}