phphtmlformsquickform

HTML_QuickForm2 Template for date element


<select id="0-d-0" name="d">
<option value="1">01</option>
...
<option value="31">31</option>
</select>
<select id="1-M-0" name="M">
<option value="1">Jan</option>
...
<option value="12">Dec</option>
</select>
... So on and so on

I'm sure you reading this will be familiar with the element returned from HTML_QuickForm2.

It drove me crazy.

Nigh a post nor manual that could tell me how to change it to the more useful date element...

<input type="date" name="date" value="TODAY" />

Which the browser would display very nicely with a calendar and easy UI.

I figured a template redefinition was in order, so I'll show you next how I did it..

I ask anyone to show me how they did it??


Solution

  • Oh QuickForms, You Beauty.

    $form = HTML_QuickForm2_Controller( 'realDate' );
    
    $form = new HTML_FormsFactory(
                 new HTML_QuickForm2(
                                     'realDate',
                                      NULL,['name'=>'date','action'=>''], NULL 
                                    )
                                 );
    
    $date = "2016-05-18";    
    
    $form->addHandler( 'process' , new HTMLProcess() );
    $form->addHandler( 'display' , new HTMLDisplay($date) ); // The HACK.
    $form->run();
    

    And now some Class definitions for the Controller and so forth...

    //
    class HTMLFormsFactory extends HTML_QuickForm2_Controller_Page{
    
     public function __construct(){
      parent::__construct( $form );
      $this->HTML = $this->form;
     } 
    
     public function populateForm(){
      $this->HTML->addDate( 'dater', [ 'id'=>'dater' ] );
     }
    }
    
    //
    class HTMLProcess implements HTML_QuickForm2_Controller_Action{
    
     public function perform( HTML_QuickForm2_Controller_Page $page, $name ){
      var_dump( $page->getController()->getValue() );
     }
    }
    
    //
    class HTMLDisplay extends HTML_QuickForm2_Controller_Action_Display{
    
     public function __construct($date){ $this->_date = $date; } // That HACK
    
     public function renderForm( HTML_QuickForm2 $form ){
      $renderer = HTML_QuickForm2_Renderer::factory( 'default' );
    
      // Make a new template and drop the date value in..
      // make sure the id to the form handler is the 
      // same as the dateElement added in populateForm 
      // so it is present in the _POST params after submit..
      $renderer->setTemplateForId(
          'dater', '<div class="row"><p class="label"><label 
                                                       style="padding-right:1em;">
                    Date: </label></p><br />
                    <input id="dater" type="date" name="dater" value="'.$this->_date.
                    '" /></div>');
    
      echo $form->render( $renderer );
     }
    }