Commit bc8704c3 by Crisu83

added support for setting htmlOptions for each form element

parent d912b1e8
......@@ -200,13 +200,12 @@ class BootActiveForm extends CActiveForm
* @param CModel $model the data model
* @param string $attribute the attribute
* @param array $htmlOptions additional HTML attributes
* @param array $captchaOptions the captcha options
* @return string the generated row
* @since 0.9.3
*/
public function captchaRow($model, $attribute, $htmlOptions = array(), $captchaOptions = array())
public function captchaRow($model, $attribute, $htmlOptions = array())
{
return $this->inputRow(BootInput::TYPE_CAPTCHA, $model, $attribute, $captchaOptions, $htmlOptions);
return $this->inputRow(BootInput::TYPE_CAPTCHA, $model, $attribute, $htmlOptions);
}
/**
......
......@@ -64,11 +64,10 @@ abstract class BootInput extends CInputWidget
if ($this->type === self::TYPE_UNEDITABLE)
{
$classes = 'uneditable-input';
if (isset($this->htmlOptions['class']))
$this->htmlOptions['class'] .= ' '.$classes;
$this->htmlOptions['class'] .= ' uneditable-input';
else
$this->htmlOptions['class'] = $classes;
$this->htmlOptions['class'] = 'uneditable-input';
}
}
......@@ -139,11 +138,18 @@ abstract class BootInput extends CInputWidget
/**
* Returns the label for the input.
* @param array $htmlOptions additional HTML attributes
* @return string the label
*/
protected function getLabel($htmlOptions = array())
protected function getLabel()
{
if (isset($this->htmlOptions['labelOptions']))
{
$htmlOptions = $this->htmlOptions['labelOptions'];
unset($this->htmlOptions['labelOptions']);
}
else
$htmlOptions = array();
if ($this->label !== false && !in_array($this->type, array('checkbox', 'radio')) && $this->hasModel())
return $this->form->labelEx($this->model, $this->attribute, $htmlOptions);
else if ($this->label !== null)
......@@ -154,24 +160,33 @@ abstract class BootInput extends CInputWidget
/**
* Returns the prepend element for the input.
* @param array $htmlOptions additional HTML attributes
* @return string the element
*/
protected function getPrepend($htmlOptions = array())
protected function getPrepend()
{
if ($this->hasAddOn())
{
$classes = 'add-on';
if (isset($this->htmlOptions['prependOptions']))
{
$htmlOptions = $this->htmlOptions['prependOptions'];
unset($this->htmlOptions['prependOptions']);
}
else
$htmlOptions = array();
if (isset($htmlOptions['class']))
$htmlOptions['class'] .= ' '.$classes;
$htmlOptions['class'] .= ' add-on';
else
$htmlOptions['class'] = $classes;
$htmlOptions['class'] = 'add-on';
$classes = $this->getInputContainerCssClass();
ob_start();
echo '<div class="'.$classes.'">';
if (isset($this->htmlOptions['prepend']))
{
echo CHtml::tag('span', $htmlOptions, $this->htmlOptions['prepend']);
unset($this->htmlOptions['prepend']);
}
return ob_get_clean();
}
else
......@@ -180,22 +195,31 @@ abstract class BootInput extends CInputWidget
/**
* Returns the append element for the input.
* @param array $htmlOptions additional HTML attributes
* @return string the element
*/
protected function getAppend($htmlOptions = array())
protected function getAppend()
{
if ($this->hasAddOn())
{
$classes = 'add-on';
if (isset($this->htmlOptions['appendOptions']))
{
$htmlOptions = $this->htmlOptions['appendOptions'];
unset($this->htmlOptions['appendOptions']);
}
else
$htmlOptions = array();
if (isset($htmlOptions['class']))
$htmlOptions['class'] .= ' '.$classes;
$htmlOptions['class'] .= ' add-on';
else
$htmlOptions['class'] = $classes;
$htmlOptions['class'] = 'add-on';
ob_start();
if (isset($this->htmlOptions['append']))
{
echo CHtml::tag('span', $htmlOptions, $this->htmlOptions['append']);
unset($this->htmlOptions['append']);
}
echo '</div>';
return ob_get_clean();
}
......@@ -216,36 +240,19 @@ abstract class BootInput extends CInputWidget
}
/**
* Returns the input container CSS classes.
* @return string the classes
* Returns the error text for the input.
* @return string the error text
*/
protected function getInputContainerCssClass()
protected function getError()
{
$classes = array();
if (isset($this->htmlOptions['prepend']))
$classes[] = 'input-prepend';
if (isset($this->htmlOptions['append']))
$classes[] = 'input-append';
return implode(' ', $classes);
}
/**
* Returns whether the input has an add-on (prepend and/or append).
* @return boolean the result
*/
protected function hasAddOn()
if (isset($this->htmlOptions['errorOptions']))
{
return isset($this->htmlOptions['prepend']) || isset($this->htmlOptions['append']);
$htmlOptions = $this->htmlOptions['errorOptions'];
unset($this->htmlOptions['errorOptions']);
}
else
$htmlOptions = array();
/**
* Returns the error text for the input.
* @param array $htmlOptions additional HTML attributes
* @return string the error text
*/
protected function getError($htmlOptions = array())
{
return $this->form->error($this->model, $this->attribute, $htmlOptions);
}
......@@ -257,9 +264,23 @@ abstract class BootInput extends CInputWidget
{
if (isset($this->htmlOptions['hint']))
{
if (isset($this->htmlOptions['hintOptions']))
{
$htmlOptions = $this->htmlOptions['hintOptions'];
unset($this->htmlOptions['hintOptions']);
}
else
$htmlOptions = array();
if (isset($htmlOptions['class']))
$htmlOptions['class'] .= ' help-block';
else
$htmlOptions['class'] = 'help-block';
$hint = $this->htmlOptions['hint'];
unset($this->htmlOptions['hint']);
return '<p class="help-block">'.$hint.'</p>';
return CHtml::tag('p', $htmlOptions, $hint);
}
else
return '';
......@@ -267,7 +288,7 @@ abstract class BootInput extends CInputWidget
/**
* Returns the container CSS class for the input.
* @return string the CSS class.
* @return string the CSS class
*/
protected function getContainerCssClass()
{
......@@ -276,6 +297,48 @@ abstract class BootInput extends CInputWidget
}
/**
* Returns the input container CSS classes.
* @return string the CSS class
*/
protected function getInputContainerCssClass()
{
$classes = array();
if (isset($this->htmlOptions['prepend']))
$classes[] = 'input-prepend';
if (isset($this->htmlOptions['append']))
$classes[] = 'input-append';
return implode(' ', $classes);
}
/**
* Returns the HTML attributes for the CAPTCHA widget.
* @return array the attributes
* @since 0.10.0
*/
protected function getCaptchaOptions()
{
if (isset($this->htmlOptions['captchaOptions']))
{
$htmlOptions = $this->htmlOptions['captchaOptions'];
unset($this->htmlOptions['captchaOptions']);
}
else
$htmlOptions = array();
return $htmlOptions;
}
/**
* Returns whether the input has an add-on (prepend and/or append).
* @return boolean the result
*/
protected function hasAddOn()
{
return isset($this->htmlOptions['prepend']) || isset($this->htmlOptions['append']);
}
/**
* Renders a checkbox.
* @return string the rendered content
* @abstract
......
......@@ -32,16 +32,15 @@ class BootInputHorizontal extends BootInput
*/
protected function getLabel($htmlOptions = array())
{
$classes = 'control-label';
if (isset($htmlOptions['class']))
$htmlOptions['class'] .= ' '.$classes;
else
$htmlOptions['class'] = $classes;
if (!isset($this->htmlOptions['labelOptions']))
$this->htmlOptions['labelOptions'] = array();
if(isset($this->htmlOptions['id']))
$htmlOptions['for'] = $this->htmlOptions['id'];
if (isset($this->htmlOptions['labelOptions']['class']))
$this->htmlOptions['labelOptions']['class'] .= ' control-label';
else
$this->htmlOptions['labelOptions']['class'] = 'control-label';
return parent::getLabel($htmlOptions);
return parent::getLabel();
}
/**
......@@ -197,7 +196,7 @@ class BootInputHorizontal extends BootInput
{
echo $this->getLabel();
echo '<div class="controls"><div class="captcha">';
echo '<div class="widget">'.$this->widget('CCaptcha', $this->data, true).'</div>';
echo '<div class="widget">'.$this->widget('CCaptcha', $this->getCaptchaOptions(), true).'</div>';
echo $this->form->textField($this->model, $this->attribute, $this->htmlOptions);
echo $this->getError().$this->getHint();
echo '</div></div>';
......
......@@ -16,19 +16,6 @@ Yii::import('bootstrap.widgets.input.BootInput');
class BootInputVertical extends BootInput
{
/**
* Renders a CAPTCHA.
* @return string the rendered content
*/
protected function captcha()
{
echo $this->getLabel().'<div class="captcha">';
echo '<div class="widget">'.$this->widget('CCaptcha', $this->data, true).'</div>';
echo $this->form->textField($this->model, $this->attribute, $this->htmlOptions);
echo $this->getError().$this->getHint();
echo '</div>';
}
/**
* Renders a checkbox.
* @return string the rendered content
*/
......@@ -158,6 +145,19 @@ class BootInputVertical extends BootInput
}
/**
* Renders a CAPTCHA.
* @return string the rendered content
*/
protected function captcha()
{
echo $this->getLabel().'<div class="captcha">';
echo '<div class="widget">'.$this->widget('CCaptcha', $this->getCaptchaOptions(), true).'</div>';
echo $this->form->textField($this->model, $this->attribute, $this->htmlOptions);
echo $this->getError().$this->getHint();
echo '</div>';
}
/**
* Renders an uneditable field.
* @return string the rendered content
*/
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment