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>