0

Prevent Save Data in Invalid Form

asked 2015-08-18 07:10:42 +0800

fanjavaid gravatar image fanjavaid
1

Hello, I try to create simple User Registration using Zkoss, and implement MVVM Concept on it. When i try to emptying some fields, Zkoss validate it but the Submit value still passed to ViewModel. And i get null value.

How to prevent submitted value pass to ViewModel when found invalid data?

Here is my index.zul

<zk>
    <window border="normal" width="400px" title="Create New Account"
        apply="org.zkoss.bind.BindComposer" 
        viewModel="@id('vm') @init('com.fanjavaid.mvvm.viewmodel.UserViewModel')">

        <grid hflex="1">
            <columns visible="false">
                <column />
                <column />
            </columns>

            <auxhead>
                <auxheader colspan="2" label="Registration Form (MVVM)" style="font-size:16px" image="/images/vcard_add.png" />
            </auxhead>

            <rows>
                <row>
                    User Name <textbox id="username" hflex="1" constraint="No Empty" value="@bind(vm.username)" />
                </row>
                <row>
                    Gender
                    <radiogroup id="genderRadio" selectedItem="@bind(vm.gender) @load(vm.gender)">
                        <radio label="Male" value="male" image="/images/user.png" />
                        <radio label="Female" value="female" image="/images/user_female.png" />
                    </radiogroup>
                </row>
                <row>
                    Birthday <datebox id="birthdayBox" hflex="1" value="@bind(vm.birthday)" constraint="no empty, no today, no future"/>
                </row>
                <row spans="2" align="center">
                    <hlayout>
                        <checkbox id="acceptTermCheckbox" checked="@bind(vm.agree)" /> Accept Term of Use
                    </hlayout>
                </row>
                <row spans="2" align="right">
                    <hlayout>
                        <button id="resetButton" label="Reset" />
                        <button id="submitButton" label="Submit" disabled="@load(empty vm.username) @load(empty vm.birthday) @load(!vm.agree) @save(!vm.agree)" onClick="@command('save')" />
                    </hlayout>
                </row>
            </rows>
        </grid>
    </window>
</zk>

And here is my ViewModel :

public class UserViewModel {
    private String username;

    private String gender;

    private Date birthday;

    private List<User> userList = new ArrayList<User>();

    private boolean agree;

    public UserViewModel() {
        this.gender = "male";
    }

    @Command
    @NotifyChange("userList")
    public void save() {
        userList.add(new User(username, gender, birthday));

        System.out.println("Updated User List : ");
        for (User user : userList) {
            System.out.println(user.toString());
        }
        System.out.println("---");
    }
...

Thank you...

delete flag offensive retag edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2015-08-19 07:22:42 +0800

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

Just use the middle object provided by ZK.

You can read it in this link :

http://books.zkoss.org/zk-mvvm-book/8.0/databinding/formbinding.html

Greetz 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
2 followers

RSS

Stats

Asked: 2015-08-18 07:10:42 +0800

Seen: 31 times

Last updated: Aug 19 '15

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