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+ }
0 commit comments