简体   繁体   English

带有ng-pattern形式的AngularJS不会将无效字段发送到服务器

[英]AngularJS with ng-pattern in form does not send invalid field to server

I want to validate a text input in form, so the submit of the form could not be done until the input match a regular expression. 我想验证表单中的文本输入,因此在输入匹配正则表达式之前无法完成表单的提交。 But when I type a wrong field value and I clik submit the form is submitted but the input value is not sent to the server. 但是,当我键入了错误的字段值并且单击提交时,将提交表单,但输入值不会发送到服务器。 I want the same behaviour as with HTML5 required Attribute. 我想要与HTML5必需属性相同的行为。 This is my code: 这是我的代码:

<div class="row">
    <label class="col-sm-2 label-on-left">APN</label>
    <div class="col-sm-7">
        <div class="form-group label-floating">
            <label class="control-label"></label>
            <input class="form-control" type="text" name="apn" ng-model="Configure3gCtrl.configure3g.apn" ng-pattern="/^[a-zA-Z0-9-.]*$/" required/>
        </div>
    </div>
</div>          

As i said in the comment [value not sent because when you pass the input with incorrect pattern the ng-model is undefined ]. 正如我在评论中所说的那样 [未发送值,因为当您以错误的模式传递输入时ng-modelundefined ]。

But we can use the form validation here as sample if our ng-model are invalid the form will disabled. 但是,如果我们的ng-model invalid ,则可以在此处使用表单验证作为示例,表单将被禁用。

 var app = angular.module("app", []); app.controller("ctrl", ["$scope", "$filter", function($scope, $filter) { $scope.submit = function() { console.log($scope.object) } }]); 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app="app" ng-controller="ctrl"> <form name="form"> <label class="col-sm-2 label-on-left">APN</label> <input type="text" name="apn" ng-model="object.apn" ng-pattern="/^[a-zA-Z0-9-.]*$/" required /> <button ng-click="submit()" ng-disabled="form.$invalid">submit</button> </form> </div> 

Ideally, you should not send the invalid value to server, So you should disable\\hide your submit button, but if you really require sending the invalid value as well to server, then from angularjs 1.3+ you have ng-model-options ( Read Doc ) directive which can help you. 理想情况下,您不应该将无效值发送到服务器,因此应该disable\\hide提交按钮,但是如果您确实需要将无效值也发送到服务器,那么从angularjs 1.3+开始,您可以使用ng-model-options阅读Doc )指令可以为您提供帮助。

Simply mark your text type input as ng-model-options="{allowInvalid: true }" , It will persist the invalid values as well. 只需将您的text type输入标记为ng-model-options="{allowInvalid: true }" ,它将同样保留无效值。

See Demo: 观看演示:

 var myApp = angular.module('myApp', []); myApp.controller('MyCtrl', function MyCtrl($scope) { $scope.submitt = function() { alert($scope.Configure3gCtrl.configure3g.apn); } $scope.Configure3gCtrl = { configure3g: { apn: "" } } }); 
 <script src="https://code.angularjs.org/1.3.1/angular.js"></script> <div ng-app="myApp" ng-controller="MyCtrl"> <form name="frm" ng-submit="submitt()" class="row"> <label class="col-sm-2 label-on-left">APN</label> <div class="col-sm-7"> <div class="form-group label-floating"> <label class="control-label"></label> <input class="form-control" type="text" name="apn" ng-model="Configure3gCtrl.configure3g.apn" ng-model-options="{allowInvalid: true }" ng-pattern="/^[a-zA-Z0-9-.]*$/" required/> </div> </div> <input type="submit" value="submit" type="submit" /> </form> </div> 

also, Test with removing ng-model-options="{allowInvalid: '$inherit' }" from above code snippet then ng-model will be undefined , because it is invalid. 同样,从上面的代码片段中删除ng-model-options="{allowInvalid: '$inherit' }" ,然后进行测试,则ng-model将是undefined ,因为它是无效的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM