Home > Flex > Flex Tip of the Day: validateNow() on text fields

Flex Tip of the Day: validateNow() on text fields

April 27th, 2007

(One in a series of random, daily, useful Flex tips.)

If you have a Label or another component that uses a Label or derives from a Label, you may run into situations where you need to update the Label’s text property and then immediately use its textWidth property in a calculation. Normally, the textWidth property will not be updated until later when the Label’s commitProperties() runs. But if you need to use its textWidth property immediately, you can call validateNow() on the Label. That will cause the Label’s commitProperties() to run immediately, after which you can get the correct value of textWidth.

In the example below, I’m using a Label to display the title of a document. When the underlying data structure changes, I need to update the title, then truncate its width to the width of the column it’s being displayed in. Here’s the commitProperties() of the UIComponent I’m using to do the display, which has a Label as a member variable:

override protected function commitProperties():void
    {
        super.commitProperties();
        if (_documentListInfoChanged)
        {
        	_label.text = _documentListInfo.title;
        	_label.validateNow();
        	_label.width = Math.min(_label.textWidth + UITextField.TEXT_WIDTH_PADDING*2, maxTitleWidth);
        	_documentListInfoChanged = false;
         }
    }



By the way, calling validateNow() can be useful in many situations; this is just one specific and common case.

Technorati Tags: ,

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • StumbleUpon
Author: David Coletta Categories: Flex Tags:
  1. superabe
    April 27th, 2007 at 12:49 | #1

    So if commitProperties calls validateNow which in turn calls commitProperties, does that not get into a recursive loop ?

    (I havent tried it…thougjt its easier to ask)

    BTW, your blog is one of the few truly useful blogs when it comes to Flex dev topics!

  2. david
    April 27th, 2007 at 14:15 | #2

    Sorry, I had a feeling this example was going to be confusing. I’m talking about two different objects’ commitProperties() methods. The example above is the commitProperties() method of my document item component, and the validateNow() ends up triggering the commitProperties() of the Label. So no, no recursive loop.

    And thanks for the good word!

  3. superabe
    April 27th, 2007 at 17:04 | #3

    ah makes sense…on re-reading I realize you had made this clear. my bad.

  4. Bob Smith
    August 17th, 2007 at 02:21 | #4

    Thank you SO much. I’ve been making crazy use of callLater to get around issues like this, but this solution is SOOOO much better.

  5. October 29th, 2008 at 14:14 | #5

    Thanks! this was giving me a headache.

  6. January 8th, 2009 at 11:05 | #6

    thanks. this was very helpful.

  7. Tanzeem Akhtar Bhatti
    September 24th, 2009 at 04:28 | #7

    Thanks,
    It helps me in very fine way

  8. Adnan Afzal Mani
    September 24th, 2009 at 05:20 | #8

    Thank you SO much. I’ve been making crazy use of callLater to get around issues like this, but this solution is SOOOO much better.

  9. October 29th, 2009 at 15:36 | #9

    Thank you! ValidateNow is, indeed, applicable in many varying cases. Thanks for bringing this function to our attention.

  10. December 17th, 2009 at 14:28 | #10

    The following methods don’t seem to work on Flex4 SDK Beta 2

    label.validateNow();
    label.invalidateSize();

    However, I was able to get the width using TextLineMetrics:

    label.regenerateStyleCache(false);
    var textMetrics:TextLineMetrics = label.measureText( text );

  1. September 7th, 2008 at 18:28 | #1