This Question is Answered

14 "helpful" answers available (3 pts)
7 Replies Last post: Aug 24, 2008 8:35 AM by fukuta

How to update background color of controls with validation?

Aug 6, 2008 1:08 AM

Click to view fukuta's profile BlackBelt fukuta 105 posts since
Oct 17, 2007
I'm using validation API with some controls. Now I set a control's value, and I'd like to get the background of the control updated depending on a result of its validation. A background seems to be updated automatically if the value is set before the control is attached to a graphical hierachy, but it isn't if the control already exists on a screen.
I don't want to use set-value-with-events method because it fires value changed/finished events and shows message dialog. How can I do this trick?
Thanks.
Click to view mgordon's profile Curl mgordon 47 posts since
Oct 17, 2007
1. Re: How to update background color of controls with validation? Aug 7, 2008 9:35 AM
If the controls are in a Dialog d, you can call {validate-dialog d, partial? = true}. The partial?=true flag tells the system that the user hasn't changed anything, and it consequently won't post a dialog. But I believe it will do the colorization you're looking for.
Click to view fukuta's profile BlackBelt fukuta 105 posts since
Oct 17, 2007
2. Re: How to update background color of controls with validation? Aug 10, 2008 2:28 AM
in response to: mgordon
I made a simple sample as below, but validate-dialog with the flag partial?=true causes dialog popup. Anything wrong with this?

{curl 6.0 applet}

{def tf = {TextField
              width = 3cm,
              {validate-with {StringValidator max-chars = 3}}
          }
}

{Dialog
    {spaced-vbox
        tf,
        {CommandButton
            label = "update",
            {on Action do
                set tf.value = "too long"
                {validate-dialog {non-null tf.dialog}, partial? = true}
            }
        }
    }
}


In this case, I want to update a TextField's value and control-content-background but don't want to show a error message dialog.
Click to view fukuta's profile BlackBelt fukuta 105 posts since
Oct 17, 2007
3. Re: How to update background color of controls with validation? Aug 20, 2008 5:48 AM
in response to: fukuta
I've examined OPEN.GUI.CONTROL-VALIDATION source code, and then I made my own solution.
This procedure produces validate events at given control to update its validation result and background.

{define-proc {update-validation-background target:ActiveTraversor}:void
    set target.validation-result = null
    {target.message-display.clear}
    def v = {Validate
                {ValidationController {DialogValidator}},
                partial? = true, current? = true
            }
    {target.handle-event v}
    def vc = {ValidationComplete.from-Validate v, true}
    {target.handle-event vc}
}


I call this procedure after setting new value to a control.

set text-field.value = "invalid value"
{update-validation-background text-field} || becomes invalid entry background


Though it seems to work well for now, I'm not sure this is a sensible way. Does somone see any problems? or please tell me if there are better solutions.
Thanks.
Click to view mgordon's profile Curl 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}}
}
Click to view fukuta's profile BlackBelt fukuta 105 posts since
Oct 17, 2007
5. Re: How to update background color of controls with validation? Aug 21, 2008 3:33 AM
in response to: mgordon
Thank you for your reply!
I prefer a latter solution as I want to use with dialog-on-finished? = true.
May I ask a question for my understanding? Is there any difference in using the same ValidationController or not? Could I see something unfavorable if I make new ValidationController everytime?
Click to view mgordon's profile Curl mgordon 47 posts since
Oct 17, 2007
6. Re: How to update background color of controls with validation? Aug 21, 2008 6:20 AM
in response to: fukuta
I thought it would be best to use the same controller because when it is handling the validation events, the validation code uses attributes of the controller, such as required?. But if you use DialogValidator it doesn't matter, because it doesn't do anything on the Validate event and the events are also handled by the existing StringValidator.

The other subtle difference with my version is that I used false for the second parameter to the from-Validate constructor. That tells the MessageDialog not to show the dialog, although it does do the colorization. That way you don't have to clear the messages or the validation result.
Click to view fukuta's profile BlackBelt fukuta 105 posts since
Oct 17, 2007
7. Re: How to update background color of controls with validation? Aug 24, 2008 8:35 AM
in response to: mgordon
OK. Thanks a lot!