модель
Код: Выделить всё
<?php
namespace frontend\models;
use Yii;
use yii\base\Model;
/**
* ContactForm is the model behind the contact form.
*/
class Calculations extends Model
{
public $a;
public $b;
public $sum;
/**
* @inheritdoc
*/
public function rules()
{
return [
// name, email, subject and body are required
[['a', 'b'], 'required'],
[['a', 'b'], 'integer'],
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'a' => 'Цифра А',
'b' => 'Цифра Б'
];
}
}
Код: Выделить всё
public function actionCalc()
{
$model = new Calculations();
if ($model->load(Yii::$app->request->post()) && $model->validate())
{
$model->a + $model->b = $model->sum;
return $this->$model->sum;
//return $this->refresh();
}
else {
return $this->render('calc', [
'model' => $model,
//'sum' => $sum,
]);
}
}
Код: Выделить всё
<?php
/* @var $this yii\web\View */
/* @var $form yii\bootstrap\ActiveForm */
/* @var $model \frontend\models\ContactForm */
use yii\helpers\Html;
use yii\bootstrap\ActiveForm;
$this->title = 'Калькулятор';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="site-contact">
<h1><?= Html::encode($this->title) ?></h1>
<p>
If you have business inquiries or other questions, please fill out the following form to contact us. Thank you.
</p>
<div class="row">
<div class="col-lg-5">
<?php $form = ActiveForm::begin(['id' => 'calc-form']); ?>
<?= $form->field($model, 'a')->textInput(['autofocus' => true]) ?>
<?= $form->field($model, 'b') ?>
<?= $form->field($model, 'sum') ?>
<div class="form-group">
<?= Html::submitButton('Расчитать', ['class' => 'btn btn-primary', 'name' => 'calc-button']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>
</div>
</div>