Thursday, May 16, 2013

Using CakePHP FormHelper with Bootstrap Forms

CakePHP's FormHelper helpt to generate forms when making CakePHP applications. As one might assume, this includes generating input elements, like example:

 <form action="">  
 <fieldset>  
 <div class="control-group required error">  
   <label for="Fieldname" class="control-label">Fieldname</label>  
   <div class="controls">  
     <input name="data[Fieldname]" class="form-error" maxlength="255" type="text" value="" id="Fieldname"/>  
     <span class="help-inline">Error message</span>  
   </div>  
 </div>  
 </fieldset>  
 </form>  

The CakePHP code is:


 <?php echo $this->Form->create('ModelName', array(  
   'class' => 'form-horizontal',  
   'inputDefaults' => array(  
     'format' => array('before', 'label', 'between', 'input', 'error', 'after'),  
     'div' => array('class' => 'control-group'),  
     'label' => array('class' => 'control-label'),  
     'between' => '<div class="controls">',  
     'after' => '</div>',  
     'error' => array('attributes' => array('wrap' => 'span', 'class' => 'help-inline')),  
   )));?>  
 <fieldset>  
 <?php echo $this->Form->input('Fieldname1', array(  
   'label' => array('class' => 'control-label','text'=>'HERE YOU LABEL TEXT')  
   )); ?>  
 <?php echo $this->Form->input('Fieldname2', array(  
   'label' => array('class' => 'control-label','text'=>'HERE YOU LABEL TEXT')  
   )); ?>  
 </fieldset>   
 <?php echo $this->Form->end();?>  

No comments:

Post a Comment