Validation for forms and various other inputs can be performed by making use of Zend\ InputFilter. This component allows filtering and validation of generic sets of input data. For specific form elements you can applyvalidation and filtering on the specific elements, but if we have to filter an input set like a $_GET request or a $_POST request, this can be implemented using the InputFilter class.
In our next task, we will be adding the InputFilter class to our registration form.
To add an InputFilter class to an existing form, we need to create a new InputFilter
class and use it during form submission for validation, as shown in the following steps:
1. Create a new InputFilter class in src/Users/Form/RegisterFilter.php.
The RegisterFilter class will extend the Zend\InputFilter\InputFilter
class and will add all the necessary validators in its constructor:
<?php
namespace Users\Form;
use Zend\InputFilter\InputFilter;
class RegisterFilter extends InputFilter
{
public function construct()
{
2. Using the $this->add() method, we can add various filter options to the registration form:
1. For the Email Address field, we will add a validator to check if the value entered is a valid e-mail address:
$this->add(array(
'name' => 'email', 'required' => true, 'validators' => array(
array(
'name' => 'EmailAddress', 'options' => array(
'domain' => true,
),
),
),
));
2. For the Name field, we will add a validator to limit the size between 2 to
140 characters and will also add a filter to strip the HTML tags:
$this->add(array(
'name' => 'name', 'required' => true, 'filters' => array(
array(
'name' => 'StripTags',
),
'validators' => array( array(
'name' => 'StringLength', 'options' => array(
'encoding' => 'UTF-8', 'min' => 2,
'max' => 140,
),
),
),
));
3. For the Password and Confirm Password fields, we will not add any validators but will make them mandatory:
'password' ));
$this->add(array(
'name' => 'confirm_password', 'required' => true,
));
3. This InputFilter class is not mapped to the RegisterForm class yet; we will be performing the validation during form submission. We need to modify the RegisterController class to enable theprocessAction method and validate the form upon submission.
4. Modify the RegisterController class to enable the processAction method:
public function processAction()
{
if (!$this->request->isPost()) {
return $this->redirect()->toRoute(NULL , array( 'controller' => 'register',
'action' => 'index'
));
}
$post = $this->request->getPost();
$form = new RegisterForm();
$inputFilter = new RegisterFilter();
$form->setInputFilter($inputFilter);
$form->setData($post);
if (!$form->isValid()) {
$model = new ViewModel(array( 'error' => true,
'form' => $form,
));
$model->setTemplate('users/register/index'); return $model;
}
return $this->redirect()->toRoute(NULL , array( 'controller' => 'register',
'action' => 'confirm'
));
}
5. Now open the registration page in your web browser and test the validation:
What just happened?
We have now enabled validation on the registration form. In the processAction() function of the RegisterController class, you will see that a new instance of the RegisterFrom class is created andRegisterFilter is applied to the form using the
$form->setInputFilter() method. The data entered as input to the form is added again and validation is performed by using the isValid() method. Error messages are rendered
in the form using the FormElementErrors view helper.
We need to ensure that the names in the InputFilter class properly map to the names in the form while adding validation to InputFilter.
Models and database access
Models provide a representation of data in the MVC application. There is no Zend\Model component that is provided by Zend Framework, so developers have to decide on the implementation part of models. Models by themselves cannot talk to databases and fetch or process data, so they are usually connected to mapper objects or use ORM to connect to databases. For this example, we will be using a TableGateway pattern for storing data in the database.
Không có nhận xét nào:
Đăng nhận xét