Example #1
0
<?php

include_once '../boot.php';
//use test bootstrap's autoloader
$form = new \ClosureForm\Form('signup-form', array('method' => 'post', 'action' => '#', 'class' => 'form'));
/* username */
$form->addTextField('email')->label('Email Address')->validator(function ($value) {
    //first validator
    if (strlen($value) < 3) {
        return 'Email cannot be less than 3 characters';
    }
})->validator(function ($value) {
    //second validator
    if (!filter_var($value, FILTER_VALIDATE_EMAIL)) {
        return "This is not a valid email address";
    }
});
/* password + confirmation */
$form->addPasswordField('password')->label('Password')->validator(function ($value) {
    if (strlen($value) < 5) {
        return 'Password cannot be less than 5 characters';
    }
});
$form->addPasswordField('confirm_password')->label('Confirm Password')->validator(function ($value) use($form) {
    //example of using another field's value in a field validator via the USE keyword
    if ($value != $form->getField('password')->getSubmittedValue()) {
        return 'Password Confirmation did not match password';
    }
});
/* submit */
$form->addButton('submit')->label('Submit')->action(function ($form) {
Example #2
0
 /**
  * @group current
  * @expectedException RuntimeException
  */
 public function testGetGlobalFromFormWithInvalidMethodAttr()
 {
     $form = new \ClosureForm\Form('invalid-form', array('method' => 'snail-mail'));
     $form->getSuperglobal();
 }