简体   繁体   English

在Angular Material中使用客户端表单进行服务器端验证

[英]Server side validation with client side forms in Angular Material

I have a simple form which takes the name of a menu item and the price of that item and puts that data into a database. 我有一个简单的表格,其中包含菜单项的名称和该项目的价格,并将该数据放入数据库中。 In my server, I setup a route where when the program is adding in the menu item, if the name is the same as another name on the menu, it spits out as error via res.json() like this: 在我的服务器中,我设置了一条路线,当程序添加到菜单项中时,如果该名称与菜单上的另一个名称相同,它将通过res.json()吐出错误,如下所示:

if (newItem) {
    //Add the item to the db
  }
  else {
    // the item has the same name as another item on the menu
    res.json("{\"Error\" : \"Menu Item already exists!\"}");
  }

Now I want to show an error in my form when the error object is returned to the client from my server. 现在,当错误对象从服务器返回到客户端时,我想在表单中显示错误。 I have setup my form and the error in my form, but I can't quite get the error to toggle properly when a similar item name is given. 我已经设置了表单和表单中的错误,但是当给出相似的项目名称时,我无法完全正确地切换错误。

This is my form: 这是我的表格:

<form role="form" name="addMenuItem">
            <div layout="row">
                <md-input-container class="md-block" flex-gt-xs="">
                    <label>Item Name</label>
                    <input name="item_name" ng-model="formData.item_name" ng-minlength="1" ng-required="true" size="30">
                    <div ng-messages="addMenuItem.item_name.$error">
                        <div ng-message="required">Item name is required</div>
                    </div>
                    <div ng-messages="addMenuItem.item_name.$error.exists">
                        <div ng-message="required">Item name exists</div>
                    </div>
                </md-input-container>
//other form components

So when the server sends the error object, I want to toggle the message addMenuItem.item_name.$error.exists to show up and I wrote this to toggle it, but it's not quite working: 因此,当服务器发送错误对象时,我想切换消息addMenuItem.item_name.$error.exists来显示,并且我编写了此消息来切换它,但是它不能正常工作:

 $http.post('/addMenuItem', $scope.formData)
            .then(function (data) {
                const resJson = JSON.parse(data.data);
                "Error" in resJson ? console.log("Error") : console.log("No err");
                console.log(resJson.hasOwnProperty('Error'));
                if(resJson.hasOwnProperty('Error')) {
                    $scope.addMenuItem[item_name].$setValidity('exists'); // error thrown on this line
                }
                else {
                      //add to db
                }

            });

When I run that code, the Chrome Dev console spits on this error: 当我运行该代码时,Chrome Dev控制台会显示以下错误:

ReferenceError: item_name is not defined

From what I can decode, looks like it can't find my form(or more specifically my input of item_name ) for some reason and I can't figure out why. 从我可以解码的内容来看,由于某种原因,它似乎找不到我的表单(或更具体地说,是我对item_name输入),而且我不知道为什么。 I'd assume that there is a simple way to toggle the error that I have written in the HTML, right? 我假设有一种简单的方法可以切换我在HTML中编写的错误,对吗?

Reaplce 收割

$scope.addMenuItem[item_name].$setValidity('exists');

With

$scope.addMenuItem["item_name"].$setValidity('exists');

item_name is not a valid variable and hence the error. item_name不是有效的变量,因此是错误。

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

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