0

Form Binding + Bean Validation + AssertTrue

asked 2018-10-05 04:13:19 +0800

psinalberth gravatar image psinalberth
52 6

updated 2018-10-05 04:25:37 +0800

I'm trying to validate an object using FormBeanValidator, but I'm facing some problem with object:

Person.java

@Entity
public class Person {

    @Id
    private int id;

    @Column
    private String name;

    @Column
    private Integer age;

    @JoinColumn
    @ManyToOne
    private Address address;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @NotBlank
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @NotNull
    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Valid
    @NotNull
    public Address getAddress() {
        return address;
    }

    public void setAddress(Address adress) {
        this.address = adress;
    }

    @AssertTrue(message="age=Must be 18 or older.")
    public boolean isAgeValid() {

        if (this.getAge() == null)
            return true;

        return this.getAge().intValue() >= 18;
    }
}

Address.java

@Entity
public class Address {

    @Id
    private int id;

    @Column
    private String streetName;

    @Column
    private Integer number;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @NotBlank
    public String getStreetName() {
        return streetName;
    }

    public void setStreetName(String streetName) {
        this.streetName = streetName;
    }

    public Integer getNumber() {
        return number;
    }

    public void setNumber(Integer number) {
        this.number = number;
    }

    @AssertTrue(message="Number must be equal to 123")
    public boolean isNumberValid() {

        if (number == null)
            return true;

        return number.intValue() == 123;

    }
}

person.zul

<zk>

    <div class="content" form="@id('fx') @load(vm.person) @save(vm.person, before=vm.save) @validator('formBeanValidator', prefix='p_')">

        <label sclass="subtitle is-4" value="Edit Person"/>

        <separator />

        <div class="field">
            <label sclass="label"  value="Name" />
            <div class="control">
                <textbox class="input" value="@bind(fx.name)" />
                <label sclass="help is-danger" value="@load(vmsgs['p_name'])"/>
            </div>
        </div>

        <div class="field">
            <label sclass="label"  value="Age" />
            <div class="control">
                <intbox class="input" value="@bind(fx.age)" />
                <label sclass="help is-danger" value="@load(vmsgs['p_age'])"/>
            </div>
        </div>

        <div class="field">
            <label sclass="label"  value="Street Name" />
            <div class="control">
                <textbox class="input" value="@bind(fx.adress.streetName)" />
                <label sclass="help is-danger" value="@load(vmsgs['p_streetName'])"/>
            </div>
        </div>

        <div class="field">
            <label sclass="label"  value="Number" />
            <div class="control">
                <intbox class="input" value="@bind(fx.adress.number)" />
                <label sclass="help is-danger" value="@load(vmsgs['p_number'])"/>
                <label sclass="help is-danger" value="@load(vmsgs['p_numberValid'])"/>
            </div>
        </div>

    </div>

    <button label="Save" onClick="@command('save')"/>

</div>

</zk>

Well, I can validate all properties noted with some validation annotation: If age is null I get a message saying isn't possible to save person, but when I fill it with a value lesser than 18, I get no validation message, age is always null. I know it will be saved to person object when everything is correct, but how could I invoke this @AssertTrue? It's been a while I'm looking for some solution but I have no idea how to solve this.

delete flag offensive retag edit
Be the first one to answer this question!
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow

RSS

Stats

Asked: 2018-10-05 04:13:19 +0800

Seen: 2 times

Last updated: Oct 05

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More