Beispiel #1
0
 /**
  * Returns an entire form with input tags and everything for a specified Active Record object. Example
  * (post is a new record that has a title using VARCHAR and a body using TEXT):
  *   $active_record->form('post'); =>
  *     <form action='/post/create' method='post'>
  *       <p>
  *         <label for="post_title">Title</label><br />
  *         <input id="post_title" name="post[title]" size="30" type="text" value="Hello World" />
  *       </p>
  *       <p>
  *         <label for="post_body">Body</label><br />
  *         <textarea cols="40" id="post_body" name="post[body]" rows="20">
  *           Back to the hill and over it again!
  *         </textarea>
  *       </p>
  *       <input type='submit' value='Create' />
  *     </form>
  * 
  * It's possible to specialize the form builder by using a different action name and by supplying another
  * block renderer that will be evaled by PHP. 
  * Example (entry is a new record that has a message attribute using VARCHAR):
  *
  *   $active_record->form('entry', array('action'=>'sign','input_block' => 
  *  '<p><?=AkInflector::humanize($column)?>: <?=$this->input($record_name, $column)?></p><br />'
  *   );
  *
  *     <form action='/post/sign' method='post'>
  *       Message:
  *       <input id="post_title" name="post[title]" size="30" type="text" value="Hello World" /><br />
  *       <input type='submit' value='Sign' />
  *     </form>
  */
 function form($record_name, $options = array())
 {
     $record =& $this->_controller->{$record_name};
     $options['action'] = !empty($options['action']) ? $options['action'] : ($record->isNewRecord() ? 'create' : 'update');
     $action = $this->_controller->urlFor(array('action' => $options['action'], 'id' => $record->getId()));
     $submit_value = !empty($options['submit_value']) ? $options['submit_value'] : strtoupper(preg_replace('/[^\\w]/', '', $options['action']));
     $contents = '';
     $contents .= $record->isNewRecord() ? '' : $this->_controller->form_helper->hidden_field($record_name, 'id');
     $contents .= $this->all_input_tags($record, $record_name, $options);
     $contents .= FormTagHelper::submit_tag(Ak::t($submit_value, array(), 'helpers/active_record'));
     return TagHelper::content_tag('form', $contents, array('action' => $action, 'method' => 'post', 'enctype' => !empty($options['multipart']) ? 'multipart/form-data' : null));
 }
Beispiel #2
0
 public function time_zone_select_tag($field, $selected = null, $options = array())
 {
     $options = FormTagHelper::set_field_id_and_name($field, $options);
     return "<select" . $this->options_to_s($options) . ">" . $this->time_zone_options_for_select($selected) . "</select>";
 }
Beispiel #3
0
 public function test_for_form_tag_helpers()
 {
     $Controller = new MockAkActionController($this);
     $Controller->setReturnValue('urlFor', '/url/for/test');
     $form_tag = new FormTagHelper();
     $form_tag->setController($Controller);
     $this->assertEqual($form_tag->form_tag(), '<form action="/url/for/test" method="post">');
     $this->assertEqual($form_tag->form_tag(array(), array('method' => 'get')), '<form action="/url/for/test" method="get">');
     $this->assertEqual($form_tag->form_tag(array(), array('multipart' => true)), '<form action="/url/for/test" enctype="multipart/form-data" method="post">');
     $this->assertEqual($form_tag->end_form_tag(), '</form>');
     $this->assertEqual($form_tag->start_form_tag(), '<form action="/url/for/test" method="post">');
     $this->assertEqual($form_tag->select_tag('person', '<option>Bermi</option>', array('id' => 'bermi')), '<select id="bermi" name="person"><option>Bermi</option></select>');
     $this->assertEqual($form_tag->text_field_tag('person', 'Bermi', array('id' => 'bermi')), '<input id="bermi" name="person" type="text" value="Bermi" />');
     $this->assertEqual($form_tag->text_field_tag('person[1][name]', 'Bermi'), '<input id="person_1_name" name="person[1][name]" type="text" value="Bermi" />');
     $this->assertEqual($form_tag->hidden_field_tag('person', 'Bermi', array('id' => 'bermi')), '<input id="bermi" name="person" type="hidden" value="Bermi" />');
     $this->assertEqual($form_tag->file_field_tag('photo', array('id' => 'pick_photo')), '<input id="pick_photo" name="photo" type="file" />');
     $this->assertEqual($form_tag->password_field_tag('password', '', array('id' => 'pass')), '<input id="pass" name="password" type="password" value="" />');
     $this->assertEqual($form_tag->text_area_tag('address', 'My address', array('id' => 'address_box')), '<textarea id="address_box" name="address">My address</textarea>');
     $this->assertEqual($form_tag->check_box_tag('subscribe', 'subscribed', true), '<input checked="checked" id="subscribe" name="subscribe" type="checkbox" value="subscribed" />');
     $this->assertEqual($form_tag->radio_button_tag('subscribe', 'subscribed', true), '<input checked="checked" id="subscribe" name="subscribe" type="radio" value="subscribed" />');
     $this->assertEqual($form_tag->submit_tag(), '<input name="commit" type="submit" value="Save changes" />');
     $this->assertEqual($form_tag->submit_tag('Commit changes', array('disable_with' => "Wait'Please")), '<input name="commit" onclick="this.disabled=true;this.value=\'Wait\\\'Please\';this.form.submit();" type="submit" value="Commit changes" />');
     /**
      * @todo TEST FOR image_submit_tag($source, $options = array())
      */
 }
Beispiel #4
0
 public function text_field($name, $html_attributes = array())
 {
     $value = strval($this->get_model_data($name));
     return FormTagHelper::text_field_tag($this->get_field_name($name), $value, $html_attributes);
 }
Beispiel #5
0
 public function __toString()
 {
     if (strtolower($this->config['method']) == 'put' || strtolower($this->config['method']) == 'delete') {
         $method = 'POST';
         $extra = FormTagHelper::hidden_field('_method', '_method', $this->config['method']);
     } else {
         $method = $this->config['method'];
     }
     $form_options = array_merge(array('name' => strtolower($this->get_form_name()), 'method' => $method, 'action' => $this->config['path']), $this->tag_options);
     if (isset($this->config['type'])) {
         $form_options['enctype'] = $this->config['type'];
     }
     $return = TagHelper::tag('form', $form_options);
     if (isset($extra)) {
         $return = $return . $extra;
     }
     return $return;
 }
Beispiel #6
0
 public function end()
 {
     return FormTagHelper::close_tag('form');
 }