小编C M*_*C M的帖子

locomotivejs服务器端的模型验证

我想知道是否有一个函数来检查我的模型在服务器端是否有效,我在express上使用locomotivejs.

这是我的模型架构

var BillSchema = new Schema({
   BrandId:Schema.Types.ObjectId,
    ModelNo:{ type: String, required: true },
    BillNo:{ type: String, required: true },
    years:Number,
    months:Number,
    })
Run Code Online (Sandbox Code Playgroud)

在我的控制器中,我从视图中接收值.

var bill=new Bill();
  bill.CategoryId=self.param('CategoryId');
  bill.BrandId=self.param("BrandId");
  bill.ModelNo=self.param("Model");
  bill.BillNo=self.param("BillNo");
if(bill.categoryId!="" && bill.BrandId!="" && bill.ModelNo!="" && bill.years!="")
{
// code to save the values to db
}
Run Code Online (Sandbox Code Playgroud)

有没有办法避免这种验证

if(bill.categoryId!="" && bill.BrandId!="" && bill.ModelNo!="" && bill.years!="")
Run Code Online (Sandbox Code Playgroud)

ModelState.Isvalid.Net MVC中的内容类似

提前致谢...

javascript express locomotivejs

4
推荐指数
1
解决办法
98
查看次数

标签 统计

express ×1

javascript ×1

locomotivejs ×1