我正在通過Ponzi Coder的SailsCasts Youtube視圖創建註冊表單,但我遇到了用戶模型驗證方面的問題。如果我拿出驗證規則,我可以提交表單,但是當我重新插入規則時,我會收到以下警告。另外,當我能夠提交時,只返回ID和CreatedAt,沒有別的:SailsCasts用戶模型驗證
{
"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"
}
]
}
}
下面是我的用戶模型:
module.exports = {
\t
schema: true,
\t
attributes: {
name: {
\t type: 'string',
\t required: true
\t },
email: {
\t type: 'string',
\t email: true,
\t unique: true,
\t required: true
},
password: {
\t type: 'string',
\t required: true
},
encryptedPassword:{
\t type: 'string'
},
}
};
而下面是我的UserController中:
module.exports = {
\t // Gets the view: signup.ejs
\t 'signup': function(req, res){
\t \t res.view();
\t },
\t
\t //Creates a user with the info sent from the
\t //signup form in signup.ejs
\t create: function(req, res, next){
\t User.create (req.params.all(), function userCreated(err, user){
\t \t //If there is an error
\t \t if(err) return next(err);
\t \t
\t \t //After the user is successfully created
\t \t //redirect user to the show action
\t \t res.json(user);
\t }); \t
\t }
};
下面是我的註冊表格:
<form action="/user/create" method="POST" class="form-horizontal">
<div class="control-group">
\t <label class="control-label" for="inputname">Name</label>
<div class="controls">
<input type="text" id="Name" placeholder="Name">
</div>
</div>
<div class="control-group">
\t <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>
我一直面臨同樣的問題。我曾嘗試使用POSTMAN,並使用和不使用默認的BluePrints。我嘗試通過發送params作爲form-data或x-www-form-urlencoded或raw以及Content-type頭設置爲application/json來嘗試POSTMAN。但沒有任何工作。唯一可行的是,如果我將參數作爲查詢字符串參數提供,我不打算將這些參數用於POST請求。 – 2015-05-09 22:29:13