/** * Displays the first validation error for a model attribute. * @param CModel $model the data model * @param string $attribute the attribute name * @param array $htmlOptions additional HTML attributes to be rendered in the container div tag. * @param boolean $enableAjaxValidation whether to enable AJAX validation for the specified attribute. * @param boolean $enableClientValidation whether to enable client-side validation for the specified attribute. * @return string the validation result (error display or success message). */ public function error($model, $attribute, $htmlOptions = array(), $enableAjaxValidation = true, $enableClientValidation = true) { if (!$this->enableAjaxValidation) { $enableAjaxValidation = false; } if (!$this->enableClientValidation) { $enableClientValidation = false; } if (!$enableAjaxValidation && !$enableClientValidation) { return TbHtml::error($model, $attribute, $htmlOptions); } $id = CHtml::activeId($model, $attribute); $inputID = TbHtml::getOption('inputID', $htmlOptions, $id); unset($htmlOptions['inputID']); $htmlOptions = TbHtml::defaultOption('id', $inputID . '_em_', $htmlOptions); $option = array('id' => $id, 'inputID' => $inputID, 'errorID' => $htmlOptions['id'], 'model' => get_class($model), 'name' => $attribute, 'enableAjaxValidation' => $enableAjaxValidation, 'inputContainer' => 'div.control-group'); $optionNames = array('validationDelay', 'validateOnChange', 'validateOnType', 'hideErrorMessage', 'inputContainer', 'errorCssClass', 'successCssClass', 'validatingCssClass', 'beforeValidateAttribute', 'afterValidateAttribute'); foreach ($optionNames as $name) { if (isset($htmlOptions[$name])) { $option[$name] = TbHtml::popOption($name, $htmlOptions); } } if ($model instanceof CActiveRecord && !$model->isNewRecord) { $option['status'] = 1; } if ($enableClientValidation) { $validators = TbHtml::getOption('clientValidation', $htmlOptions, array()); $attributeName = $attribute; if (($pos = strrpos($attribute, ']')) !== false && $pos !== strlen($attribute) - 1) { // e.g. [a]name $attributeName = substr($attribute, $pos + 1); } foreach ($model->getValidators($attributeName) as $validator) { if ($validator->enableClientValidation) { if (($js = $validator->clientValidateAttribute($model, $attributeName)) != '') { $validators[] = $js; } } } if ($validators !== array()) { $option['clientValidation'] = "js:function(value, messages, attribute) {\n" . implode("\n", $validators) . "\n}"; } } $html = TbHtml::error($model, $attribute, $htmlOptions); if ($html === '') { $htmlOptions['type'] = $this->helpType; $htmlOptions = TbHtml::addStyles('display:none', $htmlOptions); $html = TbHtml::help('', $htmlOptions); } $this->attributes[$inputID] = $option; return $html; }
/** * Register required scripts. */ public function registerClientScript() { /** @var TbApi $api */ $selector = '#' . TbHtml::getOption('id', $this->htmlOptions, $this->getId()); $this->registerPlugin(TbApi::PLUGIN_TYPEAHEAD, $selector, $this->pluginOptions); }
/** * Normalizes the tab configuration. * @param array $tabs a reference to the tabs tab configuration. */ protected function normalizeTabs($tabs) { $controller = $this->getController(); if (isset($controller)) { foreach ($tabs as &$tabOptions) { $items = TbHtml::getOption('items', $tabOptions, array()); if (!empty($items)) { $tabOptions['items'] = $this->normalizeTabs($items); } else { if (isset($tabOptions['view'])) { $view = TbHtml::popOption('view', $tabOptions); if ($controller->getViewFile($view) !== false) { $tabOptions['content'] = $controller->renderPartial($view, $this->viewData, true); } } } } } return $tabs; }
/** * Normalizes the tab configuration. * @param array $tabs the tab configuration * @param array $panes a reference to the panes array * @param integer $i the current index * @return array the items */ protected function normalizeTabs($tabs, &$panes, &$i = 0) { $id = TbHtml::getOption('id', $this->htmlOptions, $this->getId()); $items = array(); //Check if has an active item $hasActiveItem = false; foreach ($tabs as $tab) { if ($hasActiveItem = TbHtml::getOption('active', $tab, false) === true) { break; } } foreach ($tabs as $tab) { $item = $tab; if (isset($item['visible']) && $item['visible'] === false) { continue; } // if no tab should be active, activate first if (!$hasActiveItem && $i == 0) { $item['active'] = true; } // set the label to the title if any if (isset($item['title'])) { $item['label'] = TbHtml::defaultOption('label', TbHtml::popOption('title', $item), $item); } $item = TbHtml::defaultOption('itemOptions', array(), $item); if (isset($tab['items'])) { $item['linkOptions']['data-toggle'] = 'dropdown'; $item['items'] = $this->normalizeTabs($item['items'], $panes, $i); } else { $item['linkOptions']['data-toggle'] = 'tab'; $item = TbHtml::defaultOption('id', $id . '_tab_' . ($i + 1), $item); $item['url'] = '#' . $item['id']; // no content and we have a view? if (!isset($item['content']) && isset($item['view'])) { $view = TbHtml::popOption('view', $item, ''); $data = TbHtml::popOption('data', $item, array()); if (is_array($this->viewData)) { $data = TbHtml::mergeOptions($this->viewData, $data); } $process = TbHtml::popOption('processOutput', $item, false); $item['content'] = !empty($view) ? $this->getController()->renderPartial($view, $data, true, $process) : ''; } $content = TbHtml::popOption('content', $item, ''); $paneOptions = TbHtml::popOption('paneOptions', $item, array()); $paneOptions['id'] = TbHtml::popOption('id', $item); $classes = array('tab-pane fade'); if (isset($item['active']) && $item['active']) { $classes[] = 'active in'; } $paneOptions = TbHtml::addClassName(implode(' ', $classes), $paneOptions); $panes[] = CHtml::tag('div', $paneOptions, $content); $i++; // increment the tab-index } $items[] = $item; } return $items; }
/** * Register required scripts. */ public function registerClientScript() { /** @var TbApi $api */ $api = Yii::app()->getComponent('bootstrap'); $selector = '#' . TbHtml::getOption('id', $this->htmlOptions, $this->getId()); $api->registerPlugin(TbApi::PLUGIN_TYPEAHEAD, $selector, $this->pluginOptions); }