Esempio n. 1
0
<?php

/*
 * Two ways to declare select options.
 *
 * Do not mix the two methods as unexpected results can occur.
 */
// 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 select form field
    $FormData->setFields([['type' => 'select', 'label' => 'Options declared as pairs', 'id' => 'pairs', 'option' => ['1' => 'Blue', '2' => 'Red', '3' => 'Yellow', '4' => 'Green', '5' => 'Orange']], ['type' => 'select', 'label' => 'Options declared as sub arrays', 'id' => 'arrays', 'required' => '1', 'option' => [['1', 'Blue'], ['2', 'Red'], ['_disabled', 'Brown'], ['3', 'Green'], ['4', 'Orange'], ['_disabled', ''], ['10', 'Earth'], ['11', 'Wind'], ['12', 'Fire']]]]);
} 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">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<title>FormData: Select options</title>
	<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
	<!--[if lt IE 9]>
	<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
	<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
Esempio n. 2
0
<?php

// 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', 'required' => '1', 'required_message' => 'We\'d love to know your first name!', 'max_length' => '50'], ['type' => 'select', 'label' => 'Favourite colour', 'name' => 'colour', 'required' => '1', 'option' => ['1' => 'Red', '2' => 'Yellow', '3' => 'Blue']], ['type' => 'checkbox', 'label' => 'Are you OK?', 'name' => '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();
        /* ********************************* Adding some custom error messages ********************************* */
        // add a generic error that is not linked to any fields, these will always be added to the end of the list
        $FormData->setError(NULL, 'A generic error message');
        // perform further, custom checks on submitted data, for example - create an error if 'first-name' is Paul
        if (!$FormData->hasError('first-name')) {
            $submitted = $FormData->getClean('first-name');
            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'