mgordon 47 posts since
Oct 17, 2007
4.
Re: How to update background color of controls with validation? Aug 20, 2008 6:50 PM

in response to:
fukuta
Here are a couple of other solutions.
The simplest is to use dialog-on-finished?=false. There are two buttons in this example, one to simulate updating the field and one to simulate submitting the form. The update uses set-value-with-events to trigger the validation. I added the second button because using dialog-on-finished?=false means you won't get any dialog until you call validate-dialog. I put in the DialogValidator on the Dialog because this makes sure that validation occurs the first time the Dialog is displayed.
This is the simplest solution if you don't want any dialogs until the user presses a button.
{def tf = {TextField
width = 3cm,
{validate-with {StringValidator max-chars = 3},
dialog-on-finished? = false
}
}
}
{Dialog
{spaced-vbox
tf,
{CommandButton
label = "update",
{on Action do
{tf.set-value-with-events "too long"}
}
},
{ok-button
{on Action do
{if {validate-dialog {non-null tf.dialog}} then
{popup-message "submit"}
}
}
}
},
{validate-with {DialogValidator}}
}
The second solution is based on yours, but I set it up to use the same ValidationController as the target is using. In this case we don't need to use set-value-with-events because the validate-target proc will fire the validation events.
{define-proc {validate-target
controller:ValidationController, target:ActiveTraversor
}:void
let v:Validate = {Validate controller, partial? = true, current? = false}
{target.handle-event v}
let vc:ValidationComplete = {ValidationComplete.from-Validate v, false}
{target.handle-event vc}
}
{def svc =
{ValidationController {StringValidator max-chars = 3}}
}
{def tf2 = {TextField width = 3cm, svc.handler}}
{Dialog
{spaced-vbox
tf2,
{CommandButton
label = "update",
{on Action do
set tf2.value = "too long"
{validate-target svc, tf2}
}
},
{ok-button
{on Action do
{if {validate-dialog {non-null tf2.dialog}} then
{popup-message "submit"}
}
}
}
},
{validate-with {DialogValidator}}
}