public function beginControlGroup(CModel $model, $attributes, $htmlOptions = array()) { if (!isset($htmlOptions['class'])) { $htmlOptions['class'] = 'control-group'; } if (!is_array($attributes)) { $attributes = explode(',', str_replace(' ', '', $attributes)); } foreach ($attributes as $attr) { if ($model->hasErrors($attr)) { $htmlOptions['class'] .= ' error'; } } return CHtml::tag('div', $htmlOptions, false, false); }
/** * Validates the specified object. * @param CModel $object the data object being validated * @param array $attributes the list of attributes to be validated. Defaults to null, * meaning every attribute listed in {@link attributes} will be validated. */ public function validate($object, $attributes = null) { if (is_array($attributes)) { $attributes = array_intersect($this->attributes, $attributes); } else { $attributes = $this->attributes; } foreach ($attributes as $attribute) { if (!$this->skipOnError || !$object->hasErrors($attribute)) { $this->validateAttribute($object, $attribute); } } }
/** * Generates a custom (pre-rendered) active form control group. * @param string $input the rendered input. * @param CModel $model the data model. * @param string $attribute the attribute. * @param array $htmlOptions additional HTML attributes. * @return string the generated control group. */ public static function customActiveControlGroup($input, $model, $attribute, $htmlOptions = array()) { // modificado segun lo visto en el foro de yii $htmlOptions['input'] = $input; if ($model->hasErrors($attribute)) { $htmlOptions['color'] = TbHtml::INPUT_COLOR_ERROR; $htmlOptions['help'] = $model->getError($attribute); $htmlOptions['helpOptions'] = array('type' => TbHtml::HELP_TYPE_BLOCK); } return self::activeControlGroup(self::INPUT_TYPE_CUSTOM, $model, $attribute, $htmlOptions); }
/** * Renders a horizontal custom field row for a model attribute. * * @param array|string $fieldData Pre-rendered field as string or array of arguments for call_user_func_array() function. * @param \CModel $model The data model. * @param string $attribute The attribute. * @param array $rowOptions Row options. */ protected function horizontalFieldRow(&$fieldData, &$model, &$attribute, &$rowOptions) { $controlGroupHtmlOptions = ['class' => 'control-group']; if ($model->hasErrors($attribute)) { Html::addCssClass($controlGroupHtmlOptions, \CHtml::$errorCss); } echo \CHtml::openTag('div', $controlGroupHtmlOptions); Html::addCssClass($rowOptions['labelOptions'], 'control-label'); if (isset($rowOptions['label'])) { if (!empty($rowOptions['label'])) { $for = isset($rowOptions['labelOptions']['for']) ? $rowOptions['labelOptions']['for'] : \CHtml::activeId($model, $attribute); echo \CHtml::label($rowOptions['label'], $for, $rowOptions['labelOptions']); } } else { echo $this->labelEx($model, $attribute, $rowOptions['labelOptions']); } echo '<div class="controls">'; if (!empty($rowOptions['prepend']) || !empty($rowOptions['append'])) { $this->renderAddOnBegin($rowOptions['prepend'], $rowOptions['append'], $rowOptions['prependOptions']); } if (is_array($fieldData)) { echo call_user_func_array($fieldData[0], $fieldData[1]); } else { echo $fieldData; } if (!empty($rowOptions['prepend']) || !empty($rowOptions['append'])) { $this->renderAddOnEnd($rowOptions['append'], $rowOptions['appendOptions']); } if ($this->showErrors && $rowOptions['errorOptions'] !== false) { echo $this->error($model, $attribute, $rowOptions['errorOptions'], $rowOptions['enableAjaxValidation'], $rowOptions['enableClientValidation']); } if (isset($rowOptions['hint'])) { if (!isset($rowOptions['hintOptions']['class'])) { Html::addCssClass($rowOptions['hintOptions'], $this->hintCssClass); } echo \CHtml::tag($this->hintTag, $rowOptions['hintOptions'], $rowOptions['hint']); } echo '</div></div>'; // controls, control-group }
/** * Renders a vertical custom field group for a model attribute. * * @param array|string $fieldData Pre-rendered field as string or array of arguments for call_user_func_array() function. * @param CModel $model The data model. * @param string $attribute The attribute. * @param array $options Row options. * */ protected function verticalGroup(&$fieldData, &$model, &$attribute, &$options) { $groupOptions = isset($options['groupOptions']) ? $options['groupOptions'] : array(); self::addCssClass($groupOptions, 'form-group'); if ($model->hasErrors($attribute)) { self::addCssClass($groupOptions, 'has-error'); } echo CHtml::openTag('div', $groupOptions); self::addCssClass($options['labelOptions'], 'control-label'); if (isset($options['label'])) { if (!empty($options['label'])) { echo CHtml::label($options['label'], CHtml::activeId($model, $attribute), $options['labelOptions']); } } else { echo $this->labelEx($model, $attribute, $options['labelOptions']); } if (!empty($options['prepend']) || !empty($options['append'])) { $this->renderAddOnBegin($options['prepend'], $options['append'], $options['prependOptions']); } if (is_array($fieldData)) { echo call_user_func_array($fieldData[0], $fieldData[1]); } else { echo $fieldData; } if (!empty($options['prepend']) || !empty($options['append'])) { $this->renderAddOnEnd($options['append'], $options['appendOptions']); } if ($this->showErrors && $options['errorOptions'] !== false) { echo $this->error($model, $attribute, $options['errorOptions'], $options['enableAjaxValidation'], $options['enableClientValidation']); } if (isset($options['hint'])) { self::addCssClass($options['hintOptions'], $this->hintCssClass); echo CHtml::tag($this->hintTag, $options['hintOptions'], $options['hint']); } echo '</div>'; }
/** * @return true/false. If the form and/or the model has any errors. * Currently this method does not support specifying the $attributeNameOrNames parameter. */ public function hasErrors($attributeNameOrNames = null) { $hasFormErrors = parent::hasErrors($attributeNameOrNames); if ($hasFormErrors) { return true; } return false; }
/** * @param string $method the CActiveForm method name, e.g. textField, dropDownList, etc. * @param CModel $model the form model * @param string $attribute the attribute name * @param bool|array $listOptions list options or false if none * @param array $options group options * @return string the rendered form group */ protected function renderGroup($method, $model, $attribute, $listOptions, $options) { if (isset($options['label']) && $options['label'] === false) { $beginLabel = $endLabel = $labelTitle = $label = ''; } else { $beginLabel = \CHtml::openTag('label', $options['labelOptions']); $endLabel = \CHtml::closeTag('label'); $labelTitle = isset($options['label']) ? $options['label'] : $model->getAttributeLabel($attribute); $label = \CHtml::tag('label', $options['labelOptions'], $labelTitle); } $beginWrapper = \CHtml::openTag($options['wrapperTag'], $options['wrapperOptions']); $endWrapper = \CHtml::closeTag($options['wrapperTag']); if ($model->hasErrors($attribute) && $options['enableErrorBlock']) { $error = \CHtml::tag($options['errorTag'], $options['errorOptions'], $model->getError($attribute)); $options['groupOptions']['class'] = isset($options['groupOptions']['class']) ? $options['groupOptions']['class'] . ' has-error' : 'has-error'; } else { $error = ''; } $help = isset($options['helpText']) ? \CHtml::tag($options['helpTag'], $options['helpOptions'], $options['helpText']) : ''; if ($this->layout === 'inline' && !isset($options['inputOptions']['placeholder'])) { $options['inputOptions']['placeholder'] = $labelTitle; } if ($listOptions) { $input = parent::$method($model, $attribute, $listOptions, $options['inputOptions']); } else { $input = parent::$method($model, $attribute, $options['inputOptions']); } if (isset($options['inputTemplate'])) { $input = strtr($options['inputTemplate'], array('{input}' => $input)); } $content = strtr($options['template'], array('{label}' => $label, '{beginLabel}' => $beginLabel, '{labelTitle}' => $labelTitle, '{endLabel}' => $endLabel, '{beginWrapper}' => $beginWrapper, '{endWrapper}' => $endWrapper, '{input}' => $input, '{error}' => $error, '{help}' => $help)); return \CHtml::tag($options['groupTag'], $options['groupOptions'], $content); }