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; }