This is the documentation for concrete5 version 5.6 and earlier. View Current Documentation

The Form validation helper aids developers by giving them an object-oriented API to validate web forms.

Loading The Helper

$val = Loader::helper('validation/form');

Setting the Data to Test Against

$val->setData($data);

Data is an array. Typically you will pass $_POST or $_REQUEST to this function.

$val->setFiles()

This function must be run if files are to be tested.

Setting Up The Fields

Add validation constraints to your $val object for different fields in your form. View example below.

Arguments

Here are the arguments used throughout each of the constraint functions

  • $field - the HTML name of the form element that this constraint applies to.
  • $errorMsg - an optional parameter that specifies the error message that should be displayed if this constraint fails.
  • $emptyIsOk - an optional boolean that determines whether it is acceptable if this parameter is left empty.

Methods

$val->addRequired($field, $errorMsg = null, $validate = ValidationFormHelper::VALID_NOT_EMPTY)

Specifies that a field is required in the form. By default, the $validate constant specifies that the field will be valid simply if it is not empty.

 

$val->addRequiredEmail($field, $errorMsg = null)

Adds a required email address to the suite of tests to be run.

 

 $val->addRequiredToken($action = '', $errorMsg = null)

Validates the form against a unique token as generated by the concrete5 token validation helper. The $action parameter should match the $action you provided to the token validation helper's output() function (if one was used).

 

$val->addInteger($value, $errorMsg, $emptyIsOk)

Adds a required field and tests that it is integer only.

 

$val->addUploadedFile($field, $errorMsg = null, $emptyIsOk = true)

Adds a test to a field to ensure that, if set, it is a valid uploaded file.

 

$val->addUploadedImage($field, $errorMsg = null, $emptyIsOk = true)

Adds a test to a field to ensure that, if set, it is a valid uploaded image.

Testing the Form

$response = $val->test()

Runs through all tests as defined above. Returns true if the test is passed, or false if there are errors.

Working with Errors

If your test() function returns false, you can grab the error object and display to a user.

ValidationErrorHelper $error = $val->getError();

You can learn more about the ValidationErrorHelper object in the Error Handling section.

Usage Example

Here is an example of the form validation helper when used from within a single page controller. If the test fails, the $errorObject will be available on the single page, and can be output or iterated through.

$val->setData($this->post());
$val->setFiles();
$val->addRequired('cName', 'You must specify a valid name for your group.');
$val->addRequired('cDescription', 'Your group should have at least some kind of description.');
$val->addInteger('zipcode', 'Your zip code should only contain numbers.');
$val->addUploadedImage('iconFID', 'The icon you uploaded does not appear to be an image.');
$val->addRequired('groupType', 'You must specify a group type.');
if ($val->test()) {
// proceed...
} else {
$errorArray = $val->getError()->getList();
$this->set('errorArray', $errorArray); // send array to view (in a single_page for example)
}

Output into view (single_page)

Once you have passed the array to the view you can iterate through it and display your errors

if (isset($errorArray)) { ... } 

 

Loading Conversation