Custom Rule work only after validate

Hi, I don’t understand why, after I see many different example of custom rule, I can’t write a custom rule that work in the right time…
I mean, if i write a custom rule, my rule work… but work only after you have compiled correctly all the standard rule…

if you have A, B, as required… and C as required when… or with other custom function…
when i press commit button on active form, red alert will be marked only on A and B input…
if you write A and B input… and press commit… NOW and only now, you can see C with red alert mark…

I don’t know if you mean my english :slight_smile:

Could you show us your code?
What do you have in your rules method and what do you do in your custom validator?

thanks for your help…
in rule i write different type of custom validation… for “durata_appalto”

i try this:
[‘durata_appalto’, ‘required’, ‘when’ => “function (attribute, value) {
return true;}”, ‘enableClientValidation’ => false]
this:
[‘durata_appalto’, ‘required’, ‘when’ => true, ‘enableClientValidation’ => false]
this:
[‘durata_appalto’,‘required’,‘when’=>“verifica_durata”,‘enableClientValidation’ => false],

with
public function verifica_durata($attribute,$params,$validator){
return true;
}

i tried too:
[‘durata_appalto’,‘verifica_durata’],

with

public function verifica_durata($attribute,$params,$validator){
           $this->addError($attribute, 'durata appalto non può  vuoto');
}

without success… they work only after the “verified basic rules”.

have you guessed what I mean?

So, “durata_appalto” is “C” in your first post, OK?

  1. What do you have for “A” and “B”. I want to see the whole rules including A, B and C.
  2. What do you try to do with “when”? What condition do you want to set for validating “C”?

Guide > Validating Input > Conditional Validation
https://www.yiiframework.com/doc/guide/2.0/en/input-validation#conditional-validation

I see that examples, i try it after i write here, but the problem is the same…
here a complete example:

Controller (a basic create function)


public function actionCreate()
{
$model = new Test();

    if ($model->load(Yii::$app->request->post()))
    {
        if($model->validate()){
            $model->save();
            return $this->redirect(['view', 'id' => $model->name]);
        }
    }

    return $this->render('create', [
        'model' => $model,
    ]);
}

model


public function rules()
{
return [
[[‘name’, ‘email’], ‘required’],
[[‘name’, ‘email’], ‘string’, ‘max’ => 40],
[[‘tel’], ‘string’, ‘max’ => 15],
[[‘name’], ‘unique’],
[‘tel’,‘required’,‘when’=> function($model) {
return $model->name == ‘alex’;
},‘enableClientValidation’ => false]
];

}

and basic form:


<?php $form = ActiveForm::begin(); ?>

<?= $form->field($model, 'name')->textInput(['maxlength' => true]) ?>

<?= $form->field($model, 'email')->textInput(['maxlength' => true]) ?>

<?= $form->field($model, 'tel')->textInput(['maxlength' => true]) ?>

in that way, I see that problem… only after I write correctly name and email, validator for tel go right. here and example live: https://www.studiomedicogurrieri.it/test/create

I see. I’ve tested your example and confirmed the issue.

I think that disabling client-side validation for “name” and “email” will solve the problem.

in what way? i used ‘enableClientValidation’ false in the model
can you told me the right metod,please?

Yo can set enableClientValidation as an option of the form.

$form = ActiveForm::begin([
    'options' => ['enableClientValidation' => false],
]) ?>
    <?= $form->field($model, 'name') ?>
    <?= $form->field($model, 'email') ?>
    ...

Guide > Getting Data from Users > Creating Forms > ActiveRecord based forms: ActiveForm
https://www.yiiframework.com/doc/guide/2.0/en/input-forms#activerecord-based-forms-activeform

Note that you have not set 'enableClientValidation' => false to ‘name’ and ‘email’ in the model rules.

public function rules()
{
    return [
        [[‘name’, ‘email’], ‘required’],
        [[‘name’, ‘email’], ‘string’, ‘max’ => 40],
        [[‘tel’], ‘string’, ‘max’ => 15],
        [[‘name’], ‘unique’],
        [‘tel’,‘required’,‘when’=> function($model) {
                return $model->name == ‘alex’;
            },
            'enableClientValidation’ => false
        ]
    ];
}

In the above, 'enableClientValidation' => false is applied only to the last rule.

Also note that you could write whenClient option to enable the client validation for ‘tel’.
Then you don’t need to disable it in the form.
As you see is the guide:

public function rules()
{
    return [
        [[‘name’, ‘email’], ‘required’],
        [[‘name’, ‘email’], ‘string’, ‘max’ => 40],
        [[‘tel’], ‘string’, ‘max’ => 15],
        [[‘name’], ‘unique’],
        [‘tel’,‘required’,‘when’=> function($model) {
                return $model->name == alex’;
            },
            'whenClient’ => "function (attribute, value) {
                return $('#name').val() == 'alex';
            }"
        ]
    ];
}

https://www.yiiframework.com/doc/guide/2.0/en/input-validation#conditional-validation

Great !!
It works !!finally I understand errors, i hope xD
So, I’ll try to read the guide better, obviously I had misunderstood some passage.
very very thanks!

1 Like