44 public $modelClassNameForError;
51 public $bindAsLive =
false;
53 public static function makeErrorsSummaryId($id)
55 assert(
'is_string($id)');
71 assert(
'$model instanceof RedBeanModel || $model instanceof CModel');
73 foreach ($model->getErrors() as $attribute => $errors)
75 if ($model::isRelation($attribute) && $model::isOwnedRelation($attribute) &&
76 in_array($model::getRelationModelClassName($attribute), array(
'Address',
'Email',
'CurrencyValue')))
78 foreach ($errors as $relatedAttribute => $relatedErrors)
80 $errorData[ZurmoHtml::activeId($model, $attribute .
'[' . $relatedAttribute .
']')] = $relatedErrors;
85 $errorData[ZurmoHtml::activeId($model, $attribute)] = $errors;
91 public static function makeErrorsDataAndResolveForOwnedModelAttributesAndHasOneRelations($model)
93 assert(
'$model instanceof RedBeanModel || $model instanceof CModel');
95 foreach ($model->getErrors() as $attribute => $errors)
97 if ($model::isRelation($attribute) && $model::isOwnedRelation($attribute) &&
98 in_array($model::getRelationModelClassName($attribute), array(
'Address',
'Email',
'CurrencyValue')))
100 foreach ($errors as $relatedAttribute => $relatedErrors)
102 $errorData[ZurmoHtml::activeId($model, $attribute .
'[' . $relatedAttribute .
']')] = $relatedErrors;
105 elseif ($model::isRelation($attribute) &&
106 in_array($model::getRelationModelClassName($attribute), array(
'Account',
'Contact',
'Opportunity',
'Project')))
108 $errorData[ZurmoHtml::activeId($model, $attribute)] = array($model->getError($attribute));
112 $errorData[ZurmoHtml::activeId($model, $attribute)] = $errors;
126 assert(
'is_string($formId)');
127 $attributes = $this->getAttributes();
128 $encodedErrorAttributes = CJSON::encode(array_values($attributes));
130 var settings = $('#" . $formId .
"').data('settings');
131 $.each(" . $encodedErrorAttributes .
", function(i)
134 var alreadyInArray = false;
135 $.each(settings.attributes, function (i)
137 if (newId == this.id)
139 alreadyInArray = true;
142 if (alreadyInArray == false)
144 settings.attributes.push(this);
147 $('#" . $formId .
"').data('settings', settings);
149 Yii::app()->getClientScript()->registerScript(
'AddAttributeErrorSettingsScript' . $formId, $script);
159 public function error($model, $attribute, $htmlOptions = array(), $enableAjaxValidation =
true, $enableClientValidation =
true, $id = null)
161 if (!$this->enableAjaxValidation)
163 $enableAjaxValidation =
false;
165 if (!$this->enableClientValidation)
167 $enableClientValidation =
false;
169 if (!isset($htmlOptions[
'class']))
171 $htmlOptions[
'class'] = $this->errorMessageCssClass;
173 if (!$enableAjaxValidation && !$enableClientValidation)
175 return CHtml::error($model, $attribute, $htmlOptions);
179 $id = $this->resolveId($model, $attribute);
181 $inputID = isset($htmlOptions[
'inputID']) ? $htmlOptions[
'inputID'] : $id;
182 unset($htmlOptions[
'inputID']);
183 if (!isset($htmlOptions[
'id']))
185 $htmlOptions[
'id'] = $inputID .
'_em_';
189 'inputID' => $inputID,
190 'errorID' => $htmlOptions[
'id'],
191 'model' => $this->resolveModelClassNameForError($model),
192 'name' => $attribute,
193 'enableAjaxValidation' => $enableAjaxValidation,
196 $optionNames = array(
204 'validatingCssClass',
205 'beforeValidateAttribute',
206 'afterValidateAttribute',
208 foreach ($optionNames as $name)
210 if (isset($htmlOptions[$name]))
212 $option[$name] = $htmlOptions[$name];
213 unset($htmlOptions[$name]);
216 if ($model instanceof CActiveRecord && !$model->isNewRecord)
218 $option[
'status'] = 1;
220 if ($enableClientValidation)
222 $validators = isset($htmlOptions[
'clientValidation']) ? array($htmlOptions[
'clientValidation']) : array();
223 $attributeName = $attribute;
224 if (($pos = strrpos($attribute,
']')) !==
false && $pos !== strlen($attribute) - 1)
226 $attributeName = substr($attribute, $pos + 1);
228 foreach ($model->getValidators($attributeName) as $validator)
230 if ($validator->enableClientValidation)
232 if (($js = $validator->clientValidateAttribute($model, $attributeName)) !=
'')
238 if ($validators !== array())
240 $option[
'clientValidation'] =
new CJavaScriptExpression(
"function(value, messages, attribute) {\n" .
241 implode(
"\n", $validators) .
"\n}");
244 $html = CHtml::error($model, $attribute, $htmlOptions);
247 if (isset($htmlOptions[
'style']))
249 $htmlOptions[
'style'] = rtrim($htmlOptions[
'style'],
';') .
';display:none';
253 $htmlOptions[
'style'] =
'display:none';
255 $html = CHtml::tag(
'div', $htmlOptions,
'');
257 $this->attributes[$inputID] = $option;
266 public function errorSummary($models, $header = null, $footer = null, $htmlOptions = array())
268 if (!$this->enableAjaxValidation && !$this->enableClientValidation)
272 if (!isset($htmlOptions[
'id']))
274 $htmlOptions[
'id'] = static::makeErrorsSummaryId($this->
id);
279 if ($header === null)
281 $header =
'<p>' . Zurmo::t(
'yii',
'Please fix the following input errors:') .
'</p>';
283 if (!isset($htmlOptions[
'class']))
285 $htmlOptions[
'class'] = ZurmoHtml::$errorSummaryCss;
287 if (isset($htmlOptions[
'style']))
289 $htmlOptions[
'style'] = rtrim($htmlOptions[
'style'],
';') .
';display:none';
293 $htmlOptions[
'style'] =
'display:none';
295 $html = ZurmoHtml::tag(
'div', $htmlOptions, $header .
"\n<ul><li>dummy</li></ul>" . $footer);
298 $this->summaryID = $htmlOptions[
'id'];
307 if (is_array($this->focus))
309 $this->focus=
"#" . ZurmoHtml::activeId($this->focus[0], $this->focus[1]);
311 echo ZurmoHtml::endForm();
312 $cs = Yii::app()->clientScript;
313 $cs->registerScriptFile(
314 Yii::app()->getAssetManager()->publish(
315 Yii::getPathOfAlias(
'application.core.views.assets')
317 CClientScript::POS_END
319 if (!$this->enableAjaxValidation && !$this->enableClientValidation || empty($this->attributes))
321 if ($this->focus !== null)
323 $cs->registerCoreScript(
'jquery');
324 $cs->registerScript(
'CActiveForm#focus',
"
325 if (!window.location.hash)
326 { $('" . $this->focus .
"').focus(); }
331 $options = $this->clientOptions;
332 if (isset($this->clientOptions[
'validationUrl']) && is_array($this->clientOptions[
'validationUrl']))
334 $options[
'validationUrl'] = ZurmoHtml::normalizeUrl($this->clientOptions[
'validationUrl']);
337 $options[
'attributes'] = array_values($this->attributes);
338 if ($this->summaryID !== null)
340 $options[
'summaryID'] = $this->summaryID;
342 if ($this->focus !== null)
344 $options[
'focus'] = $this->focus;
347 $options = CJavaScript::encode($options);
350 $cs->registerScriptFile(
351 Yii::app()->getAssetManager()->publish(
352 Yii::getPathOfAlias(
'system.web.js.source')
353 ) .
'/jquery.yii.js',
354 CClientScript::POS_END
356 $cs->registerScriptFile(
357 Yii::app()->getAssetManager()->publish(
358 Yii::getPathOfAlias(
'system.web.js.source')
359 ) .
'/jquery.yiiactiveform.js',
360 CClientScript::POS_END
363 if ($this->bindAsLive)
365 $cs->registerScript(__CLASS__.
'#' . $id,
"\$('#$id').live('focus', function(e)
367 if ($(this).data('settings') == undefined)
369 $(this).yiiactiveform($options);
376 $cs->registerScript(__CLASS__.
'#' . $id,
"\$('#$id').yiiactiveform($options);");
380 public function getAttributes()
382 return $this->attributes;
389 public function radioButtonList($model, $attribute, $data, $htmlOptions = array(), $dataSelectOptions = array(),
390 $dataHtmlOptions = array())
401 public function checkBox($model, $attribute, $htmlOptions = array())
403 return ZurmoHtml::activeCheckBox($model, $attribute, $htmlOptions);
410 public function checkBoxList($model, $attribute, $data, $htmlOptions = array())
419 public function dropDownList($model, $attribute, $data, $htmlOptions = array())
424 protected function resolveId($model, $attribute)
426 return CHtml::activeId($model, $attribute);
429 protected function resolveModelClassNameForError($model)
431 if ($this->modelClassNameForError != null)
433 return $this->modelClassNameForError;
435 return get_class($model);
static activeDropDownList($model, $attribute, $data, $htmlOptions=array())
static errorSummary($model, $header=null, $footer=null, $htmlOptions=array())
static activeCheckBoxList($model, $attribute, $data, $htmlOptions=array())
static activeRadioButtonList($model, $attribute, $data, $htmlOptions=array(), $dataSelectOptions=array(), $dataHtmlOptions=array())