Skip to content

Commit 7089ac8

Browse files
committed
validation rules
1 parent 9281efb commit 7089ac8

File tree

9 files changed

+159
-37
lines changed

9 files changed

+159
-37
lines changed

Ajax/semantic/components/Form.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<?php
22
namespace Ajax\semantic\components;
33

4-
use Ajax\common\components\SimpleExtComponent;
54
use Ajax\JsUtils;
65
use Ajax\semantic\components\validation\FieldValidation;
76
use Ajax\semantic\components\validation\Rule;
@@ -10,7 +9,7 @@
109
* @version 1.001
1110
* Generates a JSON form validation string
1211
*/
13-
class Form extends SimpleExtComponent {
12+
class Form extends SimpleSemExtComponent {
1413

1514
/**
1615
* @var array
@@ -66,4 +65,8 @@ public function getScript() {
6665
$this->compileEvents();
6766
return $this->compileJQueryCode();
6867
}
68+
69+
public function onValid($jsCode){
70+
$this->addComponentEvent("onValid", $jsCode);
71+
}
6972
}

Ajax/semantic/components/SimpleSemExtComponent.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,9 @@ public function setParamParts($paramParts) {
3737
return $this;
3838
}
3939

40+
public function addComponentEvent($event,$jsCode){
41+
$jsCode=str_ireplace("\"","%quote%", $jsCode);
42+
return $this->setParam($event, "%function(){".$jsCode."}%");
43+
}
44+
4045
}

Ajax/semantic/components/validation/FieldValidation.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22
namespace Ajax\semantic\components\validation;
3+
use Ajax\service\JArray;
4+
35
/**
46
* @author jc
57
* @version 1.001
@@ -43,7 +45,12 @@ public function getRules() {
4345
public function addRule($type,$prompt=NULL,$value=NULL){
4446
if($type instanceof Rule)
4547
$this->rules[]=$type;
46-
else
48+
else if(\is_array($type)){
49+
$value=JArray::getValue($type, "value", 2);
50+
$prompt=JArray::getValue($type, "prompt", 1);
51+
$type=JArray::getValue($type, "type", 0);
52+
$this->rules[]=new Rule($type,$prompt,$value);
53+
}else
4754
$this->rules[]=new Rule($type,$prompt,$value);
4855
}
4956

Ajax/semantic/components/validation/Rule.php

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,37 @@ public function jsonSerialize() {
6262
return $result;
6363
}
6464

65+
/**
66+
* A field should match the value of another validation field, for example to confirm passwords
67+
* @param string $name
68+
* @param string $prompt
69+
* @return \Ajax\semantic\components\validation\Rule
70+
*/
6571
public static function match($name,$prompt=NULL){
6672
return new Rule("match[".$name."]",$prompt);
6773
}
6874

69-
public static function integer($min=0,$max=100,$prompt=NULL){
70-
return new Rule("integer[{$min}..{$max}]",$prompt);
75+
/**
76+
* A field should be different than another specified field
77+
* @param string $name
78+
* @param string $prompt
79+
* @return \Ajax\semantic\components\validation\Rule
80+
*/
81+
public static function different($name,$prompt=NULL){
82+
return new Rule("different[".$name."]",$prompt);
83+
}
84+
85+
/**
86+
* A field is an integer value, or matches an integer range
87+
* @param int|NULL $min
88+
* @param int|NULL $max
89+
* @param string $prompt
90+
* @return \Ajax\semantic\components\validation\Rule
91+
*/
92+
public static function integer($min=NULL,$max=NULL,$prompt=NULL){
93+
if(\is_int($min) && \is_int($max))
94+
return new Rule("integer[{$min}..{$max}]",$prompt);
95+
return new Rule("integer",$prompt);
7196
}
7297

7398
public static function decimal($prompt=NULL){
@@ -126,4 +151,16 @@ public static function email($prompt=NULL){
126151
return new Rule("email",$prompt);
127152
}
128153

154+
public static function url($prompt=NULL){
155+
return new Rule("url",$prompt);
156+
}
157+
158+
public static function regExp($value,$prompt=NULL){
159+
return new Rule("regExp",$prompt,$value);
160+
}
161+
162+
public static function custom($name,$jsFunction){
163+
return "$.fn.form.settings.rules.".$name." =".$jsFunction ;
164+
}
165+
129166
}
Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
11
$.fn.form.settings.prompt= {
22
empty : '{name} doit avoir une valeur',
33
checked : '{name} doit être coché',
4-
email : '{name} must be a valid e-mail',
5-
url : '{name} must be a valid url',
6-
regExp : '{name} is not formatted correctly',
7-
integer : '{name} must be an integer',
8-
decimal : '{name} must be a decimal number',
9-
number : '{name} must be set to a number',
10-
is : '{name} must be \'{ruleValue}\'',
11-
isExactly : '{name} must be exactly \'{ruleValue}\'',
12-
not : '{name} cannot be set to \'{ruleValue}\'',
13-
notExactly : '{name} cannot be set to exactly \'{ruleValue}\'',
14-
contain : '{name} cannot contain \'{ruleValue}\'',
15-
containExactly : '{name} cannot contain exactly \'{ruleValue}\'',
16-
doesntContain : '{name} must contain \'{ruleValue}\'',
17-
doesntContainExactly : '{name} must contain exactly \'{ruleValue}\'',
18-
minLength : '{name} must be at least {ruleValue} characters',
19-
length : '{name} must be at least {ruleValue} characters',
20-
exactLength : '{name} must be exactly {ruleValue} characters',
21-
maxLength : '{name} cannot be longer than {ruleValue} characters',
22-
match : '{name} must match {ruleValue} field',
23-
different : '{name} must have a different value than {ruleValue} field',
24-
creditCard : '{name} must be a valid credit card number',
25-
minCount : '{name} must have at least {ruleValue} choices',
26-
exactCount : '{name} must have exactly {ruleValue} choices',
27-
maxCount : '{name} must have {ruleValue} or less choices'
4+
email : '{name} doit être une adresse e-mail valide',
5+
url : '{name} doit être une url valide',
6+
regExp : '{name} n\'est pas correctement formaté',
7+
integer : '{name} doit être un entier',
8+
decimal : '{name} doit être un nombre décimal',
9+
number : '{name} doit être un nombre',
10+
is : '{name} doit être égal à \'{ruleValue}\'',
11+
isExactly : '{name} doit être exactement \'{ruleValue}\'',
12+
not : '{name} ne doit pas être égal à \'{ruleValue}\'',
13+
notExactly : '{name} ne doit pas être exactement égal à \'{ruleValue}\'',
14+
contain : '{name} doit contenir \'{ruleValue}\'',
15+
containExactly : '{name} doit contenir exactement \'{ruleValue}\'',
16+
doesntContain : '{name} doit contenir \'{ruleValue}\'',
17+
doesntContainExactly : '{name} ne doit pas contenir exactement \'{ruleValue}\'',
18+
minLength : '{name} doit avoir au moins {ruleValue} caractères',
19+
length : '{name} doit contenir {ruleValue} caractères',
20+
exactLength : '{name} doit avoir une taille de {ruleValue} caractères',
21+
maxLength : '{name} ne doit pas avoir une longueur supérieure à {ruleValue} caractères',
22+
match : '{name} doit être égal au champ {ruleValue}',
23+
different : '{name} doit avoir une valeur différente du champ {ruleValue}',
24+
creditCard : '{name} doit être un numérode carte de crédit valide',
25+
minCount : '{name} doit comporter au moins {ruleValue} choix',
26+
exactCount : '{name} doit comporter exactement {ruleValue} choix',
27+
maxCount : '{name} doit comporter {ruleValue} choix au maximum'
2828
};

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

Lines changed: 65 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Ajax\semantic\html\collections\form\traits\FieldsTrait;
1010
use Ajax\semantic\html\elements\HtmlDivider;
1111
use Ajax\JsUtils;
12+
use Ajax\service\AjaxCall;
1213

1314
/**
1415
* Semantic Form component
@@ -125,6 +126,14 @@ public function addFieldRule($index,$type,$prompt=NULL,$value=NULL){
125126
return $this;
126127
}
127128

129+
public function addFieldRules($index,$rules){
130+
$field=$this->getItem($index);
131+
if(isset($field)){
132+
$field->addRules($rules);
133+
}
134+
return $this;
135+
}
136+
128137
/**
129138
*
130139
* @param string $identifier
@@ -145,21 +154,42 @@ public function addMessage($identifier, $content, $header=NULL, $icon=NULL, $typ
145154
return $this->addItem($message);
146155
}
147156

157+
private function addCompoValidation($js,$compo,$field){
158+
$validation=$field->getValidation();
159+
if(isset($validation)){
160+
if(isset($compo)===false){
161+
$compo=$js->semantic()->form("#".$this->identifier);
162+
}
163+
$validation->setIdentifier($field->getField()->getIdentifier());
164+
$compo->addFieldValidation($validation);
165+
}
166+
return $compo;
167+
}
168+
148169
public function run(JsUtils $js) {
149170
$compo=NULL;
150171
foreach ($this->_fields as $field){
151-
$validation=$field->getValidation();
152-
if(isset($validation)){
153-
if(isset($compo)===false){
154-
$compo=$js->semantic()->form("#".$this->identifier);
172+
$compo=$this->addCompoValidation($js, $compo, $field);
173+
}
174+
foreach ($this->content as $field){
175+
if($field instanceof HtmlFormFields){
176+
$items=$field->getItems();
177+
foreach ($items as $_field){
178+
$compo=$this->addCompoValidation($js, $compo, $_field);
155179
}
156-
$validation->setIdentifier($field->getField()->getIdentifier());
157-
$compo->addFieldValidation($validation);
158180
}
159181
}
160182
if(isset($compo)===false){
161183
return parent::run($js);
162184
}
185+
if(isset($this->_validationParams["_ajaxSubmit"])){
186+
if($this->_validationParams["_ajaxSubmit"] instanceof AjaxCall){
187+
$compilation=$this->_validationParams["_ajaxSubmit"]->compile($js);
188+
$compilation=str_ireplace("\"","%quote%", $compilation);
189+
$this->onSuccess($compilation);
190+
unset($this->_validationParams["_ajaxSubmit"]);
191+
}
192+
}
163193
$compo->addParams($this->_validationParams);
164194
$this->_bsComponent=$compo;
165195
$this->addEventsOnRun($js);
@@ -183,4 +213,33 @@ public function setValidationParams(array $_validationParams) {
183213
return $this;
184214
}
185215

216+
public function submitOn($event,$identifier,$url,$responseElement){
217+
$elem=$this->getElementById($identifier, $this->content);
218+
if(isset($elem)){
219+
$elem->addEvent($event, "$('#".$this->identifier."').form('validate form');");
220+
$this->_validationParams["_ajaxSubmit"]=new AjaxCall("postForm", ["form"=>$this->identifier,"responseElement"=>$responseElement,"url"=>$url]);
221+
}
222+
return $this;
223+
}
224+
225+
/**
226+
* Callback on each valid field
227+
* @param string $jsCode
228+
* @return \Ajax\semantic\html\collections\form\HtmlForm
229+
*/
230+
public function onValid($jsCode){
231+
$this->_validationParams["onValid"]="%function(){".$jsCode."}%";
232+
return $this;
233+
}
234+
235+
/**
236+
* Callback if a form is all valid
237+
* @param string $jsCode can use event and fields parameters
238+
* @return HtmlForm
239+
*/
240+
public function onSuccess($jsCode){
241+
$this->_validationParams["onSuccess"]="%function(evt,fields){".$jsCode."}%";
242+
return $this;
243+
}
244+
186245
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,13 @@ public function addRule($type,$prompt=NULL,$value=NULL){
121121
return $this;
122122
}
123123

124+
public function addRules(array $rules){
125+
foreach ($rules as $rule){
126+
$this->addRule($rule);
127+
}
128+
return $this;
129+
}
130+
124131
public function getValidation() {
125132
return $this->_validation;
126133
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
use Ajax\semantic\html\collections\form\traits\FieldsTrait;
1111

1212
class HtmlFormFields extends HtmlSemCollection {
13-
13+
1414
use FieldsTrait;
1515
protected $_equalWidth;
1616
protected $_name;
@@ -22,7 +22,7 @@ public function __construct($identifier, $fields=array(), $equalWidth=true) {
2222
}
2323

2424
public function addFields($fields=NULL, $label=NULL) {
25-
if (!$fieldsinstanceofHtmlFormFields) {
25+
if (!$fields instanceof HtmlFormFields) {
2626
if (\is_array($fields)===false) {
2727
$fields=\func_get_args();
2828
$end=\end($fields);

Ajax/semantic/html/collections/form/traits/CheckboxTrait.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,8 @@ public function attachEvent($selector, $action=NULL) {
3030
public function attachEvents($events=array()) {
3131
return $this->getField()->attachEvents($events);
3232
}
33+
34+
public function getField(){
35+
return $this->content["field"]->getField();
36+
}
3337
}

0 commit comments

Comments
 (0)