AngularJS空表单验证出错


AngularJS empty form validation bugs out

我有一个表单,用户的输入需要从表单传递到控制器,然后在PHP数据库中发布。

当我单击提交,并且输入为空时,false语句会起作用,但是,如果我在输入中键入内容,然后手动清空它们,则数据仍然会插入,即使它为空(返回true)。

HTML代码:

<div id="container">    
    <section ng-controller="mapController">
            <h1> Contact Us</h1>                
            <br>
            <h2> If you wish to contact us about prices and information, kindly fill in the form below. </h2>
            <br>
            <form class="form-group" id="customForms" ng-controller="getContactsCtrl">
                    <label> E-mail </label> 
                    <input id="customFormsInput" class="form-control" ng-model="contact.email" type="email" placeholder="E-mail goes here" required/>
                    <br> 
                    <label> Mobile Number </label>
                    <input id="customFormsInput" class="form-control" ng-model="contact.mobile" type="number" placeholder="Your number goes here" required/>
                    <br> 
                    <label> How may we be of service? </label>
                    <br>
                    <textarea rows="4" cols="100" ng-model="contact.text" type="text" placeholder="Insert text here" required>
                    <!-- User text -->
                    </textarea>
                    <br><br>
                    <button class="btn btn-primary" ng-click="newContact(contact)"> Submit </button>
                    <br> <br><p> {{queryMsg}} </p>
                </form> 
            <h2> You can also visit us at our premises at Smart City, Xghajra. </h2>
            <br>
            <div id="googleMap" style="width:500px;height:380px;"></div>
            <br><br>
    </section>  
</div>

AngularJS控制器:

    app.controller('getContactsCtrl', ['$scope', '$http', function($scope, $http)   
{
    $scope.newContact = function(contact) {
        console.log(contact);
        $scope.queryMsg= ""; //displays if sent or not in html form
        //post is used to create
        $http.post('model/addContact.php', contact).success(function(data) {
                if (data && contact != null) 
                {//row inserted
                  $scope.queryMsg = "Query has been sent successfully.";
                  console.log("Query received in DB");
                  $scope.contact=""; //clear text again after submit is pressed
                }
                else
                {
                  $scope.queryMsg = "We were unable to fetch your query at this time.";
                  console.log("Query not received");
                }
            })
        };
    }
]);

PHP代码:

    <?php
    $data = json_decode(file_get_contents("php://input"));
    $email = $data->email;
    $mobile = $data->mobile;
    $text = $data->text;
    require_once("connection.php");
    $connection = connectToMySQL();
    $query = "INSERT INTO tbl_contactus (email, mobile, text) VALUES ('$email', '$mobile', '$text')";
    $result = mysqli_query($connection, $query)
         or die("Error in query: ". mysqli_error($connection));
    if(mysqli_affected_rows($connection) > 0){
            $success = true;
    }else{
            $success = false;
    }
    echo json_encode($success);
?>

我已经让你在jsfiddle上直播了代码,我检查了它,对我来说,它还可以,我试图填写编辑并手动删除它们,但它没有发布表单。

我发现的唯一错误是,您使用了另一个控制器,而不是您创建的控制器。在HTML中,您使用控制器<section ng-controller="mapController">,同时使用getContactsCtrl控制器。

无论如何,请看这里:http://jsfiddle.net/tornado1979/makfjz9L/

尝试应用以下更改:

为您的表格添加name="customForms"并更改您的表格提交按钮如下:

<form class="form-group" name="customForms" id="customForms" ng-controller="getContactsCtrl">    
<button class="btn btn-primary" ng-click="newContact(customForms)"> Submit </button>

更改您的控制器如下:

app.controller('getContactsCtrl', ['$scope', '$http', function ($scope, $http){
$scope.contact = {};
$scope.newContact = function (form) {
    if (form.$invalid)
        return false;
    console.log(contact);
    $scope.queryMsg = ""; //displays if sent or not in html form
    //post is used to create
    $http.post('model/addContact.php', contact).success(function (data) {
        if (data && contact != null) {
            //row inserted
            $scope.queryMsg = "Query has been sent successfully.";
            console.log("Query received in DB");
            $scope.contact = ""; //clear text again after submit is pressed
        } else {
            $scope.queryMsg = "We were unable to fetch your query at this time.";
            console.log("Query not received");
        }
    });
  };
}]);