protected function getAttributes() { $elementName = $this->params['element']; $attribute = $this->params['attribute']; $model = $this->params['model']; // $attributes = array(); $attributes['type'] = 'text'; $attributes['name'] = $elementName . '[' . $attribute . ']'; // если в бине есть значение - берем его $attributes['value'] = $this->getValue(); // обязательность поля $validators = CCoreObjectsManager::getFieldValidators($model); if (array_key_exists($attribute, $validators)) { $attributes['required'] = 'required'; } $attributes['class'] = array(); if (array_key_exists('class', $this->params)) { $attributes['class'][] = $this->params['class']; } return $attributes; }
public static function activeLookup($name, CModel $model, $catalog = "", $isMultiple = false, $properties = array(), $allowCreation = false) { /** * Безумно полезная штука для работы со связанными * моделями. Если в названии поля есть скобки, то производится * разбор вида подмодель[ее поле] */ $submodelName = ""; if (strpos($name, "[") !== false) { $submodelName = substr($name, 0, strpos($name, "[")); $name = CUtils::strRight($name, "["); $name = CUtils::strLeft($name, "]"); $model = $model->{$submodelName}; } $field = $model::getClassName(); if ($submodelName !== "") { $field .= "[" . $submodelName . "]"; } $field .= "[" . $name . "]"; $fieldRequired = false; $validators = CCoreObjectsManager::getFieldValidators($model); if (array_key_exists($name, $validators)) { $fieldRequired = true; } $inline = ""; $class = self::getFielsizeClass(); $inline .= ' class="' . $class . '"'; $data = $model->{$name}; ?> <div class="catalogLookup" asu-catalog="<?php echo $catalog; ?> " asu-multiple="<?php echo $isMultiple ? "true" : "false"; ?> " asu-value-name="<?php echo $field; ?> " asu-creation="<?php echo $allowCreation ? "true" : "false"; ?> "> <?php if (is_object($data)) { ?> <?php $index = 0; ?> <?php foreach ($data->getItems() as $val) { $index++; ?> <input type="hidden" name="<?php echo $field; ?> [<?php echo $index; ?> ]" value="<?php echo $val->getId(); ?> " asu-type="value"> <?php } ?> <?php } else { ?> <input type="hidden" name="<?php echo $field; ?> " value="<?php echo $data; ?> " asu-type="value"> <?php } ?> <?php foreach ($properties as $key => $value) { ?> <input type="hidden" value="<?php echo $value; ?> " asu-type="property" asu-property-key="<?php echo $key; ?> "> <?php } ?> <table <?php echo $inline; ?> id="<?php echo $name; ?> " style="margin-left: 0px; "> <tr> <td width="100%"> <input type="text" value="" asu-name="lookup" placeholder="Введите текст для поиска" style="width: 95%; "> </td> <td style="width: 16px; "> <i class="icon-search" /> </td> </tr> <tr> <td width="100%"> <div class="btn-group btn-group-vertical" asu-type="placeholder" style="width: 100%; "> </div> </td> <td style="width: 16px; "> <i class="icon-remove" /> </td> </tr> </table> </div> <?php if (!self::$_catalogLookupInit) { self::$_catalogLookupInit = true; ?> <script> jQuery(document).ready(function(){ jQuery(".catalogLookup").catalogLookup(); }); </script> <?php } if ($fieldRequired) { self::requiredStar(); } }
/** * Функция валидации данных модели * * @return bool */ public function validate() { /** * Валидация полей модели */ $rules = CCoreObjectsManager::getFieldValidators($this); $labels = CCoreObjectsManager::getAttributeLabels($this); foreach ($rules as $field => $validators) { try { foreach ($validators as $validator) { if (is_object($validator)) { /** * Это новая система валидаторов */ if (!$validator->run($this->{$field})) { $error = str_replace("%name%", $labels[$field], $validator->getError()); $this->getValidationErrors()->add($field, $error); } } elseif (is_string($validator)) { /** * Это старая система валидаторов */ if ($validator == "required") { if ($this->{$field} == "") { $error = str_replace("%name%", $labels[$field], ERROR_FIELD_REQUIRED); $this->getValidationErrors()->add($field, $error); } } elseif ($validator == "numeric") { if (!is_numeric($this->{$field})) { $error = str_replace("%name%", $labels[$field], ERROR_FIELD_NUMERIC); $this->getValidationErrors()->add($field, $error); } } elseif ($validator == "selected") { if (is_a($this->{$field}, "CArrayList")) { if ($this->{$field}->getCount() == 0) { $error = str_replace("%name%", $labels[$field], ERROR_FIELD_SELECTED); $this->getValidationErrors()->add($field, $error); } } else { if ($this->{$field} == 0) { $error = str_replace("%name%", $labels[$field], ERROR_FIELD_SELECTED); $this->getValidationErrors()->add($field, $error); } } } elseif ($validator == "checkdate") { if ($this->{$field} != "") { $dateValue = $this->{$field}; $error = str_replace("%name%", $labels[$field], ERROR_FIELD_NOT_A_DATE); if (strtotime($dateValue) === false) { $this->getValidationErrors()->add($field, $error); } else { $dateArray = explode(".", $dateValue); if (!checkdate($dateArray[1], $dateArray[0], $dateArray[2])) { $this->getValidationErrors()->add($field, $error); } } } } } } } catch (Exception $e) { die("Error on field " . $field . " => " . $e->getMessage() . " on line " . $e->getLine()); } } $this->validateModel(VALIDATION_EVENT_UPDATE); return $this->getValidationErrors()->getCount() == 0; }