Skip to content

Instantly share code, notes, and snippets.

@stvvt
Forked from slywalker/bootstrap_form.php
Created October 12, 2011 07:44
Show Gist options
  • Save stvvt/1280550 to your computer and use it in GitHub Desktop.
Save stvvt/1280550 to your computer and use it in GitHub Desktop.

Revisions

  1. @slywalker slywalker revised this gist Oct 12, 2011. 1 changed file with 30 additions and 0 deletions.
    30 changes: 30 additions & 0 deletions bootstrap_form.php
    Original file line number Diff line number Diff line change
    @@ -25,6 +25,7 @@ public function input($name, $options = array()) {
    $options['label'] = false;
    $divOptions = $options['div'];
    $options['div'] = false;
    $options['legend'] = false;
    $options['error'] = array(
    'wrap' => 'span',
    'class' => 'help-block',
    @@ -42,6 +43,9 @@ public function input($name, $options = array()) {
    if (!empty($options['multiple']) && $options['multiple'] === 'checkbox') {
    $form = $options['after'] .$this->_multipleCheckbox($form, $options);
    }
    elseif ($options['type'] === 'radio') {
    $form = $this->_radio($form, $options);
    }
    $out[] = $this->Html->div($divOptions['class'], $form, $divOptions);

    $errorClass = '';
    @@ -58,6 +62,32 @@ protected function _prepend($before, $options) {
    return $options;
    }

    protected function _radio($out, $options) {
    $default = array(
    'ul' => array('class' => 'inputs-list'),
    'li' => array(),
    );
    $options = Set::merge($default, $options);

    if (!preg_match_all('/(<input type="radio"[^>]+>)([^<]*)/m', $out, $matches)) {
    return $out;
    }
    if (!preg_match('/<input type="hidden"[^>]+>/m', $out, $match)) {
    return $out;
    }

    $hidden = $match[0];
    $lines = array();
    foreach ($matches[0] as $key => $value) {
    $line = $matches[1][$key] . '&nbsp;' . $this->Html->tag('span', $matches[2][$key]);
    $line = $this->Html->tag('label', $line);
    $lines[] = $this->Html->tag('li', $line, $options['li']);
    }
    $out = $hidden;
    $out .= $this->Html->tag('ul', implode("\n", $lines), $options['ul']);
    return $out;
    }

    protected function _multipleCheckbox($out, $options) {
    $default = array(
    'ul' => array('class' => 'inputs-list'),
  2. @slywalker slywalker revised this gist Oct 6, 2011. 1 changed file with 10 additions and 4 deletions.
    14 changes: 10 additions & 4 deletions bootstrap_form.php
    Original file line number Diff line number Diff line change
    @@ -40,7 +40,7 @@ public function input($name, $options = array()) {

    $form = $this->Form->input($name, $options);
    if (!empty($options['multiple']) && $options['multiple'] === 'checkbox') {
    $form = $options['after'] . $this->_multipleCheckbox($form);
    $form = $options['after'] .$this->_multipleCheckbox($form, $options);
    }
    $out[] = $this->Html->div($divOptions['class'], $form, $divOptions);

    @@ -58,7 +58,13 @@ protected function _prepend($before, $options) {
    return $options;
    }

    protected function _multipleCheckbox($out) {
    protected function _multipleCheckbox($out, $options) {
    $default = array(
    'ul' => array('class' => 'inputs-list'),
    'li' => array(),
    );
    $options = Set::merge($default, $options);

    if (!preg_match_all('/<div[^>]+>(<input type="checkbox"[^>]+>)(<label[^>]+>)([^<]*)(<\/label>)<\/div>/m', $out, $matches)) {
    return $out;
    }
    @@ -70,10 +76,10 @@ protected function _multipleCheckbox($out) {
    foreach ($matches[0] as $key => $value) {
    $line = $matches[2][$key] . $matches[1][$key] . '&nbsp;';
    $line .= $this->Html->tag('span', $matches[3][$key]) . $matches[4][$key];
    $lines[] = $this->Html->tag('li', $line);
    $lines[] = $this->Html->tag('li', $line, $options['li']);
    }
    $out = $hidden;
    $out .= $this->Html->tag('ul', implode("\n", $lines), array('class' => 'inputs-list'));
    $out .= $this->Html->tag('ul', implode("\n", $lines), $options['ul']);
    return $out;
    }
    }
  3. @slywalker slywalker revised this gist Oct 6, 2011. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion bootstrap_form.php
    Original file line number Diff line number Diff line change
    @@ -40,7 +40,7 @@ public function input($name, $options = array()) {

    $form = $this->Form->input($name, $options);
    if (!empty($options['multiple']) && $options['multiple'] === 'checkbox') {
    $form = $this->_multipleCheckbox($form);
    $form = $options['after'] . $this->_multipleCheckbox($form);
    }
    $out[] = $this->Html->div($divOptions['class'], $form, $divOptions);

  4. @slywalker slywalker revised this gist Oct 6, 2011. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion bootstrap_form.php
    Original file line number Diff line number Diff line change
    @@ -18,7 +18,9 @@ public function input($name, $options = array()) {
    $out = array();

    $label = $options['label'];
    $out[] = $this->Form->label($name, $label);
    if ($label !== false) {
    $out[] = $this->Form->label($name, $label);
    }

    $options['label'] = false;
    $divOptions = $options['div'];
  5. @slywalker slywalker created this gist Oct 6, 2011.
    77 changes: 77 additions & 0 deletions bootstrap_form.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,77 @@
    <?php
    class BootstrapFormHelper extends AppHelper {

    public $helpers = array('Html', 'Form');

    public function input($name, $options = array()) {
    $default = array(
    'type' => null,
    'label' => null,
    'before' => null, // to convert .input-prepend
    'after' => null, // to convert .help-inline
    'div' => array(
    'class' => 'input',
    ),
    );
    $options = Set::merge($default, $options);

    $out = array();

    $label = $options['label'];
    $out[] = $this->Form->label($name, $label);

    $options['label'] = false;
    $divOptions = $options['div'];
    $options['div'] = false;
    $options['error'] = array(
    'wrap' => 'span',
    'class' => 'help-block',
    );
    if ($options['after']) {
    $options['after'] = $this->Html->tag('span', $options['after'], array(
    'class' => 'help-inline',
    ));
    }
    if ($options['before']) {
    $options = $this->_prepend($options['before'], $options);
    }

    $form = $this->Form->input($name, $options);
    if (!empty($options['multiple']) && $options['multiple'] === 'checkbox') {
    $form = $this->_multipleCheckbox($form);
    }
    $out[] = $this->Html->div($divOptions['class'], $form, $divOptions);

    $errorClass = '';
    if ($this->Form->error($name)) {
    $errorClass = ' error';
    }
    return $this->Html->div('clearfix' . $errorClass, implode("\n", $out));
    }

    protected function _prepend($before, $options) {
    $before = $this->Html->tag('span', $before, array('class' => 'add-on'));
    $options['before'] = '<div class="input-prepend">' . $before;
    $options['after'] .= '</div>';
    return $options;
    }

    protected function _multipleCheckbox($out) {
    if (!preg_match_all('/<div[^>]+>(<input type="checkbox"[^>]+>)(<label[^>]+>)([^<]*)(<\/label>)<\/div>/m', $out, $matches)) {
    return $out;
    }
    if (!preg_match('/<input type="hidden"[^>]+>/m', $out, $match)) {
    return $out;
    }
    $hidden = $match[0];
    $lines = array();
    foreach ($matches[0] as $key => $value) {
    $line = $matches[2][$key] . $matches[1][$key] . '&nbsp;';
    $line .= $this->Html->tag('span', $matches[3][$key]) . $matches[4][$key];
    $lines[] = $this->Html->tag('li', $line);
    }
    $out = $hidden;
    $out .= $this->Html->tag('ul', implode("\n", $lines), array('class' => 'inputs-list'));
    return $out;
    }
    }