以简单格式

时间:2017-02-20 11:28:33

标签: php yii2 save select2

我有使用Select2小部件的简单表单。我有问题的部分看起来像这样:

<?php
$form->field($model, 'payment_option_id')->widget(Select2::classname(), [
    'data' => [],
    'id' => 'payment-option-id-select-field',
    'options' => ['placeholder' => Yii::t('app', 'Select payment')],
    'pluginOptions' => [
        'allowClear' => true,
        'minimumInputLength' => 0,
        'language' => [
            'errorLoading' => new JsExpression("function () { return 'Waiting for results...'; }"),
        ],
        'ajax' => [
            'url' => '#',
            'dataType' => 'json',
            'data' => new JsExpression('function(params) { console.log(params); return {q:params.term}; }')
        ],
    ]])->label(Yii::t('app', Yii::t('app', 'Payment')))
?>

JS工作正常,最后我的select字段填充了我想要的选项,所以一切都很好。我选择了一个选项,当我提交表单时,我收到一个保存错误:payment_option_id包含无效值。当我var_dump我的$ _POST有什么,它看起来没问题,它包含正确的值,但它并不想保存该记录。

var_dump($ _ POST):

array (size=2)
'Payments' => 
      array (size=5)
              'amount' => string '3567' (length=4)
              'date' => string '2017-02-23' (length=10)
              'deal_id' => string '5' (length=1)
              'buyer_id' => string '5' (length=1)
              'payment_option_id' => string '18' (length=2)
'submit' => string 'create' (length=6)

我在PaymentOption模型中的规则:

public function rules()
    {
        return [
            [['amount', 'date', 'deal_id', 'user_id'], 'required'],
            [['amount'], 'number'],
            [['date', 'buyer_id'], 'safe'],
            [['deal_id', 'user_id', 'payment_option_id'], 'integer'],
            [['deal_id'], 'exist', 'skipOnError' => true, 'targetClass' => Deals::className(), 'targetAttribute' => ['deal_id' => 'id']],
            [['user_id'], 'exist', 'skipOnError' => true, 'targetClass' => User::className(), 'targetAttribute' => ['user_id' => 'id']],
            [['payment_option_id'], 'exist', 'skipOnError' => true, 'targetClass' => User::className(), 'targetAttribute' => ['payment_option_id' => 'id']],
        ];
    }

0 个答案:

没有答案