static function create($definition, $object = null) { $form = patForms::createForm(null, array('name' => $definition->name)); foreach ($definition->elements as $el) { $element =& $form->createElement($el['name'], $el['type'], null); if (!empty($el['attributes']['datasource'])) { $ds = $el['attributes']['datasource']; unset($el['attributes']['datasource']); $element->setDatasource(new $ds['name']($ds)); } // patForms will choke when we try to set attributes that // don't exist for an element type. So we'll have to ask. foreach ($el['attributes'] as $name => $value) { if ($element->hasAttribute($name)) { $element->setAttribute($name, $value); } } if (isset($el['rules'])) { foreach ($el['rules'] as $rule) { $element->addRule(new $rule['type']($rule)); } } $form->addElement($element); } if (!is_null($object)) { $form->setValues($object->toArray()); } if ($definition->autoValidate) { $form->setAutoValidate($definition->autoValidate); } return $form; }
/** * Create a form from a propel instance * * @access public * @param mixed $object An instance of a Propel object * @param array $options Any options the creator may need * @return object $form The patForms object, or a patError object on failure. */ function &create($object, $options = array()) { // Propel stuff $propel_peer = $object->getPeer(); $propel_mapBuilder = $propel_peer->getMapBuilder(); // Not sure if we're gonna need this one $propel_tablename = constant(get_class($propel_peer) . '::TABLE_NAME'); $propel_tableMap = $propel_mapBuilder->getDatabaseMap()->getTable($propel_tablename); // The form $form =& patForms::createForm(null, array('name' => 'patForms_Creator_Form')); $propel_cols = $propel_tableMap->getColumns(); foreach ($propel_cols as $propel_colname => $propel_col) { // phpName can be altered by editing the schema.xml, // thus I think, we should lowercase()/ucfirst() this $propel_colname = strtolower($propel_colname); $el_displayname = ucFirst($propel_colname); // this could be omitted of course, but I think, it's a // convenient way to get more safe request properties $el_name = $propel_tablename . '[' . $propel_colname . ']'; $el_attr = array('edit' => 'yes', 'title' => $el_displayname, 'label' => $el_displayname, 'name' => $el_name, 'description' => $el_displayname); //// Obsolete ? // Parse column info to element type info //$type_info = $this->parseTypeInfoFromColumn($propel_col); // Merge extra element attributes //$el_attr = array_merge( $el_attr, $type_info['attributes'] ); // Is the element required ? Can we retrieve this info from the Column object ? $el_attr['required'] = 'yes'; // Value: for now we use default to set the value. Is there a better (more correct) way to do this ? $el_attr['default'] = $object->{'get' . $propel_col->getPhpName()}(); if ($propel_col->isPrimaryKey()) { $el_type = 'hidden'; } else { $el_type = self::$creoleTypeMapping[$propel_col->getCreoleType()]; } $el =& $form->createElement($el_name, $el_type, null); // patForms will choke when we try to set attributes // that don't match the element type. So we'll ask. foreach ($el_attr as $name => $value) { if ($el->hasAttribute($name)) { $el->setAttribute($name, $value); } } $form->addElement($el); } return $form; }
*/ require_once 'patForms.php'; /** * patErrorManager class */ require_once 'patErrorManager.php'; // create the creator :) $creator =& patForms::createCreator('Propel'); // create the form object from the given propel Object class instance require_once 'model/general/UserProfile.php'; $userProfile = UserProfilePeer::retrieveByPK(1); $form =& $creator->create($userProfile); //$wikipage = WikipagePeer::retrieveByPK('wiki'); //$form =& $creator->create($wikipage); // create the needed renderer $renderer =& patForms::createRenderer("Array"); // set the renderer $form->setRenderer($renderer); // use auto-validation $form->setAutoValidate('save'); // serialize the elements $elements = $form->renderForm(); // ERROR DISPLAY ------------------------------------------------------ if ($form->isSubmitted()) { displayErrors($form); // see patExampleGen/customFunctions.php } // DISPLAY FORM ------------------------------------------------------ displayForm($form, $elements); // see patExampleGen/customFunctions.php /**
/** * set the locale, this is needed to update the rule * translations, that have been passed to the container * element * * @access public * @param string new locale * @return boolean */ function setLocale($locale) { // rules do not store locale information if (!patForms::isCustomLocale($locale)) { return true; } $errorMessages = patForms::getCustomLocale($locale, 'Rule::' . $this->getRuleName()); if (is_array($errorMessages)) { $this->validatorErrorCodes[$locale] = $errorMessages; } $this->container->addValidatorErrorCodes($this->validatorErrorCodes, $this->errorOffset); return true; }
/** * Creates a new patForms_Creator object * * @static * @access public * @return object $creator The creator object, or a patError object on failure */ function createCreator($type) { return patForms::_createModule('Creator', $type); }
/** * Template method that applies rules and calls the elements * validation method * * @final * @access public * @return bool $success True on success, false otherwise */ function validate() { // apply locale, if the current locale is a custom locale if (patForms::isCustomLocale($this->locale)) { $cnt = count($this->_rules); for ($i = 0; $i < $cnt; $i++) { $this->_rules[$i]['rule']->setLocale($this->locale); } } /** * validate custom rules */ if (!$this->_applyRules(PATFORMS_RULE_BEFORE_VALIDATION)) { $this->_announce('status', 'error'); return false; } /** * the the unfiltered value */ $value = $this->getValue(false); $valid = $this->validateElement($value); if ($valid === false) { $this->_announce('status', 'error'); return false; } /** * validate custom rules */ if (!$this->_applyRules(PATFORMS_RULE_AFTER_VALIDATION)) { $this->_announce('status', 'error'); return false; } $this->_announce('status', 'validated'); return true; }
// uncomment this to edit an existing record $pk = array('Id' => 2); /** * the rest should work out of the box if you don't have any unusal * types in your database schema.xml (strings, int etc. should work) */ require_once 'bookstore/' . $classname . '.php'; Propel::init($propelConfFilename); // create a form definition $definition = patForms_Definition_Propel::create(array('name' => $classname, 'filename' => $path . '/form.' . $classname . '.xml')); // create a storage $storage = patForms::createStorage('Propel'); $storage->setStorageLocation($classname . 'peer'); // create a form $form =& patForms::createCreator('Definition')->create($definition); $form->setRenderer(patForms::createRenderer('Array')); $form->setStorage($storage); if (isset($pk)) { $form->setValues($pk); } // render it to a patTemplate (could be done by other template engines) $tpl = new patTemplate(); $tpl->setRoot($path); $tpl->readTemplatesFromInput('form.dynamic.tpl'); $tpl->addVar('page', 'title', 'Bookstore party'); $tpl->addVar('form', 'start', $form->serializeStart()); $tpl->addVar('form', 'end', $form->serializeEnd()); $tpl->addRows('elements', $form->renderForm()); // this should be possible to be done in a more elegant way if ($errors = $form->getValidationErrors()) { foreach ($errors as $field => $error) {