Detect Form Errors in PHP

You can use perch:error tags in your Perch forms to conditionally display errors for the user:

<perch:error for="cv" type="filetype">
  <span class="error">File must be a PDF or Office document.</span>
</perch:error>

What if you wanted to detect the error in PHP too? Perhaps the form is initially hidden in a tab or a modal and detecting a form error in PHP allows you to display the form on page load instead of it failing silently giving no helpful feedback to the user.

You can grab the errors in PHP like so:

$form_errors = $Perch->form_errors;

Check if any Perch form on the page has errors:

if($form_errors) {
    // you have errors
}

You can also check if a specific Perch form has an error which can be useful if you have more than one form on the page. The following checks if the form with the id="careers" has an error:

if(isset($form_errors['careers'])) {
    // you have errors
}
link

Related articles