Validation required should depend on other model


Some fields on my model should only be required if a related field is true:

public function rules() { 

    return [

       	[['ao_anzahl', 'ao_preis', 'br_anzahl', 'br_preis', 'gl_anzahl', 'gl_preis', 'fa_anzahl', 'fa_preis'], 'required', 

            'when' => function ($model, $attribute) {

 		return $model->kunde->edvsystem->$attribute;



} // End of method.

So there are boolean fields in my edvsystem model with the same name, which should define if the value is required. Is this possible? In my view I managed it with this:

<?= $form->field($unternehmenszahlen[$letztes_jahr], "[$letztes_jahr]br_preis")->textInput(['readonly' => !$kunde->edvsystem->br_preis]); ?>


Sorry man but I do not get the question…

Generally speaking yes, making your own rule is possible…

But you made some code, so you already done it.

Now the question is: is the code working?

I guess no , I hope you tried it, so in which way is not working as you expect?

Sorry Roberto, you are right, my question was not clear enough.

return $model->kunde->edvsystem->$attribute;

The code doesn’t work… I don’t know how to get the name of the attribute beeing checked.


Did you defined all the relation in your main model so you can access to edvsystem?

I guess this rule should be used only in update because in insert for sure you do not have related data.

try to debug by steps, just few lines of hard debug…

function ($model, $attribute) {

echo "attribute: $attribute<br>";

echo "kunde<br>";


echo "kunde -- edvsystem<br>";


echo "kunde -- edvsystem --- $attribute<br>";



In this way you should see where it is failing.

Thanks for the debug help, now it works (the problem was in my controller, kunden_id was empty):


[['ao_anzahl', 'ao_preis', 'br_anzahl', 'br_preis', 'gl_anzahl', 'gl_preis', 'fa_anzahl', 'fa_preis'], 'required', 

'when' => function ($model, $attribute) {

	return $model->kunde->edvsystem->{$attribute};


but only when switching Javascript off :-[

I think because yii2 can’t generate a jquery-condition from it, but I don’t know how do it by myself with the whenClient condition??

It works now with javascript, although there might be a better solution.

In my view I put some hidden inputs:

<?= Html::hiddenInput('ao_anzahl', $kunde->edvsystem->ao_anzahl, ['id' => 'ao_anzahl']) ?>

<?= Html::hiddenInput('ao_preis', $kunde->edvsystem->ao_preis, ['id' => 'ao_preis']) ?>

<?= Html::hiddenInput('br_anzahl', $kunde->edvsystem->br_anzahl, ['id' => 'br_anzahl']) ?>

<?= Html::hiddenInput('br_preis', $kunde->edvsystem->br_preis, ['id' => 'br_preis']) ?>

<?= Html::hiddenInput('gl_anzahl', $kunde->edvsystem->gl_anzahl, ['id' => 'gl_anzahl']) ?>

<?= Html::hiddenInput('gl_preis', $kunde->edvsystem->gl_preis, ['id' => 'gl_preis']) ?>

<?= Html::hiddenInput('fa_anzahl', $kunde->edvsystem->fa_anzahl, ['id' => 'fa_anzahl']) ?>

<?= Html::hiddenInput('fa_preis', $kunde->edvsystem->fa_preis, ['id' => 'fa_preis']) ?>

And in the model

[['ao_anzahl', 'ao_preis', 'br_anzahl', 'br_preis', 'gl_anzahl', 'gl_preis', 'fa_anzahl', 'fa_preis'], 'required', 'when' => function ($model, $attribute) {

	return $model->kunde->edvsystem->{$attribute};

}, 'whenClient' => "function (attribute, value) {

	return $('#' + attribute +').val();