_form.php 1.7 KB

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