Aspen:Form Validation

From Aspen Documentation

When developing forms you'll often want to validate incoming data. After your isSubmitted function call you'll want to place in code similar to the following.

if(!$this->APP->form->isFilled('page_title')){
	$this->APP->form->addError('form_field', 'You must enter some content.');
}
 
 
if($this->APP->form->save()){
	$this->APP->router->redirect('view');
}

Before you save the form, we need to validate any fields. We add in condition statements that check for a validation rule and if false, it adds a new error. The act of adding an error will prevent the form from saving, and if you return users to the page and your template has the printErrors function or similar display, they will see a list of all errors.

The save function will fail if any errors have been added. However, you may use error to add some additional logic to handle the failure.

To display error messages on the original form, use the following function.

<?php print $this->APP->form->printErrors(); ?>

From within the config you may control the output html, or using getErrors you may easily write your own display logic. Error messages are associated with field names, making it easy for you to display errors associated with their fields.

By default, all acceptable POST values will return to their associated fields saving the user from refilling in data. If editing a record, the original record data will be loaded first, then POST data, so the form values should be the most accurate available.