Ejemplo n.º 1
0
 /**
  * Get form for model prefilled with values for specified instance.
  *
  * @param string $modelClass
  * @param mixed $instance
  * @param array $options
  * @return Curry_Form_ModelForm
  */
 public function getEditForm($modelClass, $instance = null, $options = array())
 {
     $options = array_merge(array('action' => (string) url('', $_GET), 'method' => 'post', 'class' => isAjax() ? 'dialog-form' : ''), $options);
     $form = new \Curry_Form_ModelForm($modelClass, $options);
     if ($instance !== null) {
         $form->fillForm($instance);
     }
     $form->addElement('submit', 'save', array('label' => 'Save'));
     return $form;
 }
Ejemplo n.º 2
0
 /**
  * Get form from model.
  * 
  * @param BaseObject $row
  * @return Curry_Form_ModelForm
  */
 public static function getRowForm(BaseObject $row)
 {
     $form = new Curry_Form_ModelForm($_GET['table'], array('ignorePks' => false, 'ignoreFks' => false, 'action' => url('', array("module", "view", "table", "pk")), 'method' => 'post', 'class' => 'dialog-form'));
     $form->fillForm($row);
     $displayGroup = 1;
     foreach ($form->getElementColumns() as $column) {
         $element = $form->getElement(strtolower($column->getName()));
         if (!$element) {
             continue;
         }
         // Remove column if it's auto generated
         if ($row->isNew() && $column->isPrimaryKey() && $column->getTable()->isUseIdGenerator()) {
             $form->removeElement($element->getName());
             continue;
         }
         // Make sure we have an option for foreign keys
         if ($column->isForeignKey() && !$row->isNew()) {
             $related = $element->getMultioptions();
             if (!array_key_exists($element->getValue(), $related)) {
                 $related[$element->getValue()] = $element->getValue();
                 $element->setMultioptions($related);
             }
         }
         // Add null checkbox
         if (!$column->isNotNull()) {
             $isNull = $element->getValue() === null;
             if (!$column->isNotNull()) {
                 $name = $element->getName() . '__null__';
                 $form->addElement('checkbox', $name, array('label' => 'Null', 'title' => 'Is Null?', 'class' => 'trigger-change', 'onchange' => "document.getElementById('" . $element->getId() . "').disabled = this.checked;", 'value' => $isNull));
                 $form->addDisplayGroup(array($element->getName(), $name), 'displayGroup' . $displayGroup++, array('class' => 'horizontal-group', 'legend' => $element->getLabel()));
             }
         }
     }
     $form->addElement('submit', 'save', array('label' => $row->isNew() ? 'Insert' : 'Save'));
     return $form;
 }