Skip to content

Commit 511b11c

Browse files
committed
Add possibility to correctly inject service manager to the ZF form, and process the input filters and elements.
1 parent f7b73c8 commit 511b11c

1 file changed

Lines changed: 83 additions & 83 deletions

File tree

src/Form/UserForm.php

Lines changed: 83 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@
66
use Zend\Form\Form;
77
use Zend\InputFilter\InputFilter;
88
use ProspectOne\UserModule\Validator\UserExistsValidator;
9+
use Zend\InputFilter\InputFilterProviderInterface;
910
use Zend\Validator\Hostname;
1011

1112
/**
1213
* This form is used to collect user's email, full name, password and status. The form
1314
* can work in two scenarios - 'create' and 'update'. In 'create' scenario, user
1415
* enters password, in 'update' scenario he/she doesn't enter password.
1516
*/
16-
class UserForm extends Form
17+
class UserForm extends Form implements InputFilterProviderInterface
1718
{
1819
/**
1920
* Scenario ('create' or 'update').
@@ -87,7 +88,13 @@ protected function getRoleSelector(){
8788
* @param mixed $rolesselector
8889
* @param int $rolecurrent
8990
*/
90-
public function __construct($scenario = 'create', EntityManager $entityManager = null, UserInterface $user = null, $rolesselector = null, $rolecurrent = null)
91+
public function __construct(
92+
$scenario = 'create',
93+
EntityManager $entityManager = null,
94+
UserInterface $user = null,
95+
$rolesselector = null,
96+
$rolecurrent = null
97+
)
9198
{
9299
// Define form name
93100
parent::__construct('user-form');
@@ -101,15 +108,12 @@ public function __construct($scenario = 'create', EntityManager $entityManager =
101108
$this->user = $user;
102109
$this->rolesselector = $rolesselector;
103110
$this->rolecurrent = $rolecurrent;
104-
105-
$this->addElements();
106-
$this->addInputFilter();
107111
}
108112

109113
/**
110114
* This method adds elements to form (input fields and submit button).
111115
*/
112-
protected function addElements()
116+
public function init()
113117
{
114118
// Add "email" field
115119
$this->add([
@@ -209,108 +213,104 @@ protected function addElements()
209213
/**
210214
* This method creates input filter (used for form filtering/validation).
211215
*/
212-
protected function addInputFilter()
216+
public function getInputFilterSpecification()
213217
{
214-
// Create main input filter
215-
$inputFilter = new InputFilter();
216-
$this->setInputFilter($inputFilter);
217-
218218
// Add input for "email" field
219-
$inputFilter->add([
220-
'name' => 'email',
221-
'required' => true,
222-
'filters' => [
223-
['name' => 'StringTrim'],
224-
],
225-
'validators' => [
226-
[
227-
'name' => 'StringLength',
228-
'options' => [
229-
'min' => 1,
230-
'max' => 128
231-
],
232-
],
233-
[
234-
'name' => 'EmailAddress',
235-
'options' => [
236-
'allow' => Hostname::ALLOW_DNS,
237-
'useMxCheck' => false,
238-
],
239-
],
240-
[
241-
'name' => UserExistsValidator::class,
242-
'options' => [
243-
'entityManager' => $this->entityManager,
244-
'user' => $this->user
245-
],
246-
],
247-
],
248-
]);
249-
250-
// Add input for "full_name" field
251-
$inputFilter->add([
252-
'name' => 'full_name',
253-
'required' => true,
254-
'filters' => [
255-
['name' => 'StringTrim'],
256-
],
257-
'validators' => [
258-
[
259-
'name' => 'StringLength',
260-
'options' => [
261-
'min' => 1,
262-
'max' => 512
263-
],
264-
],
265-
],
266-
]);
267-
268-
if ($this->scenario == 'create') {
269-
270-
// Add input for "password" field
271-
$inputFilter->add([
272-
'name' => 'password',
219+
$inputFilters = [
220+
[
221+
'name' => 'email',
273222
'required' => true,
274223
'filters' => [
224+
['name' => 'StringTrim'],
275225
],
276226
'validators' => [
277227
[
278228
'name' => 'StringLength',
279229
'options' => [
280-
'min' => 6,
281-
'max' => 64
230+
'min' => 1,
231+
'max' => 128
232+
],
233+
],
234+
[
235+
'name' => 'EmailAddress',
236+
'options' => [
237+
'allow' => Hostname::ALLOW_DNS,
238+
'useMxCheck' => false,
239+
],
240+
],
241+
[
242+
'name' => UserExistsValidator::class,
243+
'options' => [
244+
'entityManager' => $this->entityManager,
245+
'user' => $this->user
282246
],
283247
],
284248
],
285-
]);
286-
287-
// Add input for "confirm_password" field
288-
$inputFilter->add([
289-
'name' => 'confirm_password',
249+
], [
250+
'name' => 'full_name',
290251
'required' => true,
291252
'filters' => [
253+
['name' => 'StringTrim'],
292254
],
293255
'validators' => [
294256
[
295-
'name' => 'Identical',
257+
'name' => 'StringLength',
296258
'options' => [
297-
'token' => 'password',
259+
'min' => 1,
260+
'max' => 512
298261
],
299262
],
300263
],
264+
]
265+
];
266+
267+
if ($this->scenario == 'create') {
268+
269+
// Add input for "password" field
270+
$inputFilters = array_merge_recursive($inputFilters, [
271+
[
272+
'name' => 'password',
273+
'required' => true,
274+
'filters' => [
275+
],
276+
'validators' => [
277+
[
278+
'name' => 'StringLength',
279+
'options' => [
280+
'min' => 6,
281+
'max' => 64
282+
],
283+
],
284+
],
285+
], [
286+
'name' => 'confirm_password',
287+
'required' => true,
288+
'filters' => [
289+
],
290+
'validators' => [
291+
[
292+
'name' => 'Identical',
293+
'options' => [
294+
'token' => 'password',
295+
],
296+
],
297+
],
298+
]
301299
]);
302300
}
303301

304302
// Add input for "status" field
305-
$inputFilter->add([
306-
'name' => 'status',
307-
'required' => true,
308-
'filters' => [
309-
['name' => 'ToInt'],
310-
],
311-
'validators' => [
312-
['name' => 'InArray', 'options' => ['haystack' => [1, 2]]]
313-
],
303+
return array_merge_recursive($inputFilters, [
304+
[
305+
'name' => 'status',
306+
'required' => true,
307+
'filters' => [
308+
['name' => 'ToInt'],
309+
],
310+
'validators' => [
311+
['name' => 'InArray', 'options' => ['haystack' => [1, 2]]]
312+
]
313+
]
314314
]);
315315
}
316316
}

0 commit comments

Comments
 (0)