エラーを検出したときのフォーマッタのプロトコルでは、空のストリングが返され、エラー状況を記述するストリングがフォーマッタの error プロパティに書き込まれます。format() メソッドの戻り値が空のストリングかどうかをチェックできます。空のストリングが見つかった場合は、error プロパティにアクセスしてエラーの原因を確認します。
代わりに、フォーマットのエラーでエラーメッセージを返すエラーハンドラ関数を記述できます。次の例に、単純なエラーハンドラ関数を示します。
<?xml version="1.0"?>
<!-- formatters\FormatterSimpleErrorForDevApps.mxml -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Script>
<![CDATA[
private function formatWithError(value:Object):String {
var formatted:String = myFormatter.format(value);
if (formatted == "") {
if (myFormatter.error != null ) {
if (myFormatter.error == "Invalid value") {
formatted = ": The value is not valid.";
}
else {
formatted = ": The formatString is not valid.";
}
}
}
return formatted;
}
]]>
</mx:Script>
<!-- Declare a formatter and specify formatting properties.-->
<mx:DateFormatter id="myFormatter"
formatString="MXXXMXMXMXMXM"/>
<!-- Trigger the formatter while populating a string with data.-->
<mx:TextInput id="myTI"
width="75%"
text="Your order shipped on {formatWithError('May 23, 2005')}"/>
</mx:Application>
前の例で実行する SWF ファイルは以下のとおりです。
この例では、不正なフォーマットストリングで DateFormatter を定義します。次に、Date フォーマッタの format() メソッドを直接呼び出す代わりに、formatWithError() メソッドを使用して TextInput コントロールでフォーマッタを呼び出します。この例では、入力ストリングまたはフォーマットストリングのいずれかが不正である場合、formatWithError() メソッドは、フォーマットされたストリング値ではなくエラーメッセージを返します。
このページに新しいコメントが追加された場合に、電子メールでの通知を希望する。 | コメントレポート