-
-
Notifications
You must be signed in to change notification settings - Fork 106
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ActiveForm: validation error not showing with Bootstrap 4 when using custom input template #214
Comments
Have you setup up properly ActiveForm with BS4 horizontal layout with proper namespace? use yii\bootstrap4\ActiveForm;
$form = ActiveForm::begin([
'layout' => 'horizontal',
'fieldConfig' => [
'template' => "{label}\n{beginWrapper}\n{input}\n{hint}\n{error}\n{endWrapper}",
'horizontalCssClasses' => [
'label' => 'col-sm-4 text-right',
'offset' => 'col-sm-offset-4',
'wrapper' => 'col-sm-7',
'error' => '',
'hint' => '',
],
],
]);
...
<?= $form->field($model, 'myAttribute')->textInput() ?> // works OK with BS3 & BS4
...
<?php ActiveForm::end() ?> See documentation. |
I'm afraid that your example is quite different from what I'm trying to point out. To make it more clear, this works perfect with bootstrap 4:
This does not work with Bootstrap 4:
As a use case, you may want to show the error somewhere far away from the input. For example at the top of the form. |
I think to show the error(s) on top of the form, you should use |
During ajax validation the In any case, this does not explain why the code above is not supposed to work. Since there is the possibility to separate the I know many things are different but in Yii1 there is a very similar way to separate the error messages and it works right away. |
Well, if you isolate the error (by changing
|
Hello. I don't know if my issue is directly related with this, but I think it can be. The issue is, if I use yii\widgets\ActiveForm works ok, but If I use yii\bootstrap4\ActiveForm, the errors not showing up.
Also, I tried with 'enableClientValidation' => false ... the form is submitted but when it comes back with errors, these not showing up either. Any ideas? Thanks |
No. I guess further debugging is needed in order to come up with the reason for it. |
How I can help? |
|
Hello. After a debug session, I think "form.yiiActiveForm" is not the problem ... really, it nevers called, because "beforeSubmit" is not called eather. Form: $form = ActiveForm::begin([
'id' => 'orden-items-create-form-id',
'enableClientValidation' => false,
'enableAjaxValidation' => true,
'validationUrl' => ['/orden-items/validate-create-from-orden', 'orden_id' => $orden_id],
'action' => ['/orden-items/create-from-orden', 'orden_id' => $orden_id],
]); The controller: public function actionValidateCreateFromOrden($orden_id)
{
$model = new OrdenItems(['scenario' => 'insert']);
$model->estado_c = 'A';
$model->orden_id = $orden_id;
if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())) {
Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
return \yii\bootstrap4\ActiveForm::validate($model);
// return \yii\widgets\ActiveForm::validate($model);
}
} (here I tested with boostrap4 and widgets validate, but the result its the same). In the form when I use yii\widgets\ActiveForm works ok, but when I use yii\bootstrap4\ActiveForm, nothing happens (no error is shown). Any ideas? |
It is in HTML in both cases, right? If so, that's CSS issue. There's basically no class for it. |
I don't understand ... The css is the same ... why is working ok with yii\widgets\ActiveForm but not with yii\bootstrap4\ActiveForm? |
the same problem with bootstrap5 |
I had the same issue but when I've added a CSS class tweak to my site's CSS then I was able to see errors in my Boostrap 5 form. .invalid-feedback { For some odd reason .invalid-feedback class in _forms.scss has display: none. |
Hi. Just created a library to solve problems like this one |
The issue is related to the extension
yiisoft/yii2-bootstrap4
but I think it is due to a compatibility issue ofyiisoft/yii2
What steps will reproduce the problem?
Model
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)
Controller
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
ordisplay:none
depending on the class of a sibling tag (i.e. the input tag). The css code contains in fact the “sibling combinator”~
.Working solution
Two additional lines can be added to the function
updateInput
inyii.activeForm.js
, in order to force the setting of"display": "block"
:I have the modified version of
yii.activeForm.js
in the branch "custom" of my fork david-it/yii2-frameworkThe text was updated successfully, but these errors were encountered: