HTML_QuickForm::addRule()の引数で一部不明なのがありましたので、ちょっとハックしてみました。
形式 :
void addRule( string $element, string $message, string $type, [string $format = null], [string $validation = 'server'], [boolean $reset = false], [boolean $force = false])
中身:
string | $element | Form element name |
string | $message | Message to display for invalid data |
string | $type | Rule type, use getRegisteredRules() to get types |
string | $format | (optional)Required for extra rule data |
string | $validation | (optional)Where to perform validation: "server", "client" |
boolean | $reset | Client-side validation: reset the form element to its original value if there is an error? |
boolean | $force | Force the rule to be applied, even if the target form element does not exist |
$element, $message, $type, $validation, $reset, $forceまでは、まあ、英語の解説でわかるんですよ。
問題は $format 引数。
HTML_QuickForm::addRule()をハックしてみると、
$this->_rules[$element][] = array( 'type' => $type, 'format' => $format, 'message' => $message, 'validation' => $validation, 'reset' => $reset, 'dependent' => $dependent );
みたくなってて、内部の$_rules[]に連想配列でセットされてます。んで、これが使われているところはメインはHTML_QuickForm::validate()なのでちょっと調べてみます。
function validate() { ... $registry =& HTML_QuickForm_RuleRegistry::singleton(); ... $result = $registry->validate( $rule['type'], $submitValue, $rule['format'], ...);
のように使われてました(*1)。
結構後の方まで引きずってます。んで、HTML_QuickForm_RuleRegistryというのは、結局Ruleの統合インターフェイスですな。シングルトンですから、ぶっちゃけファクトリーみたいなもんです。中身は。
んで、それのvalidateを調べてみます。
function validate($ruleName, $values, $options = null, $multiple = false) { ... $rule =& $this->getRule($ruleName); ... return $rule->validate($values, $options); }
HTML_QuickForm::validate() | HTML_QuickForm_RuleRegistry::validate() |
---|---|
$rule['type'] | $ruleName |
$submitValue | $values |
$rule['format'] | $options |
注(*2) | $multiple |
えーっとですね・・・ちょっと疲れてきましたが。
getRuleっつーのはあれですな、Validation系クラスのFactroyデザインパターンのインターフェイスです。ですから結局のところ、Rule/ディレクトリ以下の各クラスのvalidate()メソッドが呼び出されるわけです。最終的に・・・
HTML_QuickForm | HTML_QuickForm_RuleRegistry | 各Ruleクラス |
---|---|---|
$submitValue | $values(*3) | $value |
$format | $options | $option |
みたく受け渡されていきます。 ・・・$formatという引数名、おかしくない?・・・
素直に$optionsとか、$rule_paramsとかつけてくれてればもうちょっとわかりやすいのに。
ちなみに、HTML_QuickFormのパッケージではRule/以下は次のファイルしかありません。
このうち、少なくともCallback.phpとRegex.phpで$optionが、ひいては$format引数が使用されています。たとえばCallback.phpでしたら、コールバック関数(call_user_func)に唯一PHPスクリプトからプログラマーが渡せる引数として利用されていますし、Regex.phpでは同様で、内部で分岐こそしていますが、基本的にはpreg_matchに渡す正規表現として使用されています。