异步向控制器提交值? yii2

问题描述 投票:0回答:1

所以这是交易。下面的ActiveForm应该接收两个变量:$ tt(radio)和$ value(字符串数字输入)并将它们发送到控制器。

$ value需要根据InputField模型中定义的不同规则和方案进行验证。 $ tt是无线电值,所以我现在还没有为它定义规则。

诀窍是,我希望对$ value进行某种输入验证,并且它的规则应该根据用户设置的$ tt值而改变。有了这种配置,控制器永远不会收到$ tt,也不会收到$ value,我认为原因是它的规则是冲突的。

理想情况下,我希望我的视图将已检查的radio $ tt值(使用jQuery或smthng)发送到控制器,然后为控制器设置模型的场景,以便设置适当的验证规则,其中输入字段为$ value。视图将继续运行。

Yii2专家:1)你能解释一下为什么我的控制器没有收到任何价值。 2)建议我计划做什么?

`

//查看片段

       $valueForm = ActiveForm::begin([ 
              'id' => 'input-field', 
          'action' => ['value-search/index'], 
          'method' => 'post', 
         'options' => ['class' => 'form-horizontal'] ]); ?>

<?= $valueForm->field($valueModel, 'tt')
              ->radioList(['P_sat'=>'Sat. Pressure', 'T_sat'=>'Sat. Temperature'])
              ->label('Select table type'); ?> 
<?= $valueForm->field($valueModel, 'value')
              ->textInput(['options' => ['type'=>'number', 'name'=>'value','id'=>'value']]); ?> 
<?php ActiveForm::end() ?>

//模型

class InputField extends \yii\base\Model

{

const SCENARIO_PSAT = 'P_sat';
const SCENARIO_TSAT = 'T_sat';
public $tt;
public $value;


public function scenarios() {
    return [
        self::SCENARIO_PSAT => ['tt','value'],
        self::SCENARIO_TSAT => ['tt','value'],
    ];
}

public function rules() {
    return [
        ['tt','safe'],
        ['value', 'double', 'numberPattern' => '/[0-9]+?(\.[0-9]{0,5})?/', 'min' => 0, 'max' => 22064, 
        'on' => self::SCENARIO_PSAT],
        ['value', 'double', 'numberPattern' => '/[0-9]+?(\.[0-9]{0,5})?/', 'min' => 0, 'max' => 373.95, 
        'on' => self::SCENARIO_TSAT],
    ];
}

//来自控制器的操作动作

public function actionIndex()
{   
    $inputModel = new InputField;

    $inputModel->load(\Yii::$app->request->post());

    $tt = $inputModel->tt;


    switch($tt) {
        case 'P_sat': $inputModel->scenario = $inputModel::SCENARIO_PSAT;
                     break;
        case 'T_sat': $inputModel->scenario = $inputModel::SCENARIO_TSAT;
                     break;
        default: throw new UserException('setTableType is not returning shit');
    }


    if ($inputModel->validate('value')) { 
        $searchValue = $inputModel->value;
        $this->test = $inputModel->tt;
        $this->test2 = $inputModel->value;

    }  else {
        throw new UserException('Input is not validated');
    }

    return $this->render('index', [
        //'provider' => $provider,
        'array' => $this->test,
        'array2' => $this->test2
    ]);
}

`

测试和test2,它们应该从模型接收$ tt和$ value,都是NULL :(

php validation yii2 rules scenarios
1个回答
0
投票

必须在加载值之前设置场景

public function actionIndex()
{   
    $inputModel = new InputField;

    $inputModel->load(\Yii::$app->request->post());

    $tt = $inputModel->tt;


    switch($tt) {
        case 'P_sat': $inputModel->scenario = $inputModel::SCENARIO_PSAT;
                 break;
        case 'T_sat': $inputModel->scenario = $inputModel::SCENARIO_TSAT;
                 break;
        default: throw new UserException('setTableType is not returning shit');
    }


    if ($inputModel->load(\Yii::$app->request->post()) && $inputModel->validate('value')) { 
        $searchValue = $inputModel->value;
        $this->test = $inputModel->tt;
        $this->test2 = $inputModel->value;

    }  else {
        throw new UserException('Input is not validated');
    }

    return $this->render('index', [
        //'provider' => $provider,
        'array' => $this->test,
        'array2' => $this->test2
    ]);
}
© www.soinside.com 2019 - 2024. All rights reserved.