Example #1
0
 /**
  * Render the form as HTML
  */
 public function render()
 {
     // Create the view
     $this->layout($this->layout);
     // Separate hidden and displayed fields
     $fields = array();
     $hidden = array();
     // Are we using a twitter bootstrap layout?
     $bootstrap = strrpos($this->layout->view, 'bootstrap') !== false;
     // This is an extra iteration of the fields, but oh well
     $this->assign_values();
     foreach ($this->fields as $field) {
         // Add bootstrap classes if appropriate
         if ($bootstrap && $field->label) {
             $field->label->add_class('control-label');
         }
         // Is it hidden?
         // @todo: this prevents hidden fields from being
         // usable inside multi-field, but I think that's okay
         if ($field->is_hidden()) {
             $hidden[] = $field;
         } else {
             if (!isset($field->label)) {
                 $field->label(ucwords(str_replace('_', ' ', $field->name)));
                 // Retroactively apply required class to label
                 if ($field->is_required()) {
                     $field->label->add_class('required');
                 }
             }
             $fields[] = $field;
         }
     }
     // Make the buttons
     $buttons = array();
     if (!is_array($this->buttons)) {
         $this->buttons = array();
     } elseif (!count($this->buttons)) {
         $this->buttons = array(Form_Button::make('Cancel')->attr('type', 'button')->attr('class', 'btn'), Form_Button::make('Submit')->attr('type', 'submit')->attr('class', 'btn btn-primary'));
     }
     foreach ($this->buttons as $key => $val) {
         if (is_int($key) && is_array($val)) {
             $buttons[] = Form_Button::make()->parse_config($val);
         } elseif (is_int($key) && is_a($val, 'Squi\\Form_Button')) {
             $buttons[] = $val;
         } elseif (is_int($key)) {
             $buttons[] = Form_Button::make($val)->attr('class', 'btn');
         } elseif (is_string($key) && is_array($val)) {
             $buttons[] = Form_Button::make($key)->parse_config($val);
         } elseif (is_string($key) && is_string($val)) {
             $buttons[] = Form_Button::make($key)->attr('name', $val)->attr('class', 'btn');
         }
     }
     return $this->layout->with('form', $this)->with('fields', $fields)->with('hidden', $hidden)->with('buttons', $buttons)->render();
 }