A
A
Alexander Laikov2017-02-22 23:42:06
Yii
Alexander Laikov, 2017-02-22 23:42:06

How to accept the result of a dropDownList?

Good afternoon!
I am creating a dropdown list.

<?= $f->field($form, 'block')->dropDownList([
                        7 => '7 дней',
                        14 => '14 дней',
                        30 => '30 дней',
                        0 => 'Полная',
                   ])->label('Срок'); ?>

Then I look at the result
public function addBlock($user) {
    	var_dump($this->block);die;
}

In the answer I see NULL
In parallel, there is a text input and the answer comes from it.
The form model looks like this:
<?php

namespace app\models;

use Yii;
use yii\base\Model;

class BlockForm extends Model {

  public $block;
  public $reason;
  

  public function rules() {
        return [ 
        	['reason', 'required', 'message' => 'Укажите причину блокировки'],
        ];
    }

    public function addBlock($user) {
    	//$time = time() + 60*60*24*$this->block;
    	var_dump($this->block);
    	var_dump($this->reason);die;
    	/*$block = new Block();
    	$block->iduser = $user->id;
    	$block->who = Yii::$app->user->identity->id;
    	$block->reason = $this->reason;
    	$block->time = time();
    	if($this->block > 0){ $block->timeblock = $time; }
    	else { $block->timeblock = 0; }
    	return $block->save();*/
    }
}

In controller:
public function actionBlock() {
        $form = new BlockForm;
        $id = Yii::$app->request->get('id');
        $user = User::findOne($id);

        if($form->load(Yii::$app->request->post()) && $form->validate() && $form->addBlock($user)) {
            //var_dump($form); die;
            //return $this->redirect('/admin/user/blockall');
        }
        return $this->render('block', [
                'form' => $form,
                'id' => $id,
                'user' => $user,
            ]);
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
padlyuck, 2017-02-22
@Florens

public function rules() {
        return [ 
                ['block','safe'],//указать правило нужно
        	['reason', 'required', 'message' => 'Укажите причину блокировки'],
        ];
    }

properties not specified in the validation rules cannot be loaded via $model->load();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question