4 Replies Latest reply on Apr 15, 2010 11:19 AM by neptas

    Creating and managing rule expressions

    neptas Level 1

      Hello,

       

      Most of my forms have simple expressions that should be validated depending, for instance, of the value of some fields. According to those values, some fields must be, for instance, disabled or hide.

       

      Example:

       

      fieldA must be disabled if value FieldB > 50 and value fieldC < 40

       

      What I would like to create would be a rule expression that would be validated on the initialize of the form where the fields belong, as well as everytime the value of fieldB or FieldC changes. and depending from the evaluation of the expression, fieldA would be enabled or disabled. For the evaluation of the expression I can use something like an eval function. At the same time I would need to register or hook a change value event for fields B and C, so that the rule expression would be automatically evaluated. What I dont know is what is the best way to achieve this using flex...

       

      Nny help or direction would be great

       

      thank you

        • 1. Re: Creating and managing rule expressions
          msakrejda Level 4

          ActionScript3 has no real eval. Your options are pretty much either write an expression parser, lexer, and evaluator yourself or use ExternalInterface to "borrow" JavaScript's eval. I've heard of people having some success with the second approach, but be aware that ExternalInterface is not exactly cheap., performance-wise.

          • 2. Re: Creating and managing rule expressions
            Sebastien V. Level 3

            Why do you need to use such a rule instead of using MXML inline code such as enabled="{fieldB>50}" ?

             

            For instance:

             

            <mx:Whatever
                 id="fieldA"
                 enabled="{fieldB.field > 50 &amp;&amp; fieldC.field}"
                 />
            

             

            Here field depends on what component fieldB and fieldC are, and the "&amp;" is used to represent &, that can not be used inside inline MXML code.

            • 3. Re: Creating and managing rule expressions
              msakrejda Level 4

              Ah, I may have misread--if you're doing this with static form fields, Sebastien's answer should suffice.


              • 4. Re: Creating and managing rule expressions
                neptas Level 1

                Hello,

                 

                Yes, I will be using static fileds/forms. My goal is to keep these rules separated from the layout, so that they could be easily changed by any user. Keeping these rules as you said will create an application difficult to maintain. This would be even worst in a flex application because I would need to recompile the swf file.

                In what concerns to the eval function I read somewhere that there is an alternative for it. So my problem right know is to find some mechanism to control the events of the fields, when the form is loaded and when the value of the fileds change. I think i can figure out an alternative, but I would like to know if this was already thought by adobe when flex was created... I dont want to reinvent the wheel

                 

                For instance, one thing I would like to know is if there is someway to listen for all events of all fields in a form?

                 

                thank you