BsFormHelper

create()

Create a bootstrap form (horizontal form by default).

<?php echo 
$this->BsForm->create('Post', array('action' => 'add'));
?>

<form action="/bbcorg/posts/add" role="form" class="form-horizontal" id="PostAddForm" method="post" accept-charset="utf-8">
   <div style="display:none;">
      <input type="hidden" name="_method" value="POST"/>
   </div>

Parameters

  • [mixed] model -The model name for which the form is being defined. (default = null)
  • [array] options - An array of html attributes and options. (default = array())

input()

Create a bootstrap input.

<?php echo 
$this->BsForm->input('User.name', array('label' => 'Full Name', 'state' => 'success', 'help' => 'Type your full name here'));
?>

<div class="form-group has-success">
   <label for="UserName" class="control-label col-md-3">Full Name</label>
   <div class="col-md-9">
      <input name="data[User][name]" class="form-control" type="text" id="UserName"/>
      <span class="help-block">Type your full name here</span>
   </div>
</div>

Parameters

  • [string] fieldName - This should be "Modelname.fieldname"
  • [array] options - Each type of input takes different options. (default = array())

    There are options by default and new options added :

    • [Default] before - Add a 'form-group' div
    • [Default] between - Open the right column of the form
    • [Default] after - Close div tags
    • [Default] div - Set to 'false'
    • [Default] class - Add 'form-control'
    • [Default] label - Add a 'control-label' class and if the form is horizontal, put the label into the left column of it

    • [Added] state - Change the state of the input to 'error', 'warning' or 'success'
    • [Added] help - Add a message under the input to give more informations

inputGroup()

Create a bootstrap input group.

<?php echo 
$this->BsForm->inputGroup('User.email', array('content' => '@', 'class' => 'addon-mail'), array('label' => 'E-mail'));
?>

<div class="form-group">
  <label for="UserEmail" class="control-label col-md-3">E-mail</label>
  <div class="col-md-9">
     <div class="input-group">
        <span class="addon-mail input-group-addon">@</span>
        <input name="data[User][email]" class="form-control" type="email" id="UserEmail"/>
     </div>
  </div>
</div>

Parameters

  • [string] fieldName - This should be "Modelname.fieldname"
  • [array] addonOptions - Options for the addon. (default = array())
    • 'content' - The addon content.
    • 'side' - Which side the addon will be. (Left by default)
    • 'class' - Add HTML class attribute.
    • 'type' - Change the type of the addon : button, submit or image
    • 'state' - If it's a button or submit, change the state (success, danger, warning, etc).
    • 'src' - If it's an image, change the URL.
  • [array] options - Some options of the BsFormHelper::input() . (default = array())

checkbox()

Create a bootstrap select field. It can be used to generate multiples checkbox too.

<?php echo 
$this->BsForm->checkbox('User.notifications', array('label' => 'Do you want notifications ?', 'checked' => 'checked'));
?>

<div class="form-group">
  <div class="col-md-offset-3 col-md-9">
     <div class="checkbox">
        <label for="UserNotifications">
           <input type="hidden" name="data[User][notifications]" id="UserNotifications_" value="0"/>
           <input type="checkbox" name="data[User][notifications]"  checked="checked" value="1" id="UserNotifications"/> Do you want notifications ?
        </label>
     </div>
  </div>
</div>

Parameters

  • [string] fieldName - This should be "Modelname.fieldname"
  • [array] options - Differents options for the input. (default = array())

    There are options by default and new options added :

    • [Default] div - Set to 'false'.
    • [Default] class - Add 'form-control'.

    • [Added] help - Add a message under the input to give more informations.
    • [Added] labelClass - Add a class to the label element.
    • [Added] labelHelp - Add a message under the label to give more informations.

select()

Create a bootstrap checkbox field. Only for a single checkbox. For multiples checkbox, we recommend to use BsFormHelper::select().

<?php 
$animals = array(
  'dog' => 'Dog',
  'cat' => 'Cat',
  'rabbit' => 'Rabbit'
);
echo $this->BsForm->select('User.animal', $animals, array('label' => "What's your favorite animal ?", 'help' => 'Only one can be chosen'));
?>

