/** * Returns the JavaScript needed for performing client-side validation. * @param CModel $object the data object being validated * @param string $attribute the name of the attribute to be validated. * @return string the client-side validation script. * @see CActiveForm::enableClientValidation * @since 1.1.7 */ public function clientValidateAttribute($object,$attribute) { $message=$this->message; if($this->requiredValue!==null) { if($message===null) $message=Yii::t('yii','{attribute} must be {value}.'); $message=strtr($message, array( '{value}'=>$this->requiredValue, '{attribute}'=>$object->getAttributeLabel($attribute), )); return " if(value!=" . CJSON::encode($this->requiredValue) . ") { messages.push(".CJSON::encode($message)."); } "; } else { if($message===null) $message=Yii::t('yii','{attribute} cannot be blank.'); $message=strtr($message, array( '{attribute}'=>$object->getAttributeLabel($attribute), )); return " if($.trim(value)=='') { messages.push(".CJSON::encode($message)."); } "; } }
/** * Returns the JavaScript needed for performing client-side validation. * @param CModel $object the data object being validated * @param string $attribute the name of the attribute to be validated. * @return string the client-side validation script. * @see CActiveForm::enableClientValidation * @since 1.1.7 */ public function clientValidateAttribute($object,$attribute) { if($this->pattern===null) throw new CException(Yii::t('yii','The "pattern" property must be specified with a valid regular expression.')); $message=$this->message!==null ? $this->message : Yii::t('yii','{attribute} is invalid.'); $message=strtr($message, array( '{attribute}'=>$object->getAttributeLabel($attribute), )); $pattern=$this->pattern; $pattern=preg_replace('/\\\\x\{?([0-9a-fA-F]+)\}?/', '\u$1', $pattern); $delim=substr($pattern, 0, 1); $endpos=strrpos($pattern, $delim, 1); $flag=substr($pattern, $endpos + 1); if ($delim!=='/') $pattern='/' . str_replace('/', '\\/', substr($pattern, 1, $endpos - 1)) . '/'; else $pattern = substr($pattern, 0, $endpos + 1); if (!empty($flag)) $pattern .= preg_replace('/[^igm]/', '', $flag); return " if(".($this->allowEmpty ? "$.trim(value)!='' && " : '').($this->not ? '' : '!')."value.match($pattern)) { messages.push(".CJSON::encode($message)."); } "; }
/** * Returns the JavaScript needed for performing client-side validation. * @param CModel $object the data object being validated * @param string $attribute the name of the attribute to be validated. * @return string the client-side validation script. * @see CActiveForm::enableClientValidation * @since 1.1.7 */ public function clientValidateAttribute($object, $attribute) { $message = $this->message; if ($this->requiredValue !== null) { if ($message === null) { $message = Yii::t('yii', '{attribute} phải là {value}.'); } $message = strtr($message, array('{value}' => $this->requiredValue, '{attribute}' => $object->getAttributeLabel($attribute))); return "\nif(value!=" . CJSON::encode($this->requiredValue) . ") {\n\tmessages.push(" . CJSON::encode($message) . ");\n}\n"; } else { if ($message === null) { $message = Yii::t('yii', '{attribute} không thể trống.'); } $message = strtr($message, array('{attribute}' => $object->getAttributeLabel($attribute))); return "\nif(jQuery.trim(value)=='') {\n\tmessages.push(" . CJSON::encode($message) . ");\n}\n"; } }
/** * Returns the validation error message. * @param CModel $object the data object being validated. * @param string $attribute the name of the attribute to be validated. * @return string the message. */ public function getErrorMessage($object, $attribute) { if (isset($this->message)) { $message = $this->message; } else { $message = Yii::t('validator', 'This is not a valid phone number.'); } return strtr($message, array('{attribute}' => $object->getAttributeLabel($attribute))); }
/** * Returns the validation error message. * @param CModel $object the data object being validated. * @param string $attribute the name of the attribute to be validated. * @return string the message. */ public function getErrorMessage($object, $attribute) { if (isset($this->message)) { $message = $this->message; } else { $message = Yii::t('validator', 'This value must be repeated exactly.'); } return strtr($message, array('{attribute}' => $object->getAttributeLabel($attribute))); }
/** * Validates the attribute of the object. * If there is any error, the error message is added to the object. * @param CModel $object the object being validated * @param string $attribute the attribute being validated */ protected function validateAttribute($object, $attribute) { $value = $object->{$attribute}; if ($this->allowEmpty && $this->isEmpty($value)) { return; } if ($this->compareValue !== null) { $compareTo = $compareValue = $this->compareValue; } else { $compareAttribute = $this->compareAttribute === null ? $attribute . '_repeat' : $this->compareAttribute; $compareValue = $object->{$compareAttribute}; $compareTo = $object->getAttributeLabel($compareAttribute); } switch ($this->operator) { case '=': case '==': if ($this->strict && $value !== $compareValue || !$this->strict && $value != $compareValue) { $message = $this->message !== null ? $this->message : Yii::t('yii', '{attribute} must be repeated exactly.'); $this->addError($object, $attribute, $message, array('{compareAttribute}' => $compareTo)); } break; case '!=': if ($this->strict && $value === $compareValue || !$this->strict && $value == $compareValue) { $message = $this->message !== null ? $this->message : Yii::t('yii', '{attribute} must not be equal to "{compareValue}".'); $this->addError($object, $attribute, $message, array('{compareAttribute}' => $compareTo, '{compareValue}' => $compareValue)); } break; case '>': if ($value <= $compareValue) { $message = $this->message !== null ? $this->message : Yii::t('yii', '{attribute} must be greater than "{compareValue}".'); $this->addError($object, $attribute, $message, array('{compareAttribute}' => $compareTo, '{compareValue}' => $compareValue)); } break; case '>=': if ($value < $compareValue) { $message = $this->message !== null ? $this->message : Yii::t('yii', '{attribute} must be greater than or equal to "{compareValue}".'); $this->addError($object, $attribute, $message, array('{compareAttribute}' => $compareTo, '{compareValue}' => $compareValue)); } break; case '<': if ($value >= $compareValue) { $message = $this->message !== null ? $this->message : Yii::t('yii', '{attribute} must be less than "{compareValue}".'); $this->addError($object, $attribute, $message, array('{compareAttribute}' => $compareTo, '{compareValue}' => $compareValue)); } break; case '<=': if ($value > $compareValue) { $message = $this->message !== null ? $this->message : Yii::t('yii', '{attribute} must be less than or equal to "{compareValue}".'); $this->addError($object, $attribute, $message, array('{compareAttribute}' => $compareTo, '{compareValue}' => $compareValue)); } break; default: throw new CException(Yii::t('yii', 'Invalid operator "{operator}".', array('{operator}' => $this->operator))); } }
/** * Returns the validation error message. * @param CModel $object the data object being validated. * @param string $attribute the name of the attribute to be validated. * @return string the message. */ public function getErrorMessage($object, $attribute) { if (isset($this->message)) { $message = $this->message; } elseif (isset($this->min, $this->max)) { $message = Yii::t('validator', 'The value must gave between {min} and {max} characters.'); } elseif (isset($this->min)) { $message = Yii::t('validator', 'The value is too short (minimum is {min} characters).'); } elseif (isset($this->max)) { $message = Yii::t('validator', 'The value is too long (maximum is {max} characters).'); } else { $message = Yii::t('validator', 'The value is invalid.'); } return strtr($message, array('{attribute}' => $object->getAttributeLabel($attribute), '{min}' => $this->min, '{max}' => $this->max)); }
/** * Returns the validation error message. * @param CModel $object the data object being validated. * @param string $attribute the name of the attribute to be validated. * @return string the message. */ public function getErrorMessage($object, $attribute) { if (isset($this->message)) { $message = $this->message; } elseif (isset($this->min, $this->max)) { $message = Yii::t('validator', 'You must select between {min} and {max} choices.'); } elseif (isset($this->min)) { $message = Yii::t('validator', 'You must select at least {min} choices.'); } elseif (isset($this->max)) { $message = Yii::t('validator', 'You cannot have more than {max} choices selected.'); } else { $message = Yii::t('validator', 'Invalid amount of choices selected.'); } return strtr($message, array('{attribute}' => $object->getAttributeLabel($attribute), '{min}' => $this->min, '{max}' => $this->max)); }
/** * Returns the JavaScript needed for performing client-side validation. * @param CModel $object the data object being validated * @param string $attribute the name of the attribute to be validated. * @return string the client-side validation script. * @see CActiveForm::enableClientValidation * @since 1.1.7 */ public function clientValidateAttribute($object, $attribute) { if (!is_array($this->range)) { throw new CException(Yii::t('yii', 'The "range" property must be specified with a list of values.')); } if (($message = $this->message) === null) { $message = $this->not ? Yii::t('yii', '{attribute} is in the list.') : Yii::t('yii', '{attribute} is not in the list.'); } $message = strtr($message, array('{attribute}' => $object->getAttributeLabel($attribute))); $range = array(); foreach ($this->range as $value) { $range[] = (string) $value; } $range = CJSON::encode($range); return "\nif(" . ($this->allowEmpty ? "\$.trim(value)!='' && " : '') . ($this->not ? "\$.inArray(value, {$range})>=0" : "\$.inArray(value, {$range})<0") . ") {\n\tmessages.push(" . CJSON::encode($message) . ");\n}\n"; }
/** * Validates the attribute of the object. * If there is any error, the error message is added to the object. * @param CModel $object the object being validated * @param string $attribute the attribute being validated */ protected function validateAttribute($object, $attribute) { if ($this->otherFieldValue === false && !$object->{$this->otherField}) { return; } if ($this->otherFieldValue === false || $object->{$this->otherField} == $this->otherFieldValue || is_array($this->otherFieldValue) && in_array($object->{$this->otherField}, $this->otherFieldValue)) { $otherFieldLabel = $object->getAttributeLabel($this->otherField); $otherFieldValueLabel = $this->otherFieldValue ? $this->otherFieldValue : Yii::t('dressing', 'not blank'); $value = $object->{$attribute}; if ($this->requiredValue !== null) { if (!$this->strict && $value != $this->requiredValue || $this->strict && $value !== $this->requiredValue) { $message = $this->message !== null ? $this->message : Yii::t('dressing', '{attribute} must be {value} when {otherFieldLabel} is {otherFieldValueLabel}.', array('{value}' => $this->requiredValue, '{otherFieldLabel}' => $otherFieldLabel, '{otherFieldValueLabel}' => $otherFieldValueLabel)); $this->addError($object, $attribute, $message); } } else { if ($this->isEmpty($value, true)) { $message = $this->message !== null ? $this->message : Yii::t('dressing', '{attribute} cannot be blank when {otherFieldLabel} is {otherFieldValueLabel}.', array('{otherFieldLabel}' => $otherFieldLabel, '{otherFieldValueLabel}' => $otherFieldValueLabel)); $this->addError($object, $attribute, $message); } } } }
/** * Returns the JavaScript needed for performing client-side validation. * @param CModel $object the data object being validated * @param string $attribute the name of the attribute to be validated. * @return string the client-side validation script. * @see CActiveForm::enableClientValidation * @since 1.1.7 */ public function clientValidateAttribute($object, $attribute) { $message = $this->message; if ($this->requiredValue !== null) { if ($message === null) { $message = Yii::t('yii', '{attribute} must be {value}.'); } $message = strtr($message, array('{value}' => $this->requiredValue, '{attribute}' => $object->getAttributeLabel($attribute))); return "\r\nif(value!=" . CJSON::encode($this->requiredValue) . ") {\r\n\tmessages.push(" . CJSON::encode($message) . ");\r\n}\r\n"; } else { if ($message === null) { $message = Yii::t('yii', '{attribute} cannot be blank.'); } $message = strtr($message, array('{attribute}' => $object->getAttributeLabel($attribute))); if ($this->trim) { $emptyCondition = "jQuery.trim(value)==''"; } else { $emptyCondition = "value==''"; } return "\r\nif({$emptyCondition}) {\r\n\tmessages.push(" . CJSON::encode($message) . ");\r\n}\r\n"; } }
/** * Returns the JavaScript needed for performing client-side validation. * @param CModel $object the data object being validated * @param string $attribute the name of the attribute to be validated. * @return string the client-side validation script. * @see CActiveForm::enableClientValidation * @since 1.1.7 */ public function clientValidateAttribute($object, $attribute) { $js = ''; if (!$this->allowEmpty) { $message = $this->message; if ($message == null) { $message = Yii::t('yii', '{attribute} cannot be blank.'); } $message = strtr($message, array('{attribute}' => $object->getAttributeLabel($attribute))); $js .= ' if($.trim(value)==""){messages.push(' . CJSON::encode($message) . ');} '; } if ($this->types !== null) { if (is_string($this->types)) { $types = preg_split('/[\\s,]+/', strtolower($this->types), -1, PREG_SPLIT_NO_EMPTY); } else { $types = $this->types; } $message = $this->wrongType; if ($message == null) { $message = Yii::t('yii', 'The file "{file}" cannot be uploaded. Only files with these extensions are allowed: {extensions}.'); } $message = strtr($message, array('{file}' => ':file', '{extensions}' => implode(', ', $types))); $js .= "\r\n if(['" . implode("','", $types) . "'].indexOf(\$.trim(value).split('.').pop().toLowerCase()) == -1" . ($this->allowEmpty ? " && \$.trim(value)!=''" : '') . ")\r\n {\r\n messages.push('" . $message . "'.replace(':file', \$.trim(value)));\r\n }\r\n "; } /** * Check the maxfile size setting */ if ($this->maxSize !== null) { $message = $this->tooLarge !== null ? $this->tooLarge : Yii::t('yii', 'The file "{file}" is too large. Its size cannot exceed {limit} bytes.'); $message = strtr($message, array('{file}' => ':file', '{limit}' => $this->getReadableFileSize($this->getSizeLimit()))); $inputId = get_class($object) . "_" . $attribute; $js .= "\r\n if (\$('#{$inputId}')[0].files[0]){\r\n var fileSize = \$('#{$inputId}')[0].files[0].size; \r\n if(fileSize>{$this->maxSize}){\r\n messages.push('" . $message . "'.replace(':file', \$.trim(value)));\r\n }\r\n }\r\n "; } return $js; }
/** * Returns the JavaScript needed for performing client-side validation. * @param CModel $object the data object being validated * @param string $attribute the name of the attribute to be validated. * @return string the client-side validation script. * @see CActiveForm::enableClientValidation * @since 1.1.7 */ public function clientValidateAttribute($object, $attribute) { $captcha = $this->getCaptchaAction(); $message = $this->message !== null ? $this->message : Yii::t('yii', 'The verification code is incorrect.'); $message = strtr($message, array('{attribute}' => $object->getAttributeLabel($attribute))); $code = $captcha->getVerifyCode(false); $hash = $captcha->generateValidationHash($this->caseSensitive ? $code : strtolower($code)); $js = "\r\nvar hash = jQuery('body').data('{$this->captchaAction}.hash');\r\nif (hash == null)\r\n\thash = {$hash};\r\nelse\r\n\thash = hash[" . ($this->caseSensitive ? 0 : 1) . "];\r\nfor(var i=value.length-1, h=0; i >= 0; --i) h+=value." . ($this->caseSensitive ? '' : 'toLowerCase().') . "charCodeAt(i);\r\nif(h != hash) {\r\n\tmessages.push(" . CJSON::encode($message) . ");\r\n}\r\n"; if ($this->allowEmpty) { $js = "\r\nif(jQuery.trim(value)!='') {\r\n\t{$js}\r\n}\r\n"; } return $js; }
/** * Returns the JavaScript needed for performing client-side validation. * @param CModel $object the data object being validated * @param string $attribute the name of the attribute to be validated. * @return string the client-side validation script. * @see CActiveForm::enableClientValidation * @since 1.1.7 */ public function clientValidateAttribute($object, $attribute) { if ($this->validateIDN) { Yii::app()->getClientScript()->registerCoreScript('punycode'); // punycode.js works only with the domains - so we have to extract it before punycoding $validateIDN = ' var info = value.match(/^(.+:\\/\\/|)([^/]+)/); if (info) value = info[1] + punycode.toASCII(info[2]); '; } else { $validateIDN = ''; } $message = $this->message !== null ? $this->message : Yii::t('yii', '{attribute} is not a valid URL.'); $message = strtr($message, array('{attribute}' => $object->getAttributeLabel($attribute))); if (strpos($this->pattern, '{schemes}') !== false) { $pattern = str_replace('{schemes}', '(' . implode('|', $this->validSchemes) . ')', $this->pattern); } else { $pattern = $this->pattern; } $js = "\n{$validateIDN}\nif(!value.match({$pattern})) {\n\tmessages.push(" . CJSON::encode($message) . ");\n}\n"; if ($this->defaultScheme !== null) { $js = "\nif(!value.match(/:\\/\\//)) {\n\tvalue=" . CJSON::encode($this->defaultScheme) . "+'://'+value;\n}\n{$js}\n"; } if ($this->allowEmpty) { $js = "\nif(jQuery.trim(value)!='') {\n\t{$js}\n}\n"; } return $js; }
/** * @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); }
/** * Returns the validation error message. * @param CModel $object the data object being validated. * @param string $attribute the name of the attribute to be validated. * @return string the message. */ public function getErrorMessage($object, $attribute) { if (isset($this->message)) { $message = $this->message; } elseif (isset($this->min, $this->max)) { if ($this->integerOnly) { $message = Yii::t('validator', 'The value must be an integer between {min} and {max}.'); } else { $message = Yii::t('validator', 'The value must be a number between {min} and {max}.'); } } elseif (isset($this->min)) { $message = Yii::t('validator', 'The value is too small (minimum is {min}).'); } elseif (isset($this->min)) { $message = Yii::t('validator', 'The value is too big (maximum is {max}).'); } elseif ($this->integerOnly) { $message = Yii::t('validator', 'The value must be an integer.'); } else { $message = Yii::t('validator', 'The value must be a number.'); } return strtr($message, array('{attribute}' => $object->getAttributeLabel($attribute), '{min}' => $this->min, '{max}' => $this->max)); }
/** * Returns the JavaScript needed for performing client-side validation. * @param CModel $object the data object being validated * @param string $attribute the name of the attribute to be validated. * @return string the client-side validation script. * @see CActiveForm::enableClientValidation * @since 1.1.7 */ public function clientValidateAttribute($object, $attribute) { $message = $this->message !== null ? $this->message : Yii::t('yii', '{attribute} must be either {true} or {false}.'); $message = strtr($message, array('{attribute}' => $object->getAttributeLabel($attribute), '{true}' => $this->trueValue, '{false}' => $this->falseValue)); return "\nif(" . ($this->allowEmpty ? "\$.trim(value)!='' && " : '') . "value!=" . CJSON::encode($this->trueValue) . " && value!=" . CJSON::encode($this->falseValue) . ") {\n\tmessages.push(" . CJSON::encode($message) . ");\n}\n"; }
/** * Adds an error about the specified attribute to the active record. * This is a helper method that performs message selection and internationalization. * @param CModel $object the data object being validated * @param string $attribute the attribute being validated * @param string $message the error message * @param array $params values for the placeholders in the error message */ protected function addError($object, $attribute, $message, $params = array()) { $params['{attribute}'] = $object->getAttributeLabel($attribute); $object->addError($attribute, strtr($message, $params)); }
/** * Generates an active form row. * @param string $type the input type. * @param CModel $model the data model. * @param string $attribute the attribute. * @param array $htmlOptions additional HTML attributes. * @param array $data data for multiple select inputs. * @return string the generated control group. */ public static function activeControlGroup($type, $model, $attribute, $htmlOptions = array(), $data = array()) { $color = BsArray::popValue('color', $htmlOptions); $groupOptions = BsArray::popValue('groupOptions', $htmlOptions, array()); $controlOptions = BsArray::popValue('controlOptions', $htmlOptions, array()); $labelOptions = BsArray::popValue('labelOptions', $htmlOptions, array()); $layout = BsArray::popValue('formLayout', $htmlOptions); if (in_array($type, array(self::INPUT_TYPE_CHECKBOX, self::INPUT_TYPE_RADIOBUTTON))) { $htmlOptions['label'] = $model->getAttributeLabel($attribute); $htmlOptions['labelOptions'] = $labelOptions; } $help = BsArray::popValue('help', $htmlOptions, ''); $helpOptions = BsArray::popValue('helpOptions', $htmlOptions, array()); if (!empty($help)) { $help = self::inputHelp($help, $helpOptions); } $error = BsArray::popValue('error', $htmlOptions, ''); $input = isset($htmlOptions['input']) ? $htmlOptions['input'] : self::createActiveInput($type, $model, $attribute, $htmlOptions, $data); self::addCssClass('form-group', $groupOptions); if (!empty($layout)) { if ($layout === BsHtml::FORM_LAYOUT_HORIZONTAL) { $controlClass = BsArray::popValue('class', $controlOptions, self::$formLayoutHorizontalControlClass); self::addCssClass($controlClass, $controlOptions); } } if (!empty($color)) { self::addCssClass($color, $groupOptions); } $output = self::openTag('div', $groupOptions); if ($labelOptions !== false && $layout !== self::FORM_LAYOUT_INLINE) { if (isset($labelOptions['ex']) && empty($labelOptions['ex'])) { // todo: consider adding support for overriding the label with plain text. unset($labelOptions['ex']); $output .= parent::activeLabel($model, $attribute, $labelOptions); } else { $output .= parent::activeLabelEx($model, $attribute, $labelOptions); } } $output .= self::controls($input . $error . $help, $controlOptions); $output .= '</div>'; return $output; }
/** * Returns the JavaScript needed for performing client-side validation. * @param CModel $object the data object being validated * @param string $attribute the name of the attribute to be validated. * @return string the client-side validation script. * @see CActiveForm::enableClientValidation * @since 1.1.7 */ public function clientValidateAttribute($object, $attribute) { $label = $object->getAttributeLabel($attribute); if (($message = $this->message) === null) { $message = $this->integerOnly ? Yii::t('yii', '{attribute} must be an integer.') : Yii::t('yii', '{attribute} must be a number.'); } $message = strtr($message, array('{attribute}' => $label)); if (($tooBig = $this->tooBig) === null) { $tooBig = Yii::t('yii', '{attribute} is too big (maximum is {max}).'); } $tooBig = strtr($tooBig, array('{attribute}' => $label, '{max}' => $this->max)); if (($tooSmall = $this->tooSmall) === null) { $tooSmall = Yii::t('yii', '{attribute} is too small (minimum is {min}).'); } $tooSmall = strtr($tooSmall, array('{attribute}' => $label, '{min}' => $this->min)); $pattern = $this->integerOnly ? $this->integerPattern : $this->numberPattern; $js = "\r\nif(!value.match({$pattern})) {\r\n\tmessages.push(" . CJSON::encode($message) . ");\r\n}\r\n"; if ($this->min !== null) { $js .= "\r\nif(value<{$this->min}) {\r\n\tmessages.push(" . CJSON::encode($tooSmall) . ");\r\n}\r\n"; } if ($this->max !== null) { $js .= "\r\nif(value>{$this->max}) {\r\n\tmessages.push(" . CJSON::encode($tooBig) . ");\r\n}\r\n"; } if ($this->allowEmpty) { $js = "\r\nif(\$.trim(value)!='') {\r\n\t{$js}\r\n}\r\n"; } return $js; }
/** * Generates an active form row. * @param string $type the input type. * @param CModel $model the data model. * @param string $attribute the attribute. * @param array $htmlOptions additional HTML attributes. * @param array $data data for multiple select inputs. * @return string the generated control group. */ public static function activeControlGroup($type, $model, $attribute, $htmlOptions = array(), $data = array()) { $color = TbArray::popValue('color', $htmlOptions); $groupOptions = TbArray::popValue('groupOptions', $htmlOptions, array()); $controlOptions = TbArray::popValue('controlOptions', $htmlOptions, array()); $label = TbArray::popValue('label', $htmlOptions); $labelOptions = TbArray::popValue('labelOptions', $htmlOptions, array()); // todo: remove everything that has to do with form layout $formLayout = TbArray::popValue('formLayout', $htmlOptions, self::FORM_LAYOUT_VERTICAL); $labelWidthClass = TbArray::popValue('labelWidthClass', $htmlOptions, self::$defaultFormLabelWidthClass); // Retrieve the old-style "span" option $span = TbArray::popValue('span', $htmlOptions); if (!empty($span)) { $controlWidthClass = 'col-md-' . $span; } else { $defaultFormControlWidthClass = 'col-sm-9'; $controlWidthClass = TbArray::popValue('controlWidthClass', $htmlOptions, self::$defaultFormControlWidthClass); } $useFormGroup = true; $useControls = true; $output = ''; // Special label case case for individual checkboxes and radios if ($type == self::INPUT_TYPE_CHECKBOX || $type == self::INPUT_TYPE_RADIOBUTTON) { $htmlOptions['label'] = isset($label) ? $label : $model->getAttributeLabel($attribute); $htmlOptions['labelOptions'] = $labelOptions; $htmlOptions['useContainer'] = true; $label = false; $useFormGroup = false; } // Special conditions depending on the form type if ($formLayout == self::FORM_LAYOUT_HORIZONTAL) { switch ($type) { case self::INPUT_TYPE_CHECKBOX: case self::INPUT_TYPE_RADIOBUTTON: self::addCssClass(self::switchColToOffset($labelWidthClass), $controlOptions); self::addCssClass(self::switchOffsetToCol($controlWidthClass), $controlOptions); $useFormGroup = true; break; default: self::addCssClass(self::switchOffsetToCol($labelWidthClass), $labelOptions); self::addCssClass(self::switchOffsetToCol($controlWidthClass), $controlOptions); //print_r($labelWidthClass . ' - ' . $controlWidthClass ); die(); } } elseif ($formLayout == self::FORM_LAYOUT_INLINE || $formLayout == self::FORM_LAYOUT_SEARCH) { switch ($type) { case self::INPUT_TYPE_TEXT: case self::INPUT_TYPE_PASSWORD: case self::INPUT_TYPE_URL: case self::INPUT_TYPE_EMAIL: case self::INPUT_TYPE_NUMBER: case self::INPUT_TYPE_RANGE: case self::INPUT_TYPE_DATE: case self::INPUT_TYPE_FILE: case self::INPUT_TYPE_SEARCH: self::addCssClass('sr-only', $labelOptions); if ($label !== null && TbArray::getValue('placeholder', $htmlOptions) !== null) { $htmlOptions['placeholder'] = $label; } break; case self::INPUT_TYPE_CHECKBOX: case self::INPUT_TYPE_RADIOBUTTON: $useControls = false; break; } } // remove until here. if (isset($label) && $label !== false) { $labelOptions['label'] = $label; } $help = TbArray::popValue('help', $htmlOptions, ''); $helpOptions = TbArray::popValue('helpOptions', $htmlOptions, array()); if (!empty($help)) { $help = self::inputHelp($help, $helpOptions); } $error = TbArray::popValue('error', $htmlOptions, ''); $input = isset($htmlOptions['input']) ? $htmlOptions['input'] : self::createActiveInput($type, $model, $attribute, $htmlOptions, $data); if (!empty($color)) { self::addCssClass($color, $groupOptions); } self::addCssClass('control-label', $labelOptions); if ($label !== false) { $output .= parent::activeLabelEx($model, $attribute, $labelOptions); } if ($useControls) { $output .= self::controls($input . $error . $help, $controlOptions); } else { $output .= $input; } if ($useFormGroup) { self::addCssClass('form-group', $groupOptions); return self::tag('div', $groupOptions, $output); } else { return $output; } }
/** * Returns the JavaScript needed for performing client-side validation. * @param CModel $object the data object being validated * @param string $attribute the name of the attribute to be validated. * @return string the client-side validation script. * @see CActiveForm::enableClientValidation * @since 1.1.7 */ public function clientValidateAttribute($object, $attribute) { $message = $this->message !== null ? $this->message : Yii::t('yii', '{attribute} is not a valid email address.'); $message = strtr($message, array('{attribute}' => $object->getAttributeLabel($attribute))); $condition = "!value.match({$this->pattern})"; if ($this->allowName) { $condition .= " && !value.match({$this->fullPattern})"; } return "\nif(" . ($this->allowEmpty ? "\$.trim(value)!='' && " : '') . $condition . ") {\n\tmessages.push(" . CJSON::encode($message) . ");\n}\n"; }
/** * Returns the JavaScript needed for performing client-side validation. * @param CModel $object the data object being validated * @param string $attribute the name of the attribute to be validated. * @return string the client-side validation script. * @since 1.1.7 */ public function clientValidateAttribute($object, $attribute) { $label = $object->getAttributeLabel($attribute); if (($message = $this->message) === null) { $message = Yii::t('yii', '{attribute} is of the wrong length (should be {length} characters).'); } $message = strtr($message, array('{attribute}' => $label, '{length}' => $this->is)); if (($tooShort = $this->tooShort) === null) { $tooShort = Yii::t('yii', '{attribute} is too short (minimum is {min} characters).'); } $tooShort = strtr($tooShort, array('{attribute}' => $label, '{min}' => $this->min)); if (($tooLong = $this->tooLong) === null) { $tooLong = Yii::t('yii', '{attribute} is too long (maximum is {max} characters).'); } $tooLong = strtr($tooLong, array('{attribute}' => $label, '{max}' => $this->max)); $js = ''; if ($this->min !== null) { $js .= "\nif(value.length<{$this->min}) {\n\tmessages.push(" . CJSON::encode($tooShort) . ");\n}\n"; } if ($this->max !== null) { $js .= "\nif(value.length>{$this->max}) {\n\tmessages.push(" . CJSON::encode($tooLong) . ");\n}\n"; } if ($this->is !== null) { $js .= "\nif(value.length!={$this->is}) {\n\tmessages.push(" . CJSON::encode($message) . ");\n}\n"; } if ($this->allowEmpty) { $js = "\nif(jQuery.trim(value)!='') {\n\t{$js}\n}\n"; } return $js; }
/** * Generates an input HTML tag for a model attribute. * This method generates an input HTML tag based on the given data model and attribute. * If the attribute has input error, the input field's CSS class will * be appended with {@link errorCss}. * This enables highlighting the incorrect input. * @param string $type the input type (e.g. 'text', 'radio') * @param CModel $model the data model * @param string $attribute the attribute * @param array $htmlOptions additional HTML attributes for the HTML tag * @return string the generated input tag */ protected static function activeInputField($type, $model, $attribute, $htmlOptions) { $label = $model->getAttributeLabel($attribute); $htmlOptions['type'] = $type; //$htmlOptions['placeholder']=$label; $htmlOptions['class'] = isset($htmlOptions['class']) ? 'form-control ' . $htmlOptions['class'] : "form-control"; if ($type === 'text' || $type === 'password') { if (!isset($htmlOptions['maxlength'])) { foreach ($model->getValidators($attribute) as $validator) { if ($validator instanceof CStringValidator && $validator->max !== null) { $htmlOptions['maxlength'] = $validator->max; break; } } } elseif ($htmlOptions['maxlength'] === false) { unset($htmlOptions['maxlength']); } } if ($type === 'file') { unset($htmlOptions['value']); } elseif (!isset($htmlOptions['value'])) { $htmlOptions['value'] = self::resolveValue($model, $attribute); } if ($model->hasErrors($attribute)) { self::addErrorCss($htmlOptions); } return self::tag('input', $htmlOptions); }
private function csvModel($fileHandle, CModel $cModel, $attributes) { foreach ($attributes as $attr) { $row = array(); $row[] = $cModel->getAttributeLabel($attr); $row[] = CHtml::value($cModel, $attr); fputcsv($fileHandle, $row, $this->csvDelimiter, $this->csvEnclosure); } }
/** * Generates an active form row. * @param string $type the input type. * @param CModel $model the data model. * @param string $attribute the attribute. * @param array $htmlOptions additional HTML attributes. * @param array $data data for multiple select inputs. * @return string the generated control group. */ public static function activeControlGroup($type, $model, $attribute, $htmlOptions = array(), $data = array()) { $color = TbArray::popValue('color', $htmlOptions); $groupOptions = TbArray::popValue('groupOptions', $htmlOptions, array()); $controlOptions = TbArray::popValue('controlOptions', $htmlOptions, array()); $label = TbArray::popValue('label', $htmlOptions); $labelOptions = TbArray::popValue('labelOptions', $htmlOptions, array()); if (in_array($type, array(self::INPUT_TYPE_CHECKBOX, self::INPUT_TYPE_RADIOBUTTON))) { $htmlOptions['label'] = isset($label) ? $label : $model->getAttributeLabel($attribute); $htmlOptions['labelOptions'] = $labelOptions; $label = false; } if (isset($label) && $label !== false) { $labelOptions['label'] = $label; } $help = TbArray::popValue('help', $htmlOptions, ''); $helpOptions = TbArray::popValue('helpOptions', $htmlOptions, array()); if (!empty($help)) { $help = self::inputHelp($help, $helpOptions); } $error = TbArray::popValue('error', $htmlOptions, ''); $input = isset($htmlOptions['input']) ? $htmlOptions['input'] : self::createActiveInput($type, $model, $attribute, $htmlOptions, $data); self::addCssClass('control-group', $groupOptions); if (!empty($color)) { self::addCssClass($color, $groupOptions); } self::addCssClass('control-label', $labelOptions); $output = self::openTag('div', $groupOptions); if ($label !== false) { $output .= parent::activeLabelEx($model, $attribute, $labelOptions); } $output .= self::controls($input . $error . $help, $controlOptions); $output .= '</div>'; return $output; }
/** * Returns the JavaScript needed for performing client-side validation. * @param CModel $object the data object being validated * @param string $attribute the name of the attribute to be validated. * @return string the client-side validation script. * @see CActiveForm::enableClientValidation * @since 1.1.7 */ public function clientValidateAttribute($object, $attribute) { if ($this->validateIDN) { Yii::app()->getClientScript()->registerCoreScript('punycode'); // punycode.js works only with the domains - so we have to extract it before punycoding $validateIDN = ' var info = value.match(/^(.[^@]+)@(.+)$/); if (info) value = info[1] + "@" + punycode.toASCII(info[2]); '; } else { $validateIDN = ''; } $message = $this->message !== null ? $this->message : Yii::t('yii', '{attribute} is not a valid email address.'); $message = strtr($message, array('{attribute}' => $object->getAttributeLabel($attribute))); $condition = "!value.match({$this->pattern})"; if ($this->allowName) { $condition .= " && !value.match({$this->fullPattern})"; } return "\n{$validateIDN}\nif(" . ($this->allowEmpty ? "jQuery.trim(value)!='' && " : '') . $condition . ") {\n\tmessages.push(" . CJSON::encode($message) . ");\n}\n"; }
/** * Returns the JavaScript needed for performing client-side validation. * @param CModel $object the data object being validated * @param string $attribute the name of the attribute to be validated. * @return string the client-side validation script. * @see CActiveForm::enableClientValidation * @since 1.1.7 */ public function clientValidateAttribute($object, $attribute) { $message = $this->message !== null ? $this->message : Yii::t('yii', '{attribute} is not a valid URL.'); $message = strtr($message, array('{attribute}' => $object->getAttributeLabel($attribute))); if (strpos($this->pattern, '{schemes}') !== false) { $pattern = str_replace('{schemes}', '(' . implode('|', $this->validSchemes) . ')', $this->pattern); } else { $pattern = $this->pattern; } $js = "\nif(!value.match({$pattern})) {\n\tmessages.push(" . CJSON::encode($message) . ");\n}\n"; if ($this->defaultScheme !== null) { $js = "\nif(!value.match(/:\\/\\//)) {\n\tvalue=" . CJSON::encode($this->defaultScheme) . "+'://'+value;\n}\n{$js}\n"; } if ($this->allowEmpty) { $js = "\nif(\$.trim(value)!='') {\n\t{$js}\n}\n"; } return $js; }
/** * Validates a single attribute. * This method should be overridden by child classes. * @param CModel $object the data object being validated * @param string $attribute the name of the attribute to be validated. */ protected function validateAttribute($object, $attribute) { if ($object->{$this->needFileMethodName}() && empty($object->{$attribute})) { $object->addError($attribute, "Необходимо заполнить поле {$object->getAttributeLabel($attribute)}"); } }
/** * Generates an active form row. * @param string $type the input type. * @param CModel $model the data model. * @param string $attribute the attribute. * @param array $htmlOptions additional HTML attributes. * @param array $data data for multiple select inputs. * @return string the generated control group. */ public static function activeControlGroup($type, $model, $attribute, $htmlOptions = array(), $data = array()) { $label = self::popOption('label', $htmlOptions); $color = self::popOption('color', $htmlOptions); $controlGroupOptions = self::popOption('groupOptions', $htmlOptions, array()); $labelOptions = self::popOption('labelOptions', $htmlOptions, array()); $controlOptions = self::popOption('controlOptions', $htmlOptions, array()); if (in_array($type, array(self::INPUT_TYPE_CHECKBOX, self::INPUT_TYPE_RADIOBUTTON))) { $htmlOptions = self::defaultOption('label', $model->getAttributeLabel($attribute), $htmlOptions); $htmlOptions['labelOptions'] = $labelOptions; $label = false; } $help = self::popOption('help', $htmlOptions, ''); $helpOptions = self::popOption('helpOptions', $htmlOptions, array()); if (!empty($help)) { $help = self::inputHelp($help, $helpOptions); } $error = self::popOption('error', $htmlOptions, ''); $input = self::createActiveInput($type, $model, $attribute, $htmlOptions, $data); $controlGroupOptions = self::addClassName('control-group', $controlGroupOptions); if (!empty($color)) { $controlGroupOptions = self::addClassName($color, $controlGroupOptions); } $labelOptions = self::addClassName('control-label', $labelOptions); ob_start(); echo self::openTag('div', $controlGroupOptions); if ($label !== false) { echo CHtml::activeLabelEx($model, $attribute, $labelOptions); } echo self::controls($input . $error . $help, $controlOptions); echo '</div>'; return ob_get_clean(); }