Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
MacGyer committed Jun 23, 2018
2 parents 9bc0fa7 + 81169a9 commit 5acd7a3
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 2 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"role": "Developer"
}
],
"version": "3.0.0-RC",
"version": "3.0.1-RC",
"require": {
"php": ">=5.6.0",
"yiisoft/yii2": "~2.0.0",
Expand Down
16 changes: 16 additions & 0 deletions src/lib/Html.php
Original file line number Diff line number Diff line change
Expand Up @@ -539,4 +539,20 @@ protected static function booleanInput($type, $name, $checked = false, $options

return $hidden . static::input($type, $name, $value, $options);
}

public static function error($model, $attribute, $options = [])
{
$attribute = static::getAttributeName($attribute);
$errorSource = ArrayHelper::remove($options, 'errorSource');
if ($errorSource !== null) {
$error = call_user_func($errorSource, $model, $attribute);
} else {
$error = $model->getFirstError($attribute);
}
$tag = ArrayHelper::remove($options, 'tag', 'span');
$encode = ArrayHelper::remove($options, 'encode', true);

$options['data-error'] = $error;
return Html::tag($tag, $encode ? Html::encode($error) : $error, $options);
}
}
98 changes: 97 additions & 1 deletion src/widgets/form/ActiveField.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use macgyer\yii2materializecss\widgets\Icon;
use yii\helpers\ArrayHelper;
use yii\helpers\Json;
use yii\web\JsExpression;

// TODO: file input

Expand Down Expand Up @@ -67,7 +68,7 @@ class ActiveField extends \yii\widgets\ActiveField
*
* @see [\yii\helpers\Html::renderTagAttributes()](http://www.yiiframework.com/doc-2.0/yii-helpers-basehtml.html#renderTagAttributes()-detail) for details on how attributes are being rendered.
*/
public $errorOptions = ['class' => 'help-block'];
public $errorOptions = ['class' => 'help-block helper-text', 'tag' => 'span'];

/**
* @var array the default options for the label tags. The parameter passed to [label()](http://www.yiiframework.com/doc-2.0/yii-widgets-activefield.html#label()-detail) will be
Expand Down Expand Up @@ -123,6 +124,10 @@ public function init()
Html::addCssClass($this->inputOptions, ['inputValidation' => 'validate']);
}

if ($this->model->hasErrors()) {
Html::addCssClass($this->inputOptions, $this->form->errorCssClass);
}

if ($this->showCharacterCounter === true) {
$this->inputOptions['showCharacterCounter'] = true;
}
Expand Down Expand Up @@ -696,4 +701,95 @@ public function weekInput($options = [])

return parent::input('week', $options);
}

/**
* @inheritdoc
*/
public function error($options = [])
{
if ($options === false) {
$this->parts['{error}'] = '';
return $this;
}
$options = array_merge($this->errorOptions, $options);
$this->parts['{error}'] = Html::error($this->model, $this->attribute, $options);

return $this;
}

/**
* Returns the JS options for the field.
* @return array the JS options.
*/
protected function getClientOptions()
{
$attribute = Html::getAttributeName($this->attribute);
if (!in_array($attribute, $this->model->activeAttributes(), true)) {
return [];
}

$clientValidation = $this->isClientValidationEnabled();
$ajaxValidation = $this->isAjaxValidationEnabled();

if ($clientValidation) {
$validators = [];
foreach ($this->model->getActiveValidators($attribute) as $validator) {
/* @var $validator \yii\validators\Validator */
$js = $validator->clientValidateAttribute($this->model, $attribute, $this->form->getView());
if ($validator->enableClientValidation && $js != '') {
if ($validator->whenClient !== null) {
$js = "if (({$validator->whenClient})(attribute, value)) { $js }";
}
$validators[] = $js;
}
}
}

if (!$ajaxValidation && (!$clientValidation || empty($validators))) {
return [];
}

$options = [];

$inputID = $this->getInputId();
$options['id'] = Html::getInputId($this->model, $this->attribute);
$options['name'] = $this->attribute;

$options['container'] = isset($this->selectors['container']) ? $this->selectors['container'] : ".field-$inputID";
$options['input'] = isset($this->selectors['input']) ? $this->selectors['input'] : "#$inputID";
if (isset($this->selectors['error'])) {
$options['error'] = $this->selectors['error'];
} elseif (isset($this->errorOptions['class'])) {
$options['error'] = '.' . implode('.', preg_split('/\s+/', $this->errorOptions['class'], -1, PREG_SPLIT_NO_EMPTY));
} else {
$options['error'] = isset($this->errorOptions['tag']) ? $this->errorOptions['tag'] : 'span';
}

$options['encodeError'] = !isset($this->errorOptions['encode']) || $this->errorOptions['encode'];
if ($ajaxValidation) {
$options['enableAjaxValidation'] = true;
}
foreach (['validateOnChange', 'validateOnBlur', 'validateOnType', 'validationDelay'] as $name) {
$options[$name] = $this->$name === null ? $this->form->$name : $this->$name;
}

if (!empty($validators)) {
$options['validate'] = new JsExpression("function (attribute, value, messages, deferred, \$form) {" . implode('', $validators) . "}");
}

if ($this->addAriaAttributes === false) {
$options['updateAriaInvalid'] = false;
}

// only get the options that are different from the default ones (set in yii.activeForm.js)
return array_diff_assoc($options, [
'validateOnChange' => true,
'validateOnBlur' => true,
'validateOnType' => false,
'validationDelay' => 500,
'encodeError' => true,
'error' => '.help-block helper-text',
'updateAriaInvalid' => true,
]);
}
}
34 changes: 34 additions & 0 deletions src/widgets/form/ActiveForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,22 @@ class ActiveForm extends \yii\widgets\ActiveForm
*/
public function init()
{
if (!isset($this->options['id'])) {
$this->options['id'] = $this->getId();
}

if (!isset($this->options['data-success-class'])) {
$this->options['data-success-class'] = $this->successCssClass;
}

if (!isset($this->options['data-error-class'])) {
$this->options['data-error-class'] = $this->errorCssClass;
}

if ($this->enableClientValidation) {
$this->registerAfterValidateHandler();
}

parent::init();
}

Expand All @@ -51,4 +67,22 @@ public function field($model, $attribute, $options = [])
{
return parent::field($model, $attribute, $options);
}

/**
* Register the necessary JS handlers to set error messages and validation state indicators.
*/
protected function registerAfterValidateHandler()
{
$view = $this->getView();
$id = $this->options['id'];

$view->registerJs(<<<JS
$('#{$id}').on('afterValidateAttribute', function (evt, attribute, messages) {
var yiiForm = $(this);
$(attribute.container + ' ' + attribute.error).attr('data-error', messages[0]);
messages[0] ? $(attribute.input).addClass(yiiForm.attr('data-error-class')).removeClass(yiiForm.attr('data-success-class')) : $(attribute.input).addClass(yiiForm.attr('data-success-class')).removeClass(yiiForm.attr('data-error-class'));
});
JS
);
}
}

0 comments on commit 5acd7a3

Please sign in to comment.