0

我对将对象作为字段的表单有疑问。表单验证时,仅检测到字符串字段的错误,而不检测对象字段的限制。继承表单的对象也会发生同样的事情。

形式与对象

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 放入表单中,但没有得到任何结果。我能做什么?。谢谢。

4

1 回答 1

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;

    ...
}
于 2013-11-04T07:05:15.813 回答