Problems by validating models in Controller

Hi guys,

background of following problem is how to handle attributes of models which are both in required rules of model and unkown to be placed in formular( Example:

I want to create a person, but I don’t know telefon number of this person. Both models are handled in same formular using dynamic forms)

Validating models in Controller will fail, 'cause attributes, which are in required rules are unknown. So I have found two options as yet

[list=1][*][size="2"]Taking off attribute from required rules and validate attrbiutes in Controller like this [/size][/list]

    private function Validating($model_telefon) {

        foreach ($model_telefon as $number) {

            if ((!empty($number->vorwahl) || !empty($number->telefonnummer)) && (empty($number->id_kontakt_verwendungszweck) || empty($number->id_laenderkennung))) {



                    'type' => Alert::TYPE_DANGER,

                    'title' => 'Eingabefehler',

                    'icon' => 'glyphicon glyphicon-remove-sign',

                    'body' => 'Eine Telefonnummer benötigt immer einen Verwendungszweck und eine Länderkennung....!',

                    'showSeparator' => true,

                    'delay' => false


                return true;



        return false;


2. Creating dummy table and allocate values of dummy table to origin table. So, I don't have to remove atrtributes   			from required-rules, but this entails a lot of effort( creating models, additional coding etc..etc..)
  1. I remove validating model in Controller and just create dummy attributes in origin model to be allocated in Controller. That’s worst practice, I know. So, it’s actual no alternative!

Because project leader wants to have attribute in required rules, I’m looking for more options. I’d be glad getting more options of programmers sophisticated

Many thanks in advance…

// in formular

 <?= $form->field($value, "[{$i}]dummyNummer")->textInput(['placeholder' => 'Bitte hier die Telefonnummer eingeben'])


// in Controller

$valid = Model::validateMultiple($model_telefon);

if($valid){ // this will be false as long  value being in required rules will be written into database using an allocation



$model_telefon->Nummer=$model_telefon->dummyNummer; // allocation




You have a required attribute, which basically is NOT required?

Than why have it as required attribute in the first place? ???

Or is there a scenario where the telephone number IS required and another scenario where it is not required?

Than I would suggest using exactly that: Scenarios.

Read about validation scenarios here:

Or is the telephone input field "dynamically added"? Means:

It is not present in the form?

but the user can "add" the field and if the field is present it should also be required?

Maybe I have not fully understood the problem?

In this case please explain it a littlebit more clear. :)

Best Regards

Okay, I will try to explain problem more extensively:

I have formular which represents records of 4 different models.Records of two are created dynamically using wbraganca/yii2-dynamicform-widget. As long as attributes are in required rules, user has to fill in values(see attachement formular.jpg).

But what if user doesn’t know vlaues to be filled in?

In this case, required rules are not needed. But this model will also be soliticed from another module in application, where these attributes are needed and where user knows values. In this case, required rules are needed.

As it seems, scenarios could solve my problem, but I don’t know, if scenarios will solve my problem in case of dynamic using as described( see attachement)

What do U think?

In hope of having described problem better, I gonna waiting of feedback what to do…

The problem has itself taken.

As I think, defining attributes as ‘safe’ wolud solve problem,too, my chief seems to prefer using scenarios, so I will implement them.

This thread can be solved as succesfully solved.

A special thank you to MetaCrawler