Search
Feeds
Aug 27 2008

Flex Component: ValidatingTextInput

Posted by TJ Downes at 7:52 PM
11 comments
- Categories: Technical Articles | Adobe AIR | Adobe Flex

Anyone who has built forms in Flex knows the frustration of trying to build a good experience with validation using the default Flex components. Validation is not tied to the field, validation messages only show up when you mouseover the invalid fields and when you want to process the form you essentially have to revalidate all of the data, unless you've written custom methods to handle all of your validation. I've spent a couple of days working through this and I was frustrated, to say the least. I sat down with the rest of the team and said "Hey, how can we make this easier?"

What we came up with was the ValidatingTextInput. True, the first draft we are offering here is a bit rough around the edges, but it works! I am planning to add data binding and other critical features soon, so this is an "alpha" version. Let me show you how simple this is:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" xmlns:ui="net.sanative.ui.*"
    layout="vertical"
    width="100%" height="100%"
    paddingBottom="9" paddingLeft="9" paddingTop="9" paddingRight="9"
    creationComplete="init()">
   
    <mx:Style source="/assets/styles.css"/>
   
    <mx:Script>
    <![CDATA[
   
        import mx.validators.EmailValidator;
        import mx.validators.StringValidator;


        [Bindable]
        private var emailValidator:EmailValidator = new EmailValidator();

        [Bindable]
        private var noBlanksValidator:StringValidator = new StringValidator();
      
        private function init():void
        {
            noBlanksValidator.required = true;
        }
    ]]>
   
    </mx:Script>

    <mx:Form>
        <ui:ValidatingTextInput id="ti_FirstName"
            label="First Name"
            validator="{noBlanksValidator}"
            toolTipStyleName="errorTip"
            plainTextError="is a required field."/>

        <ui:ValidatingTextInput id="ti_Email"
            label="Email"
            validator="{emailValidator}"
            toolTipStyleName="errorTip"
            plainTextError="must be a valid email address."/>

    </mx:Form>

</mx:Application>

You will notice the ValidatingTextField accepts a few parameters: label, validator, tooltipStyleName and plainTextError. I will describe each of these briefly:

label: This is the text to display the name of the field to the user

validator: this attribute accepts any validator and will check the data in the field against the validator you pass it. Just configure your validator and pass it to the component, it's that simple.

toolTipStyleName: The name of a CSS class to pass to the component for styling the look of the tooltips

plainTextError: This attribute is combined with the label attribute to make up the full text of the tooltip.

We display the tooltip on focusOut so that the user does not have to mouse over the field to determine what the problem is. Additionally, the component has a property named isValid available to you. This property allows you to simply check the value to determine if the field is valid or not, making it much easier to determine if your entire form is ready for processing. Instead of rerunning all your validation again, simply check to ensure all the fields you want to validate have a isValid value of true!

Here's the demo:

You need the latest Flash Player and JavaScript enabled to view this content.

 

There are a ton of possibilities for this component and we also plan on expanding it to other components such as the ComboBox, List, etc. If you have any suggestions, let us know!

Many of the ideas we implemented came from Aral Balkan and his readers, so thanks guys! If you would like the source, right click the example and "View Source".

Comments

Ryan

Ryan wrote on 12/12/08 1:49 PM

TJ, this is fantastic! I have searched all over and this is definitely the cleanest solution that I have seen yet! Thanks for sharing it!
TJ Downes

TJ Downes wrote on 12/12/08 1:52 PM

You're welcome. I have an even more refined version if you are interested. The one in this article has some issues, which I am sure you will find. The newer version has fixed some of that, and although its not yet perfect I've found its a significant help over the default stuff
Ryan

Ryan wrote on 12/12/08 2:53 PM

That would be really great! Do you mind if I use this in commercial projects?
TJ Downes

TJ Downes wrote on 12/12/08 2:54 PM

not at all! If you make improvements Id love to be able to use them myself :D sending via email
Ryan

Ryan wrote on 12/12/08 3:03 PM

Sounds great, will do!
vlad

vlad wrote on 12/13/08 2:20 PM

Very usefull. I spend this day, researching source code of flex. I found role of tooltip manager, but finally i found on google this solution, and work very well.
Thanks!
Neil

Neil wrote on 12/21/08 10:47 AM

Hi

I've been searching high and low for a way of making the tooltip re-position itself if the form is resized or scrolled.

I was wondering if you had a solution for that issue?

Neil
Ryan

Ryan wrote on 01/02/09 10:23 AM

Neil, I have also been searching for a solution to this without much luck. The issue is with the way that ToolTips are created in the first place. According to the LiveDocs, "ToolTips appear in their own layer, on top of everything except cursors."

So short of writing a new implementation of tooltips (which I haven't done), I have not found a solution. Please post if find something!

-Ryan
Neil

Neil wrote on 01/03/09 4:57 PM

Hi Ryan

Thanks for your feedback.

I worked out a clumsy way to do it using a timer.

My next problem is the tooltip remains if I change view or select a different tab in my tab navigator. Of course, that's due to the layer thing you mentioned.

Sometimes the solution to these things gets so cumbersome that you need to abandon it and just do it some other way - in this case without tooltips.

Cheers,

Neil
Ryan

Ryan wrote on 01/23/09 8:37 AM

Hey Neil,

It's been a while since I checked this post... I found the solution to the tooltip remaining, but I can't remember the exact changes I made to do this. I'll send you my current version.

Essentially, you need to get rid of the tooltip with the ValidatingTextInput is removed. So add a destroyToolTip function that is bound to removedFromStage fo the parent TextInput. Then call ToolTipManager.destroyToolTip(this.errorTip) in a try/catch, and you chould be good to go.

I'm not sure how this will work with a tab navigator, but this seemed to fix a similar problem that I was seeing.

-Ryan
TJ Downes

TJ Downes wrote on 01/23/09 8:47 AM

In my projects Ive been using the removedFromStage attribute of the component

removedFromStage="hideToolTip(event)"

private function hideToolTip(evt:*):void
{
if(this.errorTip != null)
this.errorTip.visible = false;
}

Frankly, I do not recall at the moment if it has ben the solution to all the issues Ive had

Write your comment



(it will not be displayed)