Reputation: 2139
I have a form in my web app which written in php language under yii2 framework.
now i have designed my own form with custom css and i don't want to show errors under each input.
I have canceled error showing with fieldconfig
in yii2 form.
But it is a bug for a system that doesn't show error of inputs to the users.
I want to show errors in another div or in an alert box.
how should i do this?
here is my form
<?php
$form = ActiveForm::begin([
'enableClientValidation' => true,
fieldConfig' => ['template' => "{label}\n{input}"]
])
?>
<?= $form->field($regModel, 'name')->textInput(['maxlength' => true, 'class' => 'txtbox_name', 'placeholder' => 'نام'])->label('') ?>
<?= $form->field($regModel, 'family')->textInput(['maxlength' => true, 'class' => 'txtbox_name', 'placeholder' => 'نام خانوادگی'])->label('') ?>
<?= $form->field($regModel, 'email')->textInput(['maxlength' => true, 'class' => 'txtboxpass', 'placeholder' => 'پست الکترونیک'])->label('') ?>
<?= $form->field($regModel, 'password')->passwordInput(['maxlength' => true, 'class' => 'txtboxpass', 'placeholder' => 'رمز عبور'])->label('') ?>
<?= $form->field($regModel, 'password')->passwordInput(['maxlength' => true, 'class' => 'txtboxpass', 'placeholder' => 'رمز عبور'])->label('') ?>
<?= $form->field($regModel, 'grade_id')->dropDownList(\yii\helpers\ArrayHelper::map(\app\models\Grade::find()->all(), 'id', 'name'), ['prompt' => 'انتخاب مقطع', 'class' => 'drglist'])->label(''); ?>
<div class="form-group">
<?= Html::submitButton('ثبت نام', ['class' => 'btn_sabt', 'id' => 'reg']) ?>
</div>
<?php ActiveForm::end(); ?>
please help me it is so necessary/ thank you
Upvotes: 1
Views: 4689
Reputation: 1
You can get the errors using 'afterValidate';
Put this code at the end of the view;
'#w0' is the form id element (you can get it using inspect from your browser);
$script = <<< JS
$('#w0').on('afterValidate', function (event, messages, errorAttributes) {
//console.log('afterValidate');
if (errorAttributes.length) {
//alert('Verifique os errors!');
}
$.each(messages, function(index, element) {
if(element.length > 0) {
console.log(element);
alert(element);
}
});
});
JS;
$this->registerJs($script);
Upvotes: 0
Reputation: 846
<?= $form->errorSummary($regModel) ?>
Option#1: Rather than using the activeform, try the HTML helper class:
use yii\helper\Html;
<?= Html::input('text', 'username', $user->name, ['class' => $username]) ?>
Option#2: You can try at the activeField level:
// With 'default' layout you would use 'template' to size a specific field:
echo $form->field($model, 'demo', [
'template' => '{label}
<div class="row">
<div class="col-sm-4">{input}{error}{hint}</div>
</div>'
]);
Option#3: Set at the activeForm level, verify elements using the inspect debug tool in Chrome or Mozilla:
<?php $form = ActiveForm::begin([
'options' => ['class' => 'form-horizontal'],
'fieldConfig' => [
'template' => "{label}\n{input}\n{hint}",
//'errorOptions' => ['class' => 'help-block'],
],
]); ?>
Reference:
Upvotes: 4
Reputation: 22174
You may use Html::errorSummary()
to display errors summary in any place:
<?= Html::errorSummary($regModel) ?>
Upvotes: 0