I Posted one question yesterday regarding custom validation for radio button Textfield Mandatory On Radio Button. I got answer. But, that was not exact answer. But, it solve
Atlast, i got my answer
//company_name
['company_name', 'required', 'when' => function($model){
return ($model->user_type == 'Firm' ? true : false);
}, 'whenClient' => "function (attribute, value) {
return $('input[type=\"radio\"][name=\"Users[user_type]\"]:checked').val() == 'Firm';
}"],
Actually, your action method is incorrect. When you call refresh(), you basically reload the page. If the model has not been updated in the database, you will see nothing special, and no errors since you reload a fresh model.
This code will display the error as usual in your view :
/**
* @return \yii\web\Response
*/
public function actionRegister()
{
// Register Model
$model = new RegisterForm();
if ($model->load(Yii::$app->request->post()))
{
$post = Yii::$app->request->post('RegisterForm');
if ($model->validate())
{
// whatever
}
// return $this->refresh(); // do not refresh but...
}
// ... render the view with the current model, who's errors attribute is filled
return $this->render('register', compact('model'));
}
NB : also, you don't have to call errors() in your view, the ActiveFormField rendering method takes care of it for you :
<?= $form->field($model, 'CompanyName')->textInput()->label('Company Name') ?>
is enough
@Nana Partykar
As you asked how the ajax validation can be disabled for the captcha i tried this way. But i am not sure if this is the correct way.
I have set to the the form with this settings
'enableAjaxValidation' => true,
'enableClientValidation' => false,
'validateOnSubmit' => false,
and changed the validation on the action to this (i removed my captcha in my case 'verifyCode' from the attributes which should be validated.
if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())) {
Yii::$app->response->format = 'json';
$validateAttributes = $model->activeAttributes();
$key = array_search('verifyCode', $validateAttributes);
if ($key!==false) {
array_splice($validateAttributes, $key, 1);
}
return \yii\widgets\ActiveForm::validate($model, $validateAttributes);
}
You should to add 'enableAjaxValidation' => true
in ActiveForm
.
After do this your code should be,
controller
public function actionRegister()
{
$model = new RegisterForm();
if(Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())) {
\Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
return \yii\widgets\ActiveForm::validate($model);
}
if ($model->load(Yii::$app->request->post()))
{
}
else
{
// HERE YOU CAN PRINT THE ERRORS OF MODEL
echo "<pre>";
print_r($model->getErrors());
echo "</pre>";
}
return $this->refresh();
}
View
<?php $form = ActiveForm::begin([
'id' => 'register-form',
'enableAjaxValidation' => true,
]); ?>
.
.
.
<?= $form->field($model, 'AdminType')
->radioList(array('Individual'=>'An Individual', 'Firm'=>'Firm/Company'))
->label('Are You')?>
<?= $form->field($model, 'CompanyName')->textInput()->label('Company Name')->error() ?>
.
.
<?php ActiveForm::end(); ?>
It may help you.
If you call error
without options you supress error output
ActiveField error
method
yii\widgets\ActiveField
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;
}
Your have to remove call error
method into VIEW
<?= $form->field($model, 'CompanyName')->label('Company Name')->textInput() ?>