Yii2 ActiveForm radioList需要进行值验证

时间:2016-07-12 16:46:20

标签: yii2 yii2-validation

我使用Yii2 activeForm字段实现了一个表单,但是无法提交表单。我一直收到验证错误"付款方式不能为空白"从模型中,我将付款方式设置为必需值,但不知何故,来自radioList的值无法获得模型的payment_method属性。

型号:

class OrderFormModel extends Model
{
    public $total;
    public $extra;
    public $admin_message;
    public $is_taxed;
    public $alt_email;
    public $payment_method;
    public $alt_address;
    public $address;
    public $attachment;
    public $lat;
    public $lon;
    const PAY_CREDIT = 1;
    const PAY_CHEQUE = 2;

    public static function getPaymentMethods()
    {
        return[
        self::PAY_CREDIT => 'Pay Via Credit Card',
        self::PAY_CHEQUE => 'Pay Via Cheque / Interac'
        ];
    }

    public function rules()
    {
        return
        [
            [['total', 'extra'], 'number'],
            [['admin_message'], 'string', 'max' => 1000],
            [['is_taxed'], 'integer'],
            ['alt_email', 'email'],
            [['payment_method'], 'integer'],
            [['payment_method'], 'required'],
            [['alt_address'], 'string', 'max' => 255],
            ['address', 'validateAddress','skipOnEmpty' => false, 'skipOnError' => false],
            [['attachment'], 'file', 'extensions' => 'pdf, doc, docx, rtf, txt'],
            [['lat','lon'], 'double']
        ];
    }

}

查看:

<div id="payment_method" style="margin-bottom: 5%">
            <h2 style="text-align: center">Set Customer Payment Option</h2>
            <div class="row">
                <?= $form->field($orderForm,'payment_method')->radioList(OrderFormModel::getPaymentMethods(),
                ["class"=>"form-control", "id"=>"pymnt-method", "tag" => "false","required" => "true","onclick"=>"$('#orderDetails-payment_method').val( $('input:radio:checked').val() )",
                "itemOptions" => ["class" => "radio"],
                "item" => function($index, $label, $name, $checked, $value){
                    $return = '<div class="col-sm-6"><label class = "modal-radio">';
                    $return .= '<input type = "radio" name ="'. $name .'" value = "' . $value .'" tabindex = "3" >';
                    $return .= '<span style = "padding: 0.7em;"><b style="font-size: 1.8em;">' . ucwords($label) .'</b></span>';
                    $return .= '</label></div>';
                    return $return;
                }]) ?>
            </div>
        </div>

0 个答案:

没有答案