S
S
Sergey2015-08-08 13:47:13
Yii
Sergey, 2015-08-08 13:47:13

Why is the controller not seeing form data in yii2?

The logic is this application: you need to make a confirmation by entering a certain number in the form field that comes already with the data. There is a controller

public function actionCreate()
    {
        
         $model = new Transactions();

if ($model->load(Yii::$app->request->post()) && $this->applyOperation($model)) {
               $model->save();
            return $this->redirect(['view', 'id' => $model->id]);      
            } else {
                return $this->render('create', [
                    'model' => $model,
                ]);
            }

Another controller with operations (here is the problem)
private function applyOperation($model) {
 $code = $model->code;
        $password = 123;
if($password == $code ){
   return $this->redirect(['trans']);
}

the form
<?php $form = ActiveForm::begin(); ?>

<?= Html::activeTextInput($model, 'account', [ 'value' => "$acc",'readonly' => 'readonly']) ?>
<?= Html::activeTextInput($model, 'sum', [  'value' => "$su",'readonly' => 'readonly']) ?>
<?= Html::activeTextInput($model, 'description', [ 'value' => "$desc",'readonly' => 'readonly']) ?>
<?= Html::activeTextInput($model, 'sender', [ 'value' => "$se",'readonly' => 'readonly']) ?>

<?= $form->field($model, 'code')->textInput() ?>
<div class="form-group">
        <?= Html::submitButton($model->isNewRecord ? Yii::t('frontend', 'Fill up balance') : Yii::t('frontend', 'Update'), ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
    </div>

    <?php ActiveForm::end(); ?>

The problem is that in the applyOperation() method $code = $model->code; for some reason is equal to NULL, although the data is entered and the redirect does not go through.
Can't figure out what's the problem

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Timofeev, 2015-08-08
@webinar

Try like this:

public function actionCreate()
    {
        
         $model = new Transactions();

if ($model->load(Yii::$app->request->post()) {
$this->applyOperation($model);
               $model->save();
            return $this->redirect(['view', 'id' => $model->id]);      
            } else {
                return $this->render('create', [
                    'model' => $model,
                ]);
            }

and applyOperation like so:
private function applyOperation($model) {
 $code = $model->code;
        $password = 123;
if($password == $code ){
   return $this->redirect(['trans']);
}else{
return true;
}

Yes, and you have an extra parenthesis there before &&

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question