Пример #1
0
    public function input($type, $model, $objProp, $options = [], $list = [])
    {
        $modelRules = $model->rules();
        $inpRules = isset($modelRules[$objProp]) ? $modelRules[$objProp] : [];
        if ('textarea' === $type) {
            $tag = 'textarea';
            $inp = sprintf('<textarea name="%s[%s]" value="%s" ', $model->getClassName(false), $objProp, $model->{$objProp});
        } else {
            if ('checkboxList' === $type) {
                foreach ($list as $item) {
                }
            } else {
                $tag = 'input';
                $inp = sprintf('<input type="%s" name="%s[%s]" value="%s" ', $type, $model->getClassName(false), $objProp, 'password' !== $type ? $model->{$objProp} : '');
            }
        }
        $attrs = isset($options['attrs']) ? $options['attrs'] : [];
        foreach ($attrs as $prop => $value) {
            $inp .= sprintf('%s="%s" ', $prop, $value);
        }
        if (!isset($attrs['id'])) {
            $inputId = $model->getSimpleClassName() . '-' . \components\helpers\StringHelper::slugify($objProp);
        } else {
            $inputId = $attrs['id'];
        }
        $inp .= " id=\"{$inputId}\">";
        if ('textarea' === $tag) {
            $inp .= "</textarea>";
        }
        $template = isset($options['template']) ? $options['template'] : (isset($this->options['template']) ? $this->options['template'] : $this->defaultTemplate);
        $result = str_replace('{input}', $inp, $template);
        $labels = $model->getAttributeLabels();
        $result = str_replace('{label}', !empty($labels[$objProp]) ? $labels[$objProp] : '', $result);
        $errorSpan = sprintf('<span id="%d-error">%s</span>', $inputId, $model->getError($objProp));
        echo str_replace('{error}', $errorSpan, $result);
        $errMsg = isset($inpRules['message']) ? $inpRules['message'] : "{$objProp} is required.";
        $clientValidation = isset($inpRules['clientValidator']) ? sprintf("(%s)(%s)", $inpRules['clientValidator'](), '$("#' . $inputId . '").val()') : "(function() {return false;})()";
        $this->jsValidators[] = <<<JS
                if ({$clientValidation}) {
                    showError(\$("#{$inputId}-error"), '{$errMsg}');

                    return false;
                } else {
                    hideError(\$("#{$inputId}-error"));
                }
JS;
        $this->jsOnChangeValidators[] = <<<JS
            \$("#{$inputId}").on('change', function() {
                if ({$clientValidation}) {
                    showError(\$("#{$inputId}-error"), '{$errMsg}');

                    return false;
                } else {
                    hideError(\$("#{$inputId}-error"));
                }
            });
JS;
        return $this;
    }
Пример #2
0
 public function getSimpleClassName()
 {
     return \components\helpers\StringHelper::slugify($this->getClassName(false));
 }