How to modify wrapper div error class when using CakePHP with Bootstrap

前端 未结 7 1862
我寻月下人不归
我寻月下人不归 2021-02-08 13:47

I\'m using Bootstrap 3.0RC1 with CakePHP 2.3.6. Trying to take advantage of those beautiful looking classes like has-error and has-w

7条回答
  •  说谎
    说谎 (楼主)
    2021-02-08 14:28

    If you introspect FormHelper, you'll find in this line "ugly" code that do error magic.

    Since original authors did not leave any chance to do this by configuration, I'm suggesting you writing own BootstrapFormHelper, and override input function, by changing that single line.

    Here is snippet:

    //inside public function input($fieldName, $options = array())
    
    $out['error'] = null;
    if ($type !== 'hidden' && $error !== false) {
        $errMsg = $this->error($fieldName, $error);
        if ($errMsg) {
            $divOptions = $this->addClass($divOptions, 'has-error'); //old string value was 'error'
            if ($errorMessage) {
                $out['error'] = $errMsg;
            }
        }
    }
    

    Since I'm already using custom BootstrapFormHelper, here is link to full gist.

    Just copy file to app\View\Helper, and add to ALL your Controllers this line:

    public $helpers = array(
      'Form' => array('className' => 'BootstrapForm')
    );
    

    assuming that you've saved gist as BootstrapFormHelper.php.

提交回复
热议问题