You make it very complicated :-) final TextField uname = new TextField("username", ...).add(new AttributeModifier("class", true, new Model() { public void getObject(Component c) { return uname.isValid() ? "valid" : "invalid"; } });
uname.add(new AjaxFormComponentUpdateBehavior("onblur") { public void onUpdate(AjaxRequestTarget target) { if(!uname.isValid()) { target.appendJavaScript("document.getElementById('" + uname.getMarkupId() + "').focus();"); } target.addComponent(uname); } }); Should do the trick. Note that this is from the top of my head, so things might not compile completely. Martijn On 1/14/07, Cliff Pereira <[EMAIL PROTECTED]> wrote: > Hello Mailinglist again, > > I tried to implement a simple input field where you can e.g. enter your > username. Now I want to let the background of the textfield be red while it > is not valid. And if it's finally valid it should change to green (during > the input). My problem is, that I always loose the focus on the input field > on correct input. I guess it has something to do with the addComponent > method. Here is my code so far.... > > final FormComponent usernameField; > usernameField = new TextField("username", new PropertyModel(this, > "username")); > usernameField.add(new AttributeModifier("class", true, new > PropertyModel(this, "usernamevalid"))); > usernameField.setOutputMarkupId(true); > > //this works aswell with AjaxFormSubmitBehavior. I do not see the > difference yet. > usernameField.add(new > AjaxFormComponentUpdatingBehavior("onkeyup"){ > > @Override > protected void onUpdate(AjaxRequestTarget target) { > System.out.println(username); > if (username.equals("Cliff")) { > usernameField.add(new AttributeModifier("class", true, > new Model("valid"))); > target.addComponent(usernameField); > }else { > if (usernamevalid.equals ("valid")) { > usernameField.add(new AttributeModifier("class", > true, new Model("invalid"))); > target.addComponent(usernameField); > } > } > } > > }); > add(usernameField); > > Is there a way not to loose the focus? > Thank in advance! > Cliff > > P.S.: usernamevalid could also be a boolean variable. But somehow I feel it > is not so nice code I produced here... > > ------------------------------------------------------------------------- > Take Surveys. Earn Cash. Influence the Future of IT > Join SourceForge.net's Techsay panel and you'll get the chance to share your > opinions on IT & business topics through brief surveys - and earn cash > http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV > > _______________________________________________ > Wicket-user mailing list > Wicket-user@lists.sourceforge.net > https://lists.sourceforge.net/lists/listinfo/wicket-user > > > -- Vote for Wicket at the http://www.thebeststuffintheworld.com/vote_for/wicket Wicket 1.2.4 is as easy as 1-2-4. Download Wicket now! http://wicketframework.org ------------------------------------------------------------------------- Take Surveys. Earn Cash. Influence the Future of IT Join SourceForge.net's Techsay panel and you'll get the chance to share your opinions on IT & business topics through brief surveys - and earn cash http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV _______________________________________________ Wicket-user mailing list Wicket-user@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/wicket-user