Commit 06ecc69d2cb6e42489a5182b8cd72f494103cec9
1 parent
db902f1e
email validation
Showing
1 changed file
with
36 additions
and
0 deletions
Show diff stats
app/library/App/Controllers/UserController.php
@@ -3,7 +3,11 @@ | @@ -3,7 +3,11 @@ | ||
3 | namespace App\Controllers; | 3 | namespace App\Controllers; |
4 | 4 | ||
5 | use App\Model\User; | 5 | use App\Model\User; |
6 | +use Phalcon\Validation; | ||
7 | +use Phalcon\Validation\Validator\Email; | ||
6 | use PhalconRest\Mvc\Controllers\CrudResourceController; | 8 | use PhalconRest\Mvc\Controllers\CrudResourceController; |
9 | +use PhalconApi\Exception; | ||
10 | +use PhalconApi\Constants\ErrorCodes; | ||
7 | 11 | ||
8 | class UserController extends CrudResourceController | 12 | class UserController extends CrudResourceController |
9 | { | 13 | { |
@@ -101,4 +105,36 @@ class UserController extends CrudResourceController | @@ -101,4 +105,36 @@ class UserController extends CrudResourceController | ||
101 | } | 105 | } |
102 | } | 106 | } |
103 | 107 | ||
108 | + protected function beforeHandleWrite() | ||
109 | + { | ||
110 | + $email_field = 'email'; | ||
111 | + | ||
112 | + $validation = new Validation(); | ||
113 | + | ||
114 | + $validation->add( | ||
115 | + $email_field, | ||
116 | + new Email( | ||
117 | + [ | ||
118 | + "message" => "The e-mail is not valid", | ||
119 | + ] | ||
120 | + ) | ||
121 | + ); | ||
122 | + | ||
123 | + $data = $this->getPostedData(); | ||
124 | + | ||
125 | + if (!isset($data[$email_field])) { | ||
126 | + | ||
127 | + $message = $validation->validate($data[$email_field]); | ||
128 | + if (count($message)) { | ||
129 | + throw new Exception(ErrorCodes::DATA_FAILED, 'Unable to create item', [ | ||
130 | + 'messages' => $message, | ||
131 | + 'data' => $data[$email_field] | ||
132 | + ]); | ||
133 | + } | ||
134 | + | ||
135 | + } | ||
136 | + | ||
137 | + | ||
138 | + } | ||
139 | + | ||
104 | } | 140 | } |