<div class="form-group">
   <label class="control-label col-md-3">What's your favorite animal ?</label>
   <div class="col-md-9">
      <select name="data[User][animal]" class="form-control" id="UserAnimal">
         <option value="dog">Dog</option>
         <option value="cat">Cat</option>
         <option value="rabbit">Rabbit</option>
      </select>
      <span class="help-block">Only one can be chosen</span>
   </div>
</div>


<?php 
$animals = array(
  'dog' => 'Dog',
  'cat' => 'Cat',
  'rabbit' => 'Rabbit'
);
echo $this->BsForm->select('User.animal', $animals, array('label' => "What's your favorite animal ?", 'multiple' => 'checkbox'))
?>

<div class="form-group">
   <label class="control-label col-md-3">What's your favorite animal ?</label>
   <div class="col-md-9">
      <input type="hidden" name="data[User][animal]" value="" id="UserAnimal"/>
      <div class="checkbox">
         <label for="UserAnimalDog">
            <input type="checkbox" name="data[User][animal][]" value="dog" id="UserAnimalDog" />
            Dog
         </label>
      </div>
      <div class="checkbox">
         <label for="UserAnimalCat">
            <input type="checkbox" name="data[User][animal][]" value="cat" id="UserAnimalCat" />
            Cat
         </label>
      </div>
      <div class="checkbox">
         <label for="UserAnimalRabbit">
            <input type="checkbox" name="data[User][animal][]" value="rabbit" id="UserAnimalRabbit" />
            Rabbit
         </label>
      </div>
   </div>
</div>

Parameters

  • [string] fieldName - This should be "Modelname.fieldname"
  • [array] options - Options in the field.
  • [array] attributes - Differents attributes for the input. (default = array())

    There are options added :

    • [Added] help - Add a message under the input to give more informations.
    • [Added] inline - To align multiple checkbox.
    • [Added] label - Add a label.

chosen()

Create a select with Chosen plugin attached.

            <?php 
$tab2 = 
  array(
    'group 1' => array(
    '1' => 'Choice 1',
    '2' => 'Choice 2'
  ),
  'group 2' => array(
    '3' => 'Choice 3',
    '4' => 'Choice 4'
  )     
);

echo 
$this->BsForm->chosen(
  'Tag.Tag', 
  $tab2, 
  array(
    'label'            => 'Chosen 1', 
    'data-placeholder' => 'Cliquez pour selectionner les valeurs recherchées',
    'default'          => array('2', '4'),
    'disabled'         => array('1'),
    'multiple'         => true,
  )  
);
?>
            
          
            
<div class="form-group">
  <label for="TagTag" class="control-label col-md-3">Chosen 1</label>
  <div class="col-md-9">
    <input type="hidden" name="data[Tag][Tag]" value="" id="TagTag_"/>
    <select name="data[Tag][Tag][]" class="checkbox chosen-Tag_Tag" data-placeholder="Cliquez pour selectionner les valeurs recherchées" multiple="multiple" id="TagTag">
      <optgroup label="group 1">
        <option value="1" disabled="disabled">Choice 1</option>
        <option value="2" selected="selected">Choice 2</option>
      </optgroup>
        <optgroup label="group 2">
        <option value="3">Choice 3</option>
        <option value="4" selected="selected">Choice 4</option>
      </optgroup>
    </select>
  </div>
</div>
            
          

Parameters

  • [string] fieldName - This should be "Modelname.fieldname"
  • [array] Select content - Item of the select (default = array()) [array] Select option - Options of the select HTML tag
    • 'data-placeholder' - Placeholder on multiselect.
    • 'label' - Associated label
    • 'class' - Add HTML class attribute.
    • 'default' - What item are select by default
    • 'multiple' - True or not (by default)
    • 'disabled' - What element cannot be selected
  • [array] Chosen option - Some options for chosen JS

radio()

Create a bootstrap radio field.

<?php 
$statut = array(
  0 => 'No',
  1 => 'Yes'
);
echo $this->BsForm->radio('User.married', $state, array('label' => 'Are you married ?'));
?>

<div class="form-group">
   <label class="control-label col-md-3">Are you married ?</label>
   <div class="col-md-9">
      <div class="radio">
         <label>
            <input type="hidden" name="data[User][married]" id="UserMarried_" value=""/>
            <input type="radio" name="data[User][married]" id="UserMarried0" value="0" />
            <label for="UserMarried0">No</label>
         </label></div><div class="radio">
         <label>
            <input type="radio" name="data[User][married]" id="UserMarried1" value="1" />
            <label for="UserMarried1">Yes</label>
         </label>
      </div>
   </div>
</div>

Parameters

  • [string] fieldName - This should be "Modelname.fieldname"
  • [array] options - Options in the field.
  • [array] attributes - Differents attributes for the input. (default = array())

    There are options by default and new options added :

    • [Default] label - Set to 'false'.
    • [Default] legend - Set to 'false'.

    • [Added] help - Add a message under radio buttons to give more informations.
    • [Added] inline - To align all the radio buttons. For inline form.

datepicker()

Create a bootstrap datepicker. Needs to have the variable dpLoad of BsHelper to true and before reporting functions BsHelper::css() and BsHelper::js().

<?php 
// There need these functions in the layout before
// $this->Bs->dpLoad = true;
// echo $this->Bs->css();
// echo $this->Bs->js();

echo $this->BsForm->datepicker('User.anniversary', array('orientation' => 'top right'), array('label' => 'Your date of birth'));
?>

<div class="dp-container">
   <div class="form-group">
      <label for="UserAnniversary" class="control-label col-md-3">Your date of birth</label>
      <div class="col-md-9">
         <input name="data[User][anniversary]" class="form-control" type="text" id="UserAnniversary"/>
      </div>
   </div>
   <input type="hidden" name="data[User][anniversary]" id="alt_dp" class="form-control"/>
</div>
<script>
$('.dp-container input').datepicker({orientation : "top right",format : "dd/mm/yyyy",language : "fr",}).on('changeDate', function() {
   var date = $('#UserAnniversary').datepicker('getDate');
   date.setHours(0, -date.getTimezoneOffset(), 0, 0);
   date = date.toISOString().slice(0,19).replace('T', " ");
   $('#alt_dp').attr('value', date);
});
</script>

Parameters

  • [string] fieldName - This should be "Modelname.fieldname"
  • [array] optionsDP - Options of the datepicker (default = array()). The full documentation is available here : Full Documentation

  • [array] options - Differents options for the input, same as BsForm::input(). (default = array())

submit()

Create a bootstrap button to submit the form.

<?php echo 
$this->BsForm->submit('Finish');
?>

<div class="form-group">
   <div class="col-md-offset-3 col-md-9">
      <input  class="btn btn-success" type="submit" value="Finish"/>
   </div>
</div>

Parameters

  • [string] caption - The label appearing on the button.
  • [array] options - Different options of the input. (default = array())

    There are options by default :

    • [Default] div - Set to 'false'
    • [Default] class - Set into a success button instead of a default button
    • [Default] label - Set to 'false'

end()

Closes an HTML form, cleans up values set by BsForm::create(), and writes hidden input fields where appropriate.

<?php echo 
$this->BsForm->end();
?>

</form>

Parameters

  • [array] options - A string will use $options as the value of button. (default = null)
  • [array] secureAttributes - Will be passed as html attributes into the hidden input elements generated for the Security Component. (default = array())

getLeft()

Return the width of the left column of the form. (default : 3)

<?php echo 
$this->BsForm->getLeft();
?>


getRight()

Return the width of the right column of the form. (default : 9)

<?php echo 
$this->BsForm->getRight();
?>


setLeft()

Edit the width of the left column of the form.

<?php echo 
$this->BsForm->setLeft(5);
?>

Parameters

  • [int] val - Width of the column. (bootstrap size)

setRight()

Edit the width of the right column of the form.

<?php echo 
$this->BsForm->setRight(7);
?>

Parameters

  • [int] val - Width of the column. (bootstrap size)

setFormType()

Edit the type of the form.

<?php echo 
$this->BsForm->setFormType('inline');
?>

Parameters

  • [string] val - Type of the form. (inline, horizontal)