/** * Created by PhpStorm. * User: mncedim * Date: 15/11/29 * Time: 9:49 AM * * 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) {
<?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()]);
if ($_SERVER['REQUEST_METHOD'] == 'POST') { /** * Bind the form to the POST data. */ $form = new DemoForm($_POST); if ($form->isValid()) { /** * Do something with the form data. */ do_something($form->cleaned_data); } } else { /** * Instantiate an unbound form. */ $form = new DemoForm(); } ?> <html> <head> <?php echo $form->media; ?> </head> <body> <form action="" method="POST"> <table> <?php echo $form->html(); ?> <tr>
<?php /** * Created by PhpStorm. * User: mncedim * 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);