我是Adonis.js的新手,我有这样的用户迁移:
table.enu('type', ['client','admin','super_admin']),
table.string('mobile', 80).notNullable().unique();现在,我希望在验证中检查mobile + type对所有用户都是唯一的,而我只需要使用以下代码检查mobile:
mobile : 'required|string|unique:users,mobile',我怎么能这样?
发布于 2020-02-18 19:31:30
我使用扩展验证器创建了类似的内容:
async unique_combinationFn(data, field, message, args, get) { // eslint-disable-line
const Database = use('Database');
const value = get(data, field);
if (!value) {
return;
}
const [table, ...columns] = args;
const multipleClauses = columns.reduce(
(oldObject, column) => ({
...oldObject,
[column]: data[column],
}),
{}
);
const where = { ...multipleClauses, [field]: value };
const row = data.id
? await Database.table(table)
.where(where)
.whereNot('id', data.id)
.first()
: await Database.table(table)
.where(where)
.first();
if (row) {
throw message;
}}
并使用:
code: [rule('required'), rule('unique_combination', ['users', 'company_id'])],您可以在下面的链接中找到更多的文档:
发布于 2019-07-30 09:16:45
使用Adonis.js自己的验证程序是不可能的。您应该为这种用例编写自己的验证方法。
请查看Adonisjs创始人的回复:https://forum.adonisjs.com/t/several-unique-fields/1430/3
https://stackoverflow.com/questions/57216278
复制相似问题