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))) {
?><?=
Alert::widget([
'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..)
- 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
$model_telefon->save();