Ejemplo n.º 1
0
            if (strtolower($submitted) == 'paul') {
                $FormData->setError('first-name', 'cannot be Paul, sorry');
                // message will be prefixed with field label
            }
        }
        // assign an error to a field without generating a message - for example - create an error if the 'colour' is yellow (ID = 2) but the checkbox is not checked
        if ($FormData->getClean('colour') == '2' && $FormData->getClean('ok') == 'no') {
            $FormData->setError('colour');
            // this field will be highlighted as having an error but no message will be logged, the next line will create the message for both fields
            $FormData->setError('ok', 'If your favourite colour is yellow then you must be OK', false);
            // to disable the field label being prefixed, add a 3rd argument 'false'
        }
        /* ***************************************************************************************************** */
        if ($FormData->hasError()) {
            // there was some errors, prepare the data for an error message
            $form_report = '<div class="alert alert-danger" role="alert"><h4>Form report: Errors!</h4><p>' . $FormData->getErrorList(true) . '</p></div>';
        } else {
            // there were no errors
            $form_report = '<div class="alert alert-success" role="alert"><h4>Form report: Success!</h4><p>There was no errors</p></div>';
        }
    }
} catch (Exception $e) {
    echo 'Exception caught: ' . $e->getMessage();
    exit;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="utf-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
Ejemplo n.º 2
0
// Report all errors except E_NOTICE
error_reporting(E_ALL & ~E_NOTICE);
// include the class file
include __DIR__ . '/../FormData/FormData.php';
$FormData = new FormData();
try {
    // create the form fields and set parameters
    $FormData->setFields([['type' => 'text', 'label' => 'First name', 'name' => 'first-name', 'id' => 'first-name', 'required' => '1', 'required_message' => 'We\'d love to know your first name!', 'max_length' => '50'], ['type' => 'number', 'label' => 'Favourite multiple of 5', 'id' => 'number', 'required' => '1', 'min' => '0', 'max' => '100', 'step' => '5'], ['type' => 'select', 'label' => 'Favourite colour', 'id' => 'colour', 'required' => '1', 'option' => ['1' => 'Red', '2' => 'Yellow', '3' => 'Blue']], ['type' => 'checkbox', 'label' => 'Are you OK?', 'id' => 'ok', 'checkbox_value' => 'yes', 'checkbox_value_alt' => 'no']]);
    // check for form submission
    if ($_POST['submit'] == 'submit') {
        // search POST array for all fields that were set above and check submitted data
        $FormData->checkPost();
        if ($FormData->hasError()) {
            // there was some errors - handle them here
            // an object containing all error fields with user friendly messages can be obtained as follows
            $error_obj = $FormData->getErrorList();
            // an HTML ready list of error messages (separated with '<br />') can be obtained as follows
            $error_message = $FormData->getErrorList(true);
            // just as an example, prepare the data for a confirmation message
            $form_report = '<div class="alert alert-danger" role="alert"><h4>Form report: Errors!</h4><p>' . $error_message . '</p></div>';
        } else {
            // there were no errors so the information can be processed
            // example of extracting data from the object
            $first_name = $FormData->getClean('first-name');
            $surname = $FormData->getClean('surname');
            $colour = $FormData->getClean('colour');
            $ok = $FormData->getClean('ok');
            // put the data in a confirmation message
            $form_report = '<div class="alert alert-success" role="alert">';
            $form_report .= '<h4>Form report: Success!</h4>';
            $form_report .= '<p>';