Skip to content

Commit 97a40a4

Browse files
committed
form helper
1 parent 1801899 commit 97a40a4

File tree

11 files changed

+212
-19
lines changed

11 files changed

+212
-19
lines changed

src/PHPixie/Validate.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,15 @@ public function buildValidator($rule)
4343
return $this->builder->validator($rule);
4444
}
4545

46+
/**
47+
* @param Validate\Validator $validator
48+
* @return Validate\Form
49+
*/
50+
public function form($validator)
51+
{
52+
return $this->builder->form($validator);
53+
}
54+
4655
/**
4756
* @return mixed
4857
*/

src/PHPixie/Validate/Builder.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,15 @@ public function validator($rule)
5757
);
5858
}
5959

60+
/**
61+
* @param Validator $validator
62+
* @return Form
63+
*/
64+
public function form($validator)
65+
{
66+
return new Form($validator);
67+
}
68+
6069
/**
6170
* @param string $name
6271
* @return mixed

src/PHPixie/Validate/Errors.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@
99
class Errors
1010
{
1111
/**
12+
* @param string $message
1213
* @return Errors\Error\EmptyValue
1314
*/
14-
public function emptyValue()
15+
public function emptyValue($message = null)
1516
{
16-
return new Errors\Error\EmptyValue();
17+
return new Errors\Error\EmptyValue($message);
1718
}
1819

1920
/**

src/PHPixie/Validate/Errors/Error/EmptyValue.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@
88
*/
99
class EmptyValue extends \PHPixie\Validate\Errors\Error
1010
{
11+
protected $errorMessage;
12+
13+
public function __construct($errorMessage = null)
14+
{
15+
$this->errorMessage = $errorMessage;
16+
}
17+
1118
/**
1219
* @return string
1320
*/
@@ -21,6 +28,10 @@ public function type()
2128
*/
2229
public function asString()
2330
{
31+
if($this->errorMessage !== null) {
32+
return $this->errorMessage;
33+
}
34+
2435
return "Value is empty";
2536
}
2637
}

src/PHPixie/Validate/Form.php

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
<?php
2+
3+
namespace PHPixie\Validate;
4+
5+
use PHPixie\Validate\Results\Result\Root as RootResult;
6+
use PHPixie\Validate\Results\Result\Root;
7+
8+
class Form
9+
{
10+
/** @var Validator */
11+
protected $validator;
12+
13+
/** @var RootResult|null */
14+
protected $result = null;
15+
16+
/**
17+
* @param Validator $validator
18+
*/
19+
public function __construct($validator)
20+
{
21+
$this->validator = $validator;
22+
}
23+
24+
/**
25+
* @param array $data
26+
* @return bool
27+
*/
28+
public function submit($data)
29+
{
30+
$this->result = $this->validator->validate($data);
31+
return $this->result->isValid();
32+
}
33+
34+
/**
35+
* @return bool
36+
*/
37+
public function isSubmitted()
38+
{
39+
return $this->result !== null;
40+
}
41+
42+
/**
43+
* @return Root
44+
*/
45+
public function result()
46+
{
47+
return $this->result;
48+
}
49+
50+
/**
51+
* @return bool|null
52+
*/
53+
public function isValid()
54+
{
55+
if($this->result === null) {
56+
return null;
57+
}
58+
59+
return $this->result->isValid();
60+
}
61+
62+
/**
63+
* @return array|null|object
64+
*/
65+
public function data()
66+
{
67+
if($this->result === null) {
68+
return null;
69+
}
70+
71+
return $this->result->getValue();
72+
}
73+
74+
public function isFieldValid()
75+
{
76+
if($this->result === null) {
77+
return null;
78+
}
79+
80+
$this->result->isValid();
81+
}
82+
83+
/**
84+
* @param $field
85+
* @param null $default
86+
* @return mixed
87+
*/
88+
public function fieldValue($field, $default = null)
89+
{
90+
if($this->result === null) {
91+
return $default;
92+
}
93+
94+
return $this->result->getPathValue($field);
95+
}
96+
97+
/**
98+
* @param $field
99+
* @return null|Errors\Error
100+
*/
101+
public function fieldError($field)
102+
{
103+
if($this->result === null) {
104+
return null;
105+
}
106+
107+
return $this->result->getPathError($field);
108+
}
109+
110+
public function resultError()
111+
{
112+
if($this->result === null) {
113+
return null;
114+
}
115+
116+
return $this->result->firstError();
117+
}
118+
119+
public function __get($name)
120+
{
121+
return $this->fieldValue($name);
122+
}
123+
}

src/PHPixie/Validate/Results/Result.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,10 @@ public function isValid()
9393
/**
9494
* @return Result
9595
*/
96-
public function addEmptyValueError()
96+
public function addEmptyValueError($message = null)
9797
{
9898
return $this->addError(
99-
$this->errorBuilder->emptyValue()
99+
$this->errorBuilder->emptyValue($message)
100100
);
101101
}
102102

src/PHPixie/Validate/Results/Result/Root.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,35 @@ public function getPathValue($path)
6363
return $value;
6464
}
6565

66+
/**
67+
* @param $path string
68+
* @return Errors\Error|null
69+
*/
70+
public function getPathError($path)
71+
{
72+
$path = explode('.', $path);
73+
$result = $this;
74+
foreach ($path as $step) {
75+
if(!isset($result->fields[$step])) {
76+
return null;
77+
}
78+
79+
$result = $result->fields[$step];
80+
}
81+
82+
return $result->firstError();
83+
}
84+
85+
/**
86+
* @param $path string
87+
* @return bool
88+
*/
89+
public function isPathValid($path)
90+
{
91+
return $this->getPathError($path) === null;
92+
}
93+
94+
6695
/**
6796
* @param $path string
6897
* @return mixed

src/PHPixie/Validate/Rules/Rule/Value.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ class Value implements \PHPixie\Validate\Rules\Rule
2020
* @var bool
2121
*/
2222
protected $isRequired = false;
23+
24+
/**
25+
* @var string
26+
*/
27+
protected $requiredErrorMessage = null;
28+
2329
/**
2430
* @var array
2531
*/
@@ -35,12 +41,13 @@ public function __construct($ruleBuilder)
3541
}
3642

3743
/**
38-
* @param bool $isRequired
44+
* @param string $errorMessage
3945
* @return $this
4046
*/
41-
public function required($isRequired = true)
47+
public function required($errorMessage = null)
4248
{
43-
$this->isRequired = $isRequired;
49+
$this->isRequired = true;
50+
$this->requiredErrorMessage = $errorMessage;
4451
return $this;
4552
}
4653

@@ -192,7 +199,7 @@ public function validate($value, $result)
192199

193200
if($isEmpty) {
194201
if($this->isRequired) {
195-
$result->addEmptyValueError();
202+
$result->addEmptyValueError($this->requiredErrorMessage);
196203
}
197204
return;
198205
}

src/PHPixie/Validate/Validator.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ class Validator
1515
*/
1616
protected $results;
1717
/**
18-
* @var Rule
18+
* @var Rule\Value
1919
*/
2020
protected $rule;
2121

2222
/**
2323
* Validator constructor.
2424
* @param $results Results
25-
* @param $rule Rule
25+
* @param $rule Rule\Value
2626
*/
2727
public function __construct($results, $rule)
2828
{
@@ -31,7 +31,7 @@ public function __construct($results, $rule)
3131
}
3232

3333
/**
34-
* @return Rule
34+
* @return Rule\Value
3535
*/
3636
public function rule()
3737
{

src/PHPixie/Validate/Values/Value.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,21 @@ abstract class Value
1212
* @var bool
1313
*/
1414
protected $isRequired = false;
15+
16+
/**
17+
* @var string
18+
*/
19+
protected $requiredErrorMessage = null;
20+
1521
/**
1622
* @var array
1723
*/
1824
protected $rules = array();
1925

20-
public function required()
26+
public function required($errorMessage = null)
2127
{
2228
$this->isRequired = true;
29+
$this->requiredErrorMessage = $errorMessage;
2330
}
2431

2532
/**
@@ -32,9 +39,9 @@ public function callback($callback)
3239
}
3340

3441
/**
35-
* @param $conditionCallback
42+
* @param $callback
3643
*/
37-
public function conditional($conditionCallback)
44+
public function conditional($callback)
3845
{
3946
$rule = $this->getConditionalRule();
4047
$rule = $this->ruleBuilder->callback($callback);
@@ -51,13 +58,13 @@ public function validate($value, $result)
5158

5259
if($isEmpty) {
5360
if(!$this->isRequired) {
54-
$result->emptyError();
61+
$result->emptyError($this->requiredErrorMessage);
5562
}
5663
return;
5764
}
5865

5966
$this->validateValue($value, $result);
6067
}
6168

62-
protected function validateValue($value, $result);
69+
abstract protected function validateValue($value, $result);
6370
}

0 commit comments

Comments
 (0)