Skip to content

Commit 47de240

Browse files
committed
Add CustomRule and AjaxRule
1 parent 5c767ad commit 47de240

File tree

4 files changed

+99
-6
lines changed

4 files changed

+99
-6
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
namespace Ajax\semantic\components\validation;
3+
4+
use Ajax\JsUtils;
5+
use Ajax\service\AjaxCall;
6+
7+
/**
8+
* Ajax\semantic\components\validation$AjaxRule
9+
* This class is part of phpmv-ui
10+
*
11+
* @author jc
12+
* @version 1.0.0
13+
*
14+
*/
15+
class AjaxRule extends CustomRule {
16+
17+
private $ajaxCall;
18+
19+
public function __construct($type, $url, $params, $jsCallback = null, $method = 'post', $parameters = [], $prompt = NULL, $value = NULL) {
20+
parent::__construct($type, $prompt, $value);
21+
$parameters = \array_merge([
22+
'async' => false,
23+
'url' => $url,
24+
'params' => $params,
25+
'hasLoader' => false,
26+
'jsCallback' => $jsCallback,
27+
'dataType' => 'json',
28+
'stopPropagation' => false,
29+
'preventDefault' => false,
30+
'responseElement' => null
31+
], $parameters);
32+
$this->ajaxCall = new AjaxCall($method, $parameters);
33+
}
34+
35+
public function compile(JsUtils $js) {
36+
$js->exec(Rule::custom($this->getType(), "function(value,ruleValue){var result=true;" . $this->ajaxCall->compile($js) . "return result;}"), true);
37+
}
38+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
namespace Ajax\semantic\components\validation;
3+
4+
use Ajax\JsUtils;
5+
6+
/**
7+
* Ajax\semantic\components\validation$CustomRule
8+
* This class is part of phpmv-ui
9+
*
10+
* @author jc
11+
* @version 1.0.0
12+
*
13+
*/
14+
class CustomRule extends Rule {
15+
16+
protected $jsFunction;
17+
18+
public function __construct($type, $jsFunction, $prompt = NULL, $value = NULL) {
19+
parent::__construct($type, $prompt, $value);
20+
$this->jsFunction = $jsFunction;
21+
}
22+
23+
public function compile(JsUtils $js) {
24+
$js->exec(Rule::custom($this->getType(), $this->jsFunction), true);
25+
}
26+
}

Ajax/semantic/components/validation/FieldValidation.php

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
22
namespace Ajax\semantic\components\validation;
3+
use Ajax\JsUtils;
34
use 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
}

Ajax/semantic/html/collections/form/HtmlFormField.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Ajax\semantic\html\collections\form;
44

5+
use Ajax\JsUtils;
56
use Ajax\semantic\html\base\HtmlSemDoubleElement;
67
use Ajax\semantic\html\base\constants\Wide;
78
use Ajax\semantic\html\base\constants\State;
@@ -169,4 +170,12 @@ public function getValidation() {
169170
public function setSize($size) {
170171
return $this->getField()->addToPropertyCtrl("class", $size, Size::getConstants());
171172
}
173+
174+
public function run(JsUtils $js) {
175+
if(isset($this->_validation)){
176+
$this->_validation->compile($js);
177+
}
178+
return parent::run($js);
179+
}
180+
172181
}

0 commit comments

Comments
 (0)