0

ZK8.5 Bean Validation

asked 2018-02-26 22:55:06 +0800

psinalberth gravatar image psinalberth
52 6

Hi,

I'm using ZK8 and I'd like to add some bean validation to my form:

I've coded this:

Validator __validator = Validation.buildDefaultValidatorFactory().getValidator();
        Set<ConstraintViolation<Person>> violations = __validator.validate(person);
        List<WrongValueException> exceptions = new ArrayList<WrongValueException>();

        for (ConstraintViolation<Pessoa> violation : violations) {
            System.out.println(violation.getPropertyPath());
            exceptions.add(new WrongValueException(violation.getMessage()));
        }

        if (exceptions.size() > 0) {
            throw new WrongValuesException(exceptions.toArray(new WrongValueException[0]));
        }`

The validation itself is working, I can check the validation messages, pointing some property cannot be null or blank.

My question is: how could I pick up every message and show it on my zul or point that message to a specific component on the page?

I have a form and a button, so when I click it, a method is invoked and inside it there is the code above.

Since I'm using ZK8, this example doesn't seem a good idea because those classes are now deprecated.

delete flag offensive retag edit

Comments

The wrongvalueexception constructor has place for a component. If you provide the component, message will be shown there.

chillworld ( 2018-02-27 03:05:48 +0800 )edit

Thanks @chillworld for replying me. It turns that I don't have a component. I'm using MVVM and in my ViewModel there is only a person object, so there is no references to any view element. I'd need something like vmsgs approach with a validator like 'formBeanValidator', but it is not available.

psinalberth ( 2018-02-27 03:46:04 +0800 )edit

3 Answers

Sort by ยป oldest newest most voted
0

answered 2018-02-28 02:19:07 +0800

psinalberth gravatar image psinalberth
52 6

Thanks! It worked! Now, could you tell me what am I doing wrong? I'm testing formBeanValidator using ZKEval archetype, the validation is working but even if I fill some property, the validation message is still appearing.

<div class="section" viewModel="@id('vm') @init('br.ps.teste.viewmodel.PersonViewModel')" validationMessages="@id('vmsgs')">
        <button sclass="button is-info" label="Save" onClick="@command('save')" />

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

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

            <div class="column is-8 is-offset-2">
                <div class="field">
                    <label sclass="label" value="Address" />
                    <div class="control">
                      <textbox sclass="@load(not empty vmsgs['p_address'] ? 'input is-danger': 'input')" value="@bind(fx.address)"/>
                      <label sclass="help is-danger" value="@load(vmsgs['p_address'])" />
                    </div>
                </div>
            </div>

            <div class="column is-8 is-offset-2">
                <div class="columns">

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

                    <div class="column is-6">
                        <div class="field">
                            <label sclass="label" value="Weight" />
                            <div class="control">
                              <decimalbox sclass="@load(not empty vmsgs['p_weight'] ? 'input is-danger': 'input')" value="@bind(fx.weight)" />
                              <label sclass="help is-danger" value="@load(vmsgs['p_weight'])" />
                            </div>
                        </div>
                    </div>

                </div>
            </div>
        </div>
    </div>

I took the markup from MVVM Book

<window id="win" apply="org.zkoss.bind.BindComposer" viewModel="@id('vm') @init(foo.MyViewModel)"
    validationMessages="@id('vmsgs')">
    <grid width="600px" form="@id('fx') @load(vm.user) @save(vm.user, after='save')
        @validator('formBeanValidator', prefix='p_')">
        <textbox value="@bind(fx.firstName)"/>
        <label value="@load(vmsgs['p_firstName'])"/>
    </grid>
<!--more components-->
</window>

It looks like the object going to validation is always null. I have a button which is intented to invoke a method to create a new instance of Person, so I click it and the object is created. Even when I don't, the validation process is invoked.

link publish delete flag offensive edit

Comments

I'll check it out tomorrow

chillworld ( 2018-02-28 03:16:55 +0800 )edit

is it possible to create a fiddle with that code? http://zkfiddle.org/ just give it a name when it works and save it. The link will change and post that link here.

chillworld ( 2018-03-01 14:12:40 +0800 )edit

Hi. Here it is the fiddle. Since I'm using JSR Validation, it won't work properly because the imports cannot be found, but this is the code I'm testing with FormBeanValidator from ZK 8.5 EE-Eval archetype.

http://zkfiddle.org/sample/8jsig4/9-formBeanValidator

psinalberth ( 2018-03-01 19:02:29 +0800 )edit

thx fro the fiddle. The reason why it doesn't work is because you have no reference to : vmsgs['p_addres']. You create a WrongValueException with an error message without saying where it belongs. Normally for WrongValueException, you need a component as reference.

chillworld ( 2018-03-01 19:31:28 +0800 )edit

otherwise you use AbstractValidator and put addInvalidMessage with the context. The context provides the location.

chillworld ( 2018-03-01 19:32:41 +0800 )edit
0

answered 2018-02-27 03:58:06 +0800

psinalberth gravatar image psinalberth
52 6

Thanks @chillworld for replying me. It turns that I don't have a component. I'm using MVVM and in my ViewModel there is only a person object, so there is no references to any view element. I'd need something like vmsgs approach with a validator like 'formBeanValidator', but it is available only for EE version:

<div class="column is-8 is-offset-2">
                <div class="field">
                    <label sclass="label" value="Name" />
                    <div class="control">
                      <textbox sclass="input" value="@bind(vm.person.name) @validator('beanValidator', key='name')"/>
                      <label sclass="help is-danger" value="@load(vmsgs['name'])" />
                    </div>
                </div>
            </div>

By using beanValidator it works but when I have many properties to bind, it will be a difficult task.

As I said, the example on my previous post uses BindingValidateEvent to get the components, values and binding but all those classes are now deprecated. Is there a similar approach to do that using MVVM?

link publish delete flag offensive edit
0

answered 2018-02-27 14:23:21 +0800

chillworld gravatar image chillworld flag of Belgium
5367 4 9
https://github.com/chillw...

This still runs under 8.5 in the fiddle :

http://zkfiddle.org/sample/2b78rsn/2-MVVM-sclass-for-validation-error#source-1

Chill.

link publish delete flag offensive edit
Your answer
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
1 follower

RSS

Stats

Asked: 2018-02-26 22:55:06 +0800

Seen: 21 times

Last updated: Feb 28 '18

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