Exemple #1
0
<?php

require_once 'vendor/autoload.php';
require_once 'DemoForm.php';
$loader = new Twig_Loader_Filesystem(__DIR__ . '/templates');
$twig = new Twig_Environment($loader, array('cache' => false));
$ext = new \Mncedim\Html\Form\Twig\Extension();
$twig->addExtension($ext);
//required for the csrf to work
session_start();
/*
 * Usage Example
 */
$form = new DemoForm();
echo $twig->render('twig-example.html.twig', ['form' => $form->prepareView()]);
Exemple #2
0
    //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>';
}
echo $form->prepareView()->render('open');
echo $form->getFields('_csrf')->render();
//render name field label, field, errors separately
echo '<div>';
echo $form->getFields('name')->render('label');
echo $form->getFields('name')->render('field');
//hide this fields errors
//echo $form->getFields('name')->render('errors');
echo '</div>';
echo $form->getFields('gender')->render();
echo $form->getFields('favourite_sport')->render();
//hide this fields errors
echo $form->getFields('terms')->displayErrors(false)->render();
echo $form->getFields('save')->render();
echo $form->render('close');
Exemple #3
0
 * Date: 15/11/29
 * Time: 8:47 AM
 */
require_once 'vendor/autoload.php';
require_once 'DemoForm.php';
//required for the csrf to work
session_start();
/*
 * Usage Example
 */
$form = new DemoForm();
$form->getFields('favourite_colours')->addOption('yellow')->removeOption('blue');
$form->getFields('bio')->removeAttribute('rows')->addAttribute('rows', 10);
$form->getFields('name')->removeAttribute('required')->removeClass('class1');
$form->getFields('communication')->addOption('Type', ['one' => 'One', 'two' => 'Two'])->removeOption('Other');
//add button before the save button
$form->addField('text_button', 'button', array('value' => 'Click Me'), 'save')->getFields('text_button')->addAttribute('type', 'button')->addClass('btn')->addClass('btn-primary');
//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/>';
        var_dump($_POST);
    }
    //encountered errors will be displayed when the form re-renders
}
echo $form->prepareView()->render();