0

我正在尝试在提交到外部 URL 之前验证表单:

<form name="form" novalidate ng-submit="form.$valid && alias != ''" method="POST" action="https://sis-t.redsys.es:25443/sis/realizarPago">
    <input name="alias" ng-change="updateData(alias)" ng-model="alias" type="text" class="form-control validate" placeholder="{{ translates.alias_desc }}" required>
</form>
4

1 回答 1

0

您可以编写自己的指令,以防止提交表单的默认行为。你可以这样做:

app.directive('formValidate', ['$timeout', function ($timeout) {
  return {
    restrict: 'A',
    scope: false,
    require: 'form',
    link: function (scope, elem, attrs, formCtrl) {
      elem.on('submit', function (event) {
        if (!formCtrl.$valid) {
          $timeout(function () {
            formCtrl.$setDirty();
            formCtrl.$setSubmitted();
          });
          event.preventDefault();
        }
      })
    }
  }
}])

请注意,$timeout包装器是强制调用$digest和设置$dirty以及$submitted形成表单的必要条件(如果您使用类ng-dirtyng-submitted表单验证)。

在你可以像这样使用这个指令之后:

<form name="form" novalidate form-validate method="POST" action="https://sis-t.redsys.es:25443/sis/realizarPago">
      <input name="alias" ng-change="updateData(alias)" ng-model="alias" type="text" class="form-control validate" placeholder="{{ translates.alias_desc }}" required>
      <button type="submit">Submit</button>
</form>

演示:http ://plnkr.co/edit/1WYRRT3k4YkjodOD7ul3?p=preview

于 2015-06-08T12:41:47.850 回答