SailsCasts 用户模型验证 [英] SailsCasts User model validation

查看:43
本文介绍了SailsCasts 用户模型验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在按照 Ponzi Coder 的 SailsCasts Youtube 视图创建注册表单,但我在用户模型验证方面遇到了问题.如果我取出验证规则,我可以提交表单,但是当我重新输入规则时,我会收到以下警告.此外,当我能够提交它时,只返回 ID 和 CreatedAt,没有别的:

{"错误": "E_VALIDATION",状态":400,"summary": "3 个属性无效","model": "用户",无效属性":{名称": [{"规则": "字符串","message": "`undefined` 应该是一个字符串(而不是 \"null\",它是一个对象)"},{"规则": "必需","message": "\"required\" 输入验证规则失败:null"}],电子邮件": [{"规则": "字符串","message": "`undefined` 应该是一个字符串(而不是 \"null\",它是一个对象)"},{"规则": "电子邮件","message": "\"email\" 输入验证规则失败:空"},{"规则": "必需","message": "\"required\" 输入验证规则失败:null"}],密码": [{"规则": "字符串","message": "`undefined` 应该是一个字符串(而不是 \"null\",它是一个对象)"},{"规则": "必需","message": "\"required\" 输入验证规则失败:null"}]}}

以下是我的用户模型:

module.exports = {架构:真实,属性: {名称: {类型:'字符串',要求:真实},电子邮件: {类型:'字符串',电子邮件:是的,独特:真实,要求:真实},密码: {类型:'字符串',要求:真实},加密密码:{类型:'字符串'},}};

下面是我的 UserController:

module.exports = {//获取视图:signup.ejs注册":函数(请求,资源){res.view();},//使用从发送的信息创建一个用户//signup.ejs 中的注册表单创建:函数(请求,资源,下一个){User.create (req.params.all(), 函数 userCreated(err, user){//如果有错误如果(错误)返回下一个(错误);//用户创建成功后//将用户重定向到显示操作res.json(用户);});}};

以下是我的注册表:

<div class="control-group"><label class="control-label" for="inputname">Name</label><div class="控件"><input type="text" id="Name" placeholder="Name">

<div class="control-group"><label class="control-label" for="inputEmail">Email</label><div class="控件"><input type="text" id="Email" placeholder="Email">

<div class="control-group"><label class="control-label" for="inputPassword">Password</label><div class="控件"><input type="password" id="Password" placeholder="密码">

<div class="control-group"><label class="control-label" for="inputRetypePassword">重新输入密码</label><div class="控件"><input type="password" id="RetypePassword" placeholder="重新输入密码">

<div class="control-group"><button type="submit" class="btn">注册</button>

<input type="hidden" name="_csrf" value="<%= _csrf %>"/>

解决方案

您需要在输入中添加名称"属性:

成为

所有输入都相同.不要忘记像您的模型一样将名称"小写.

I am creating a sign up form by following the SailsCasts Youtube view by Ponzi Coder but I am having issues with the User model validation. If I take out the validation rules I am able to submit the form but I get the warnings below when I put the rules back in. Also, when I am able to submit it, only the ID and the CreatedAt are returned, nothing else:

{
  "error": "E_VALIDATION",
  "status": 400,
  "summary": "3 attributes are invalid",
  "model": "User",
  "invalidAttributes": {
    "name": [
      {
        "rule": "string",
        "message": "`undefined` should be a string (instead of \"null\", which is a object)"
      },
      {
        "rule": "required",
        "message": "\"required\" validation rule failed for input: null"
      }
    ],
    "email": [
      {
        "rule": "string",
        "message": "`undefined` should be a string (instead of \"null\", which is a object)"
      },
      {
        "rule": "email",
        "message": "\"email\" validation rule failed for input: null"
      },
      {
        "rule": "required",
        "message": "\"required\" validation rule failed for input: null"
      }
    ],
    "password": [
      {
        "rule": "string",
        "message": "`undefined` should be a string (instead of \"null\", which is a object)"
      },
      {
        "rule": "required",
        "message": "\"required\" validation rule failed for input: null"
      }
    ]
  }
}

Below is my User model:

module.exports = {
	
schema: true,
	
  attributes: {
  name: {
	type: 'string',
	required: true
	  },
  
  email: {
	  type: 'string',
	  email: true,
	  unique: true,
	  required: true
  },
  password: {
	  type: 'string',
	  required: true
  },
  encryptedPassword:{
	  type: 'string'
  },
 
}
};

And below is my UserController:

module.exports = {
	// Gets the view: signup.ejs
	'signup': function(req, res){
		res.view();
	},
	
	//Creates a user with the info sent from the 
	//signup form in signup.ejs
	create: function(req, res, next){
	User.create (req.params.all(), function userCreated(err, user){
		//If there is an error
		if(err) return next(err);
		
		//After the user is successfully created
		//redirect user to the show action
		res.json(user);
	});	
	}
};

Below is my signup form:

<form action="/user/create" method="POST" class="form-horizontal">
  <div class="control-group">
	<label class="control-label" for="inputname">Name</label>
    <div class="controls">
      <input type="text" id="Name" placeholder="Name">
    </div>
  </div>
  
  <div class="control-group">
	<label class="control-label" for="inputEmail">Email</label>
    <div class="controls">
      <input type="text" id="Email" placeholder="Email">
    </div>
  </div>
  <div class="control-group">
    <label class="control-label" for="inputPassword">Password</label>
    <div class="controls">
      <input type="password" id="Password" placeholder="Password">
    </div>
  </div>
  
  <div class="control-group">
    <label class="control-label" for="inputRetypePassword">Retype Password</label>
    <div class="controls">
      <input type="password" id="RetypePassword" placeholder="Retype Password">
    </div>
  </div>
  <div class="control-group">
    
      <button type="submit" class="btn">Sign up</button>
    
  </div>
  <input type="hidden" name="_csrf" value="<%= _csrf %>"/>
</form>

解决方案

You need to put "name" attribut on your inputs :

<input type="text" id="Name" placeholder="Name">

become

<input type="text" id="Name" name="name" placeholder="Name">

Same for all your inputs. Don't forget to put "name" in lower case like your model.

这篇关于SailsCasts 用户模型验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