I am beginner in Zend Framework 3 and I want to hear your opinions about code that I wrote. I don't know how exactly I can connect forms, and validators together that the code will be quite nice and readable.
So I wait for yours advices....what I need to correct, what doesn't have sense, what could be done better...etc...
/register method
public function registerAction()
{
$form = $this->form;
$viewModel = new ViewModel(['form' => $form]);
$prg = $this->prg('users', false);
if ($prg instanceof \Zend\Http\PhpEnvironment\Response)
return $prg;
elseif ($prg === false)
return $viewModel;
$loginExistsValidator = new NoRecordExists(
[
'table' => 'users',
'field' => 'login',
'adapter' => $this->command->getAdapter()
]
);
$loginExistsValidator->setMessage('Istnieje już użytkownik o podanym loginie');
$emailExistsValidator = new NoRecordExists(
[
'table' => 'users',
'field' => 'email',
'adapter' => $this->command->getAdapter()
]
);
$emailExistsValidator->setMessage('Ten email jest już zajęty...');
$form->setInputFilter(new UsersFilter());
$form->getInputFilter()->get('login')->getValidatorChain()->attach($loginExistsValidator);
$form->getInputFilter()->get('email')->getValidatorChain()->attach($emailExistsValidator);
$form->setData($prg);
if (!$form->isValid())
return $viewModel;
$users = $this->command->addUser($form->getData());
$viewModel->setTemplate('users/register/registerSukcesfull');
return ['users' => $users];
}
/usersFilter
<?php
/**
* Created by PhpStorm.
* User: Patryk
* Date: 2016-10-09
* Time: 12:19
*/namespace Users\Filters;
use Users\Services\RegisterServices;
use Zend\Filter\Callback;
use Zend\Filter\StringTrim;
use Zend\InputFilter\InputFilter;
use Zend\Validator\Db\NoRecordExists;
use Zend\Validator\Db\RecordExists;
use Zend\Validator\EmailAddress;
use Zend\Validator\Regex;
use Zend\Validator\StringLength;
class UsersFilter extends InputFilter
{
public function __construct()
{
$this->add([
'name' => 'login',
'required' => true,
'filters' => [
[
'name' => StringTrim::class
]
],
'validators' => [
[
'name' => StringLength::class,
'options' => [
'min' => 5
]
],
],
]
);
$this->add([
'name' => 'password',
'required' => true,
'validators' => [
[
'name' => StringLength::class,
'options' => [
'min' => 12
]
],
],
]
);
$this->add([
'name' => 'email',
'required' => true,
'validators' => [
[
'name' => EmailAddress::class,
'options' =>
[
'domains' => true
]
]
]
]);
}
}