PhpRiot
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store

Check if a form is valid

After a form is submitted, you will need to check and see if it passes validations. Each element is checked against the data provided; if a key matching the element name is not present, and the item is marked as required, validations are run with a NULL value.

Where does the data come from? You can use $_POST or $_GET, or any other data source you might have at hand (web service requests, for instance):

<?php
if ($form->isValid($_POST)) {
    
// success!
} else {
    
// failure!
}

With AJAX requests, you can sometimes get away with validating a single element, or groups of elements. isValidPartial() will validate a partial form. Unlike isValid(), however, if a particular key is not present, it will not run validations for that particular element:

<?php
if ($form->isValidPartial($_POST)) {
    
// elements present all passed validations
} else {
    
// one or more elements tested failed validations
}

An additional method, processAjax(), can be used for validating partial forms. Unlike isValidPartial(), it returns a JSON-formatted string containing error messages on failure.

Assuming your validations have passed, you can now fetch the filtered values:

<?php
$values 
$form->getValues();

If you need the unfiltered values at any point, use:

<?php
$unfiltered 
$form->getUnfilteredValues();

If you on the other hand need all the valid and filtered values of a partially valid form, you can call:

<?php
$values 
$form->getValidValues($_POST);

Zend Framework