Flex: TextArea line break problem

By , last updated August 13, 2019

Recently I needed to fix a bug where a mx:TextArea was always dirty. The reason was a text with line breaks like:

“This is a text.

This is another text.”

In database this text was stored as char(255) like “This is a text.rnThis is another text.”

Here is the original code:

<mx:TextArea id="commentInput" height="50" maxChars="255"
     text="{personModel.person.comment}" width="100%" tabIndex="20"/>

After some time I figured out that the reason the form was dirty was HTML text. So to fix it I changed text with htmlText:

<mx:TextArea id="commentInput" height="50" maxChars="255"
     htmlText="{personModel.person.comment}" width="100%" tabIndex="20"/>

But, this solution gave an unexpected side effect. This was possible to render only one person, if I tried to render the other one in another tab, same data was visible for both persons.

That’s why I needed to solve the problem with replacing:

<mx:Script>
        <![CDATA[
        public function get comment():String {
            var crlf:String = String.fromCharCode(13, 10);
            var regEx:RegExp = new RegExp(crlf, "g");
            return _kommentar != null ? _kommentar.replace(regEx, "rr") : '';
        }
        ]]>
</mx:Script>
...
<mx:TextArea id="commentInput" height="50" maxChars="255"
     text="{personModel.person.comment}" width="100%" tabIndex="20"/>