_form.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. use backend\widgets\ActiveForm;
  3. use common\modules\attachment\widgets\SingleWidget;
  4. use yii\helpers\Html;
  5. /* @var $this yii\web\View */
  6. /* @var $model common\models\GoodsSku */
  7. /* @var $form ActiveForm */
  8. ?>
  9. <div class="box box-success">
  10. <div class="box-body">
  11. <?php $form = ActiveForm::begin([
  12. 'enableClientValidation' => false,
  13. 'id' => 'form-sku',
  14. 'enableAjaxValidation' => true// 需要使用 backend\widgets\ActiveForm 后台自定义表单
  15. ]); ?>
  16. <?= $form->field($model, 'name')->textInput(['maxlength' => true]) ?>
  17. <?= $form->field($model, 'origin_price')->textInput(['maxlength' => true]) ?>
  18. <?= $form->field($model, 'price')->textInput(['maxlength' => true]) ?>
  19. <?= $form->field($model, 'stock')->textInput() ?>
  20. <!-- --><? //= $form->field($model, 'sort')->textInput() ?>
  21. <?= $form->field($model, 'cover')->widget(SingleWidget::className()) ?>
  22. <div class="form-group form-submit">
  23. <?= Html::submitButton($model->isNewRecord ? Yii::t('common', 'Create') : Yii::t('common', 'Update'), ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-success']) ?>
  24. </div>
  25. <?php ActiveForm::end(); ?>
  26. </div>
  27. </div>
  28. <?php
  29. $js = <<<JS
  30. //表单提交
  31. $(document).on('beforeSubmit', 'form#form-sku', function() {
  32. var form = $(this);
  33. //返回错误的表单信息
  34. if (form.find('.has-error').length)
  35. {
  36. return false;
  37. }
  38. //表单提交
  39. $.post(form.attr('action'), form.serialize(), (res) => {
  40. if (res.errcode == 0) {
  41. $.modal.notify(res.errmsg, 'success', () => {
  42. //关闭layer
  43. parent.layer.close(parent.layer.getFrameIndex(window.name))
  44. });
  45. } else {
  46. $.modal.error(res.errmsg);
  47. }
  48. });
  49. return false;
  50. });
  51. JS;
  52. $this->registerJs($js);
  53. ?>