public function addForm(phForm $form) { /* * check the form has not been initialised, if it has then we * must throw an exception because the names will be written * wrong */ if ($form->isInitialized()) { throw new phFormException("The form {$form->getName()} has already been initialized. You must add a subform straight after creation."); } $name = $form->getName(); if ($this->entityExists($name)) { throw new phFormException("An entity with the name '{$name}' already exists"); } /* * setup the name format for the sub form */ $form->setNameFormat(sprintf($this->getNameFormat(), $name) . '[%s]'); $form->setIdFormat(sprintf($this->getIdFormat(), $name) . '_%s'); $this->_forms[$name] = $form; }
/* * phLoader handles the loading of various Minacl classes */ require_once dirname(__FILE__) . '/../../lib/form/phLoader.php'; phLoader::registerAutoloader(); /* * register a file view loader so Minacl knows where * to look for form view files */ phViewLoader::setInstance(new phFileViewLoader(dirname(__FILE__))); /* * create a phForm instance. The first argument specifies the name * of the post or get array that will have the forms data (so register[...]) * The second argument is the form template to use. */ $form = new phForm('register', 'registerForm'); /* * set the various validators */ $form->fullname->setValidator(new phRequiredValidator()); // note that fullname is the value of the name helper in the form and NOT the id helper $form->email->setValidator(new phRequiredValidator()); $form->password->setValidator(new phRequiredValidator()); $form->confirmPassword->setValidator(new phCompareValidator($form->password, phCompareValidator::EQUAL)); if ($_SERVER['REQUEST_METHOD'] == 'POST') { /* * data has been posted back, bind it to the form */ $form->bindAndValidate($_POST['register']); if ($form->isValid()) { /*
public function testRadioButtons() { $form = new phForm('test', 'radioButtonTestView'); $form->bind(array('name' => 'Rob', 'type' => '2')); $this->assertEquals($form->element()->name->getRawValue(), 'Rob', 'name has been set to Rob'); $this->assertEquals($form->element()->type1->isChecked(), false, 'type1 is NOT checked'); $this->assertEquals($form->element()->type2->isChecked(), true, 'type2 is checked'); $this->assertEquals($form->element()->type3->isChecked(), false, 'type3 is NOT checked'); }
* * @author Rob Graham <*****@*****.**> * @package phform * @subpackage examples.datetime */ /* * usual setup code */ require_once dirname(__FILE__) . '/../../lib/form/phLoader.php'; phLoader::registerAutoloader(); phViewLoader::setInstance(new phFileViewLoader(dirname(__FILE__))); /* * also need to include our custom class phDateTime */ require_once 'phDateTime.php'; $form = new phForm('event', 'eventView'); /* * last 2 params say the only available year should * be 2011 */ $timeForm = new phDateTime('time', 'dateTimeView', 2011, 2011); /* * Add the datetime form as a subform to the master event * form. */ $form->addForm($timeForm); /* * add validators to the time so the date time chosen must be between * the start and end of January */ $errorMessage = array(phCompareValidator::INVALID => 'The event must occur sometime in January');
/** * Gets all errors for the form */ public function allErrors() { if ($this->getDom()) { return $this->_form->getErrors(); } }
*/ /** * For the sub-forms example * (http://minacl.org/learn-by-example/74-3-using-sub-forms.html) * * @author Rob Graham <*****@*****.**> * @package phform * @subpackage examples.subform */ require_once dirname(__FILE__) . '/../../lib/form/phLoader.php'; phLoader::registerAutoloader(); phViewLoader::setInstance(new phFileViewLoader(dirname(__FILE__))); /* * create the main form */ $form = new phForm('deliver', 'deliveryForm'); /* * create the address form, the first argument * needs to match what we have in the form helper * on the deliveryForm template */ $address = new phForm('address', 'addressForm'); /* * add the sub form to the main form */ $form->addForm($address); if ($_SERVER['REQUEST_METHOD'] == 'POST') { /* * data has been posted back, bind it to the form */ $form->bindAndValidate($_POST['deliver']);
public function __construct($name, $template, $startYear, $endYear) { parent::__construct($name, $template); $this->_startYear = $startYear; $this->_endYear = $endYear; }