This Question is Answered

1 "correct" answer available (5 pts) 15 "helpful" answers available (3 pts)
4 Replies Last post: May 15, 2008 1:29 AM by fukuta

Is it possible to change validation rules dynamically?

May 13, 2008 6:14 AM

Click to view fukuta's profile BlackBelt fukuta 118 posts since
Oct 17, 2007
Hello,
I'd like to validate input values in a different way depending on the context.
For example, a check button is checked, then a text field's value comes to be required.
Meanwhile, it isn't required when the check button is unchecked.

To realize this type of validation, I have made custom validators or validate event handlers so far.
But if it is possible to change validation rule in a similar way to change properties, it is very easy and intuitive for me.
I wonder if I can take such a way or there are any other reasonable way.
Click to view mgordon's profile Curl mgordon 48 posts since
Oct 17, 2007
1. Re: Is it possible to change validation rules dynamically? May 13, 2008 2:30 PM
Using custom validators is the first thing I thought of, but here's another way to do it.

Instead of using validate-with, you can create a ValidationController directly. That allows you to modify it's properties later, as shown in this example. In order to force a new validation cycle when the CheckButton is changed, I put an EnablingValidator on it. We should probably have a NullValidator or something like it to serve this purpose. Toggling the required? property is no problem but when you change the requirements in a way that might make an existing entry invalid is not so nice. I'm using a MessageDisplay here to avoid a dialog coming up in that case. Another option might be to clear the fields, or at least the invalid fields. But users might not like that either. You need to be careful that you don't make it too complicated.

{let num-validator:Validator = 
    {RegExpValidator ValidationPattern.us-phone-number.value}}
{let str-validator:Validator = {StringValidator min-chars = 2}}

{let vc-a:ValidationController = 
    {ValidationController
        str-validator,
        required? = true
    }
}

{let vc-b:ValidationController = 
    {ValidationController
        str-validator,
        required? = true
    }
}

{let md-a:MessageDisplay = {MessageDisplay}}
{let a-descr:Frame = {Frame}}

{Dialog
    margin = 9pt,
    {validate-with {DialogValidator}},
    {VBox 
        spacing = 3pt, 
        {text You must provide your name and address, or your phone number
        },
        {CheckButton
            label = "I'll give you my phone number.",
            {on ValueChanged at cb:CheckButton do
                {if cb.value asa bool then
                    set vc-a.required? = true
                    set vc-b.required? = false
                    set vc-a.validator = num-validator
                    set vc-b.validator = str-validator
                 else
                    set vc-a.required? = true
                    set vc-b.required? = true
                    set vc-a.validator = str-validator
                    set vc-b.validator = str-validator
                }
            },
            {on ValidationComplete at cb:CheckButton do
                {if cb.value asa bool then
                    {a-descr.add replace? = true, "Phone (555-111-1111)"}
                 else
                    {a-descr.add replace? = true, "Name"}
                }
            },
            {validate-with {EnablingValidator}}
        },
        {Table columns = 3,
            "A", 
            {TextField
                width = 2in,
                message-display = md-a,
                vc-a.handler
            },
            a-descr,

            "B", 
            {TextField
                width = 2in,
                message-display = md-a,
                vc-b.handler
            },
            "Address",

            {row {cell colspan = 2, {value md-a}}}
        }
    }
}
Click to view fukuta's profile BlackBelt fukuta 118 posts since
Oct 17, 2007
2. Re: Is it possible to change validation rules dynamically? May 13, 2008 7:38 PM
in response to: mgordon
That is exactly what I want. Thanks.

Let me ask you about your advice below
Toggling the required? property is no problem but when you change the requirements in a way that might make an existing entry invalid is not so nice. I'm using a MessageDisplay here to avoid a dialog coming up in that case.
What undesirable thing happen if I change the requirements that make an existing entry invalid?
I changed your example to use a MessageDialog instead of a MessageDisplay, it seemed to work with no problem.
It is appreciated if I can get the point I should take care.
Click to view mgordon's profile Curl mgordon 48 posts since
Oct 17, 2007
3. Re: Is it possible to change validation rules dynamically? May 14, 2008 6:20 AM
in response to: fukuta
I just tried it and in fact there is no problem. I was afraid that if you started by typing your name and address and then changed your mind and checked the box to enter your phone number, an error dialog would come up. But the way it works is that the first field turns pink but no dialog is shown, because you didn't actually change that field. So it does work OK with MessageDialog.
Click to view fukuta's profile BlackBelt fukuta 118 posts since
Oct 17, 2007
4. Re: Is it possible to change validation rules dynamically? May 15, 2008 1:29 AM
in response to: mgordon

Great. Thanks.:)