11<?php
22namespace Ajax \semantic \components \validation ;
3+ use Ajax \JsUtils ;
34use Ajax \service \JArray ;
45
56/**
@@ -16,6 +17,12 @@ class FieldValidation implements \JsonSerializable{
1617 * @var array array of Rules
1718 */
1819 protected $ rules ;
20+ /**
21+ * @var array array of custom rules
22+ */
23+ protected $ customRules ;
24+
25+ protected $ hasCustomRules =false ;
1926
2027 /**
2128 * @var string
@@ -43,21 +50,26 @@ public function getRules() {
4350 }
4451
4552 /**
46- * @param string $type |Rule|array
53+ * @param string|Rule|array $type
4754 * @param string $prompt
4855 * @param string $value
4956 * @return Rule
5057 */
5158 public function addRule ($ type ,$ prompt =NULL ,$ value =NULL ){
52- if ($ type instanceof Rule)
53- $ rule =$ type ;
54- else if (\is_array ($ type )){
59+ if ($ type instanceof Rule) {
60+ $ rule = $ type ;
61+ if ($ type instanceof CustomRule){
62+ $ this ->customRules []=$ type ;
63+ $ this ->hasCustomRules =true ;
64+ }
65+ }elseif (\is_array ($ type )){
5566 $ value =JArray::getValue ($ type , "value " , 2 );
5667 $ prompt =JArray::getValue ($ type , "prompt " , 1 );
5768 $ type =JArray::getValue ($ type , "type " , 0 );
5869 $ rule =new Rule ($ type ,$ prompt ,$ value );
59- }else
60- $ rule =new Rule ($ type ,$ prompt ,$ value );
70+ }else {
71+ $ rule = new Rule ($ type , $ prompt , $ value );
72+ }
6173 $ this ->rules []=$ rule ;
6274 return $ rule ;
6375 }
@@ -83,4 +95,12 @@ public function setOptional($optional) {
8395 return $ this ;
8496 }
8597
98+ public function compile (JsUtils $ js ){
99+ if ($ this ->hasCustomRules ) {
100+ foreach ($ this ->customRules as $ rule ) {
101+ $ rule ->compile ($ js );
102+ }
103+ }
104+ }
105+
86106}
0 commit comments