yii2-bootstrap4: ActiveForm: validation error not showing with Bootstrap 4 when using custom input template

The issue is related to the extension yiisoft/yii2-bootstrap4 but I think it is due to a compatibility issue of yiisoft/yii2

What steps will reproduce the problem?

Model

class TestForm extends \yii\base\Model
{
    public $demo;
    public function rules()
    {
        return [
            // define validation rules here
            [['demo'], 'required'],
            [['demo'], 'string', 'length' => [2, 4]],
        ];
    }
}

View The input error is manually separated from the input field (for example, the error is needed at the top of the form during ajax validation)

<?php $form = ActiveForm::begin(['id' => 'test-form',
'options' => [
	'class' => 'form-horizontal'],
	'enableAjaxValidation' => true,
	'enableClientValidation' => false,
]) 
?>

<?= $form->errorSummary($model) ?>

<div class="form-row"> 
<div class="form-group col-md-3">
	<?php echo $form->field($model, 'demo', ['template' => "{error}"]); ?>
</div>
</div>

<div class="form-row"> 
<div class="form-group col-md-3">
	<?= $form->field($model, 'demo', ['errorOptions' => ['tag' => false]])->textInput()->label("Demo") ?>
</div>
</div> 

Controller

    public function actionTesterror()
    {
        $model = new TestForm();

        // Ajax validation
        if (Yii::$app->request->isAjax){
            $model->load(Yii::$app->request->post());
            Yii::$app->response->format = Response::FORMAT_JSON;
            $ret = ActiveForm::validate($model);
            Yii::info("Test Error Visualization: " . json_encode($ret));
            return $ret;
        }    
        return $this->render('testerror', ['model' => $model]);
    }

What is the expected result?

The error message Demo cannot be blank is visible.

What do you get instead?

The error message Demo cannot be blank is not visible.

Additional info

The issue is related to how the visualization of the error messages are handled in Bootstrap 4.

The {error} of the input demo should be displayed in a <div> which does not contain the actual input tag.

In Bootstrap 4 this creates a visualization issue due to the fact that the validation errors are set with display:block or display:none depending on the class of a sibling tag (i.e. the input tag). The css code contains in fact the “sibling combinator” ~.

.was-validated :invalid ~ .invalid-feedback,
.was-validated :invalid ~ .invalid-tooltip,
.is-invalid ~ .invalid-feedback,
.is-invalid ~ .invalid-tooltip {
  display: block;
}

Working solution

Two additional lines can be added to the function updateInput in yii.activeForm.js, in order to force the setting of "display": "block":

// Patch: show errors when input {error} is not in the default position
$(attribute.container).children(attribute.error).css({"display": "block"});
// ...
// Patch: hide errors when input {error} is not in the default position
$(attribute.container).children(attribute.error).css({"display": "none"});

I have the modified version of yii.activeForm.js in the branch “custom” of my fork david-it/yii2-framework

Q A
Yii version 2.0.32
yii2-bootstrap4 2.0.8.0
PHP version 7.3.14
Operating system Raspbian Buster

About this issue

  • Original URL
  • State: open
  • Created 7 years ago
  • Comments: 15 (6 by maintainers)

Most upvoted comments

the same problem with bootstrap5