phpMv  -UI toolkit 2.4.12
jQuery, jQuery UI, Twitter Bootstrap and Semantic-UI library for php & php MVC Frameworks
FieldValidation.php
Go to the documentation of this file.
1 <?php
3 
4 use Ajax\JsUtils;
6 
13 class FieldValidation implements \JsonSerializable {
14 
19  protected $identifier;
20 
25  protected $rules;
26 
31  protected $customRules;
32 
33  protected $hasCustomRules = false;
34 
39  protected $depends;
40 
41  protected $optional;
42 
43  public function __construct($identifier) {
44  $this->identifier = $identifier;
45  $this->rules = [];
46  }
47 
48  public function getIdentifier() {
49  return $this->identifier;
50  }
51 
52  public function setIdentifier($identifier) {
53  $this->identifier = $identifier;
54  return $this;
55  }
56 
57  public function getRules() {
58  return $this->rules;
59  }
60 
68  public function addRule($type, $prompt = NULL, $value = NULL) {
69  if ($type instanceof Rule) {
70  $rule = $type;
71  if ($type instanceof CustomRule) {
72  $this->customRules[] = $type;
73  $this->hasCustomRules = true;
74  }
75  } elseif (\is_array($type)) {
76  $value = JArray::getValue($type, "value", 2);
77  $prompt = JArray::getValue($type, "prompt", 1);
78  $type = JArray::getValue($type, "type", 0);
79  $rule = new Rule($type, $prompt, $value);
80  } else {
81  $rule = new Rule($type, $prompt, $value);
82  }
83  $this->rules[] = $rule;
84  return $rule;
85  }
86 
87  #[\ReturnTypeWillChange]
88  public function jsonSerialize() {
89  $result = [
90  "identifier" => $this->identifier,
91  "rules" => $this->rules
92  ];
93  if ($this->optional) {
94  $result["optional"] = true;
95  }
96  if (isset($this->depends)) {
97  $result["depends"] = $this->depends;
98  }
99  return $result;
100  }
101 
102  public function setDepends($depends) {
103  $this->depends = $depends;
104  return $this;
105  }
106 
107  public function setOptional($optional) {
108  $this->optional = $optional;
109  return $this;
110  }
111 
112  public function compile(JsUtils $js) {
113  if ($this->hasCustomRules) {
114  foreach ($this->customRules as $rule) {
115  $rule->compile($js);
116  }
117  }
118  }
119 }
static getValue($array, $key, $pos)
Definition: JArray.php:10
JQuery PHP library.
Definition: JsUtils.php:23
Ajax$CustomRule This class is part of phpmv-ui.
Definition: CustomRule.php:14