コード例 #1
0
ファイル: example3.php プロジェクト: mncedim/htmlform
 *
 * Render form fields individually
 */
require_once 'vendor/autoload.php';
require_once 'DemoForm.php';
//required for the csrf to work
session_start();
/*
 * Usage Example
 */
$form = new DemoForm();
$form->setAttributes(['novalidate']);
//handle POST
if (isset($_POST[$form->getName()])) {
    //populates the form with posted data
    $form->handlePostRequest();
    //validate the form using our custom validator defined above
    if ($form->isValid()) {
        //success!
        echo '<strong>' . $form->getName() . ' submitted valid data.</strong><br/>';
    }
    //encountered errors will be displayed when the form re-renders
}
//display errors at the top instead of inline
$formErrors = $form->getErrors();
if (sizeof($formErrors) > 0) {
    echo '<h3>Form errors:</h3><ul>';
    foreach ($formErrors as $field => $error) {
        echo sprintf('<li>%s - %s</li>', $field, $error);
    }
    echo '</ul>';