Auto computation of two fields

Hi! I’m new to Yii. How can I auto-compute two fields in another field?

field 1 - field 2 = field 3

should I edit the Model? Controller ? or View?

If I input or type ‘3’ in numberfield1 and ‘2’ in numberfield2 the numberfield3 is equal to ‘1’; automatically computes and display (on change) the difference between the two numbers

if you mean in model then make a simple getter

class MyModel : \yii\base\Model
{
     public $field1;
    public $field;
    //.....
    public function getField3(){
        return "{$this->field1} {$this->field2}";
    }
}

then in the view

<?= $model->field3 ?>

1 Like