0

Datebox validation

asked 2013-04-06 03:32:39 +0800

kleponboy gravatar image kleponboy flag of Indonesia
54 5
http://stupidprogrammers....

let's say i have two datebox, startDateBox and endDateBox, first i put value inside startDateBox (2/Jan/2013), then i put second value inside endDateBox, now my question is, how to set validation so that endDatebox can't set value before 2/Jan/2013

example :

startDateBox = 2/Jan/2013 endDateBox = can't set value before 2/Jan/2013

btw, sorry for my english

Thanks,

Klepon

delete flag offensive retag edit

Comments

i just figured this out. put this snippet on the code

 endDateBox.setConstraint("after "+startDatebox.getValue());
kleponboy ( 2013-04-08 04:08:49 +0800 )edit

Did you consider that the end date could be entered first? And that the fields can be edited again in any order? I also tried something similar and ended up with multiple error boxes. Sometimes it was not even possible to get rid of them again.

Matze2 ( 2013-04-08 08:27:35 +0800 )edit

2 Answers

Sort by ยป oldest newest most voted
1

answered 2013-04-08 06:48:56 +0800

iamsudhir4u gravatar image iamsudhir4u flag of India
545 7
http://corejavasupport.bl...

If you are using MVVM then below example will be helpful for you.

<zk> <window border="normal" title="hello" apply="org.zkoss.bind.BindComposer" viewmodel="@id('vm') @init('pkg$.DateValidationViewModel')">

        <datebox value="@bind(vm.beginDate)" />

        <datebox value="@bind(vm.endDate)" />

        <button id="btn" label="Validate"
            onClick="@command('validate')" />
    </window>
</zk>

DateValidationViewModel.java

import org.zkoss.zk.ui.*;
import org.zkoss.zk.ui.event.*;
import org.zkoss.zk.ui.util.*;
import org.zkoss.zk.ui.ext.*;
import org.zkoss.zk.au.*;
import org.zkoss.zk.au.out.*;
import java.sql.Timestamp;
import org.zkoss.zul.*;
import org.zkoss.bind.annotation.Command;
import org.zkoss.bind.annotation.NotifyChange;

public class DateValidationViewModel extends GenericForwardComposer {

    private Timestamp beginDate;
    private Timestamp endDate;

    public void doAfterCompose(Component comp) throws Exception {
        super.doAfterCompose(comp);
    }

    @Command
    @NotifyChange("*")
    public void validate() {
         if (beginDate != null && endDate != null && endDate.compareTo(beginDate) < 0) {
            endDate = null;
            Messagebox.show("End Date can not be less than begin date.");
        }
    }

    public Timestamp getBeginDate() {
        return beginDate;
    }

    public void setBeginDate(Timestamp beginDate) {
        this.beginDate = beginDate;
    }

    public Timestamp getEndDate() {
        return endDate;
    }

    public void setEndDate(Timestamp endDate) {
        this.endDate = endDate;
    }
}

I have created same example in Fiddle also http://zkfiddle.org/sample/77vhl8/2-Date-Validation#source-2

link publish delete flag offensive edit

Comments

it's manually validate it's not dynamically we need dynamically validate with a small code added with zk component.

hswain ( 2013-04-08 08:45:13 +0800 )edit
0

answered 2013-04-08 03:59:53 +0800

Matze2 gravatar image Matze2
773 7

updated 2013-04-08 04:00:15 +0800

If you are using MVVM, then I would use a form validator, There is an example in ZK Demo which shows how to do that.

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: 2013-04-06 03:32:39 +0800

Seen: 127 times

Last updated: Apr 08 '13

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