使用angularjs进行表单验证

时间:2017-01-03 09:21:27

标签: angularjs validation ionic-framework

我试图验证我的表单,如果字段有效,那么它应该将数据发布到数据库,如果不能防止将数据发布到数据库。当我提交表格时我得到一个错误TypeError: Cannot read property '$valid' of undefined,我不知道如何克服它,需要帮助。

HTML:

<form name="formvalidation">
<ion-view ng-controller="ValidationCheck">
<ion-content>
<div>
<strong>User:</strong> <br/>
<input ng-model="name" type="text" name="name" required/>
<ng-messages for="formvalidation.name.$error" ng-if="formvalidation.name.$invalid">
<div ng-messages-include="error/validation.html"></div>
<ng-messages>
</div>
</from>
<button ng-click="check()">

控制器:

myApp.controller('ValidationCheck',function($scope, applicationService)
{
$scope.check=function(formvalidation){
var name={'name'=$scope.name};
$scope.submitted=true;
if(formvalidation.$valid){
applicationService.save(name,$scope.home);
}}});

5 个答案:

答案 0 :(得分:1)

只需将您的if条件formvalidation.$valid更改为formvalidation 。因为您已通过ng-click

在函数中传递formvalidation.$valid
if(formvalidation){
...
...
...

}

答案 1 :(得分:0)

删除ng-click并添加ng-submit:

<form name="formvalidation" ng-submit="check()" novalidate>

答案 2 :(得分:0)

试试这个:(形式前的控制器)

    <ion-view ng-controller="ValidationCheck">
       <form name="formvalidation">

答案 3 :(得分:0)

在您的HTML中,您有:

<button ng-click="check(formvalidation.$valid)">

使用true或false值(布尔值)调用check函数,具体取决于表单是否有效。

在你的控制器中你有一个带有以下if语句的检查功能:

if(formvalidation.$valid)

但是formvalidation是一个布尔值(true或false)

我建议从check中删除参数,只需从$ scope变量中访问formvalidation属性。

所以html变成了

<button ng-click="check()">

,控制器变为

$scope.check=function(){
    var name={'name'=$scope.name};
    $scope.submitted=true;
    if($scope.formvalidation.$valid) {
        applicationService.save(name,$scope.home);
    }
}

答案 4 :(得分:0)

<form>应该在控制器声明之后

<div ng-app="app">
  <ion-view ng-controller="ValidationCheck">
    <form name="formvalidation">
      <ion-content>

&#13;
&#13;
var app = angular.module("app", []);
app.controller('ValidationCheck', function($scope) {
  $scope.check = function(formvalidationBoolean) {
    alert(formvalidationBoolean);
    var name = {
      'name': $scope.name
    };
    $scope.submitted = true;
    if (formvalidationBoolean) {
      //applicationService.save(name, $scope.home);
    }
  }
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<div ng-app="app">
  <ion-view ng-controller="ValidationCheck">
    <form name="formvalidation">
      <ion-content>
        <div>
          <strong>User:</strong> 
          <br/>
          <input ng-model="name" type="text" name="name" required/>
          <ng-messages for="formvalidation.name.$error" ng-if="formvalidation.name.$invalid">
            <div ng-messages-include="templates/admin/validation.html"></div>
            <ng-messages>
        </div>
        </from>
        <button ng-click="check(formvalidation.$valid)">Submit</button>
</div>
&#13;
&#13;
&#13;

  1. 您正在发送布尔值"ng-click="check(formvalidation.$valid)",因此您在控制器中的语句if(formvalidation.$valid){是错误的。
  2. 你需要改变&#34; =&#34;到&#34;:&#34;在var name = {'name': $scope.name };