Server-side form data validation
Step 1 - Coding the view dataformview.php
The view dataformview.php
must be installed into the INSTALL_DIR/applications/default/app/view/
folder.
- The input form
id="my-data-form"
is declared with thedata-zdk-submit
attribute to specify the controller and the action to call on form submit. - All the input fields are declared voluntarily with the
type="text"
attribute, even for the fields used to input the minimal and maximal numeric values because we want the data to be validated on the server-side.
Obviously, in a real world input form, the two numeric fields would be preferably declared with thetype="number"
attribute. - A red asterisk is added to the Title label via the
zdk-required
ZnetDK CSS class.
On the other hand, norequired
attribute is voluntarily defined for the corresponding input fieldname="title"
as we want to check in PHP on the server-side, that a value is indeed entered. - Finally, to connect the input form to the remote PHP controller action specified via the the
data-zdk-submit
attribute, the ZnetDK methodznetdkMobile.form.make()
is called in JavaScript.
So, when the submit button is clicked, the data entered in the form are sent in AJAX through POST parameters to theapp\controller\FormValidCtrl::action_submit()
PHP method described in next chapter.
View dataformview.php
<form id="my-data-form" class="w3-padding-16" data-zdk-submit="formvalidctrl:submit">
<label class="zdk-required"><b>Title</b></label><br>
<input class="w3-input w3-border" type="text" name="title" placeholder="This field is mandatory"><br>
<label><b>Min. number</b></label><br>
<input class="w3-input w3-border" type="text" name="min_number" placeholder="Must be the lower value"><br>
<label><b>Max. number</b></label><br>
<input class="w3-input w3-border" type="text" name="max_number" placeholder="Must be the higher value"><br>
<label><b>Other value</b></label><br>
<input class="w3-input w3-border" type="text" name="other" placeholder="This field is optional"><br>
<button class="w3-button w3-block w3-green w3-section w3-padding" type="submit"><i class="fa fa-check"></i> Submit</button>
</form>
<script>
console.log('dataformview.php');
znetdkMobile.form.make('#my-data-form');
</script>
HTM5
JS
Step 2 - Coding the controller formvalidctrl.php
The controller formvalidctrl.php
must be installed into the INSTALL_DIR/applications/default/app/controller/
folder.
- The custom
app\controller\FormValidCtrl
class is derived from the\AppController
ZnetDK class to be identified as an application controller. - The
action_submit()
method, a controller's action, is called on submit of the input form.
The form data is validated with the help of the\app\validator\FormValidator()
customValidator
class described in next chapter. - If data validation failed, an error message is returned in the response of the HTTP request by calling the
\Response::setFailedMessage()
method. - Otherwise if data validation succeeded, then a success message is returned by calling the
\Response::setSuccessMessage()
method.
Controller formvalidctrl.php
<?php
namespace app\controller;
class FormValidCtrl extends \AppController {
static protected function action_submit() {
$response = new \Response();
$validator = new \app\validator\FormValidator();
$validator->setCheckingMissingValues();
if (!$validator->validate()) {
$response->setFailedMessage(NULL, $validator->getErrorMessage(),
$validator->getErrorVariable());
return $response;
}
$response->setSuccessMessage(NULL, 'Form data checked.');
return $response;
}
}
PHP
Step 3 - Coding the validator formvalidator.php
The validator formvalidator.php
must be installed into the INSTALL_DIR/applications/default/app/validator/
folder.
- The
FormValidator
custom class is derived from the\Validator
ZnetDK class, specialized in data validation. - The
initVariables()
must be implemented to indicate as an array, the names of the the POST parameters whose values the validator must check. - The three other methods declared in the class with their name prefixed by
check_
are responsible for validating the value of a particular input field.
For example, thecheck_title()
method checks the value of the input field namedtitle
. - If the entry field value passed in parameter of the method through the
$value
variable is correct, then the method returnsTRUE
.
Otherwise, if the value is incorrect, the error message to be displayed to the end user is set by calling theValidator::setErrorMessage()
method and the valueFALSE
is returned. - As shown in the
check_max_number()
method, the value of the input field namedmin_number
is read by calling theValidator::getValue()
method.
Validator formvalidator.php
<?php
namespace app\validator;
class FormValidator extends \Validator {
protected function initVariables() {
return array('title','min_number','max_number');
}
protected function check_title($value) {
if (strlen($value) === 0) {
$this->setErrorMessage('Title is missing.');
return FALSE;
}
return TRUE;
}
protected function check_min_number($value) {
if (strlen($value) > 0 && !is_numeric($value)) {
$this->setErrorMessage('Min. number is not a number.');
return FALSE;
}
return TRUE;
}
protected function check_max_number($value) {
if (strlen($value) > 0 && !is_numeric($value)) {
$this->setErrorMessage('Max. number is not a number.');
return FALSE;
}
if (strlen($value) > 0 && $this->getValue('min_number') >= $value) {
$this->setErrorMessage('Max. number is not higher than the min. number');
return FALSE;
}
return TRUE;
}
}
PHP
Step 4 - Adding "Data form" item to the navigation menu
Finally, to give users access to the dataformview.php
view, the menu's item named Data form is declared into the menu.php
script of the Web App (see Get Started | New menu items for more information).
The menu.php
script is located into the INSTALL_DIR/applications/default/app/
folder.
Script menu.php
static public function initAppMenuItems() {
// ... Here, some menu items...
\MenuManager::addMenuItem(NULL, 'dataformview', 'Data form', 'fa-check-circle-o');
// ... Here, other menu items...
}
PHP