V5: Validation for update #1666
Replies: 5 comments 1 reply
-
#1513 is exactly what you are looking for. However, you seem to be using a validator class, so using the current mobile value will require access to an instance of the Mobile model. |
Beta Was this translation helpful? Give feedback.
-
You need to ignore mobile id using the export default class MobileValidator {
public schema = schema.create({
mobile: schema.number([
rules.phone(),
rules.unique({
table: Mobile.table,
column: 'mobile',
whereNot: { id: this.ctx.params.id },
})
]),
})
public messages = {
'mobile.required': 'Mobile is required',
'mobile.phone': 'Mobile must be a valid 10 digit mobile number',
'mobile.unique': 'Mobile is already in use',
}
} |
Beta Was this translation helpful? Give feedback.
-
It doesn't work for me |
Beta Was this translation helpful? Give feedback.
-
in the update it continues showing that the data already exists, and even changing all the data this error keeps showing up |
Beta Was this translation helpful? Give feedback.
-
@victorLessa & @Pratikkshirsagar create multiple validators: import { schema, rules } from '@ioc:Adonis/Core/Validator'
import Role from 'App/Models/Role'
const role = Object.keys(Role.$keys.columnsToSerialized.all())
export const indexSchema = schema.create({
page: schema.number(),
per_page: schema.number([
rules.range(10, 100)
]),
order_column: schema.string.optional({}, [rules.regex(new RegExp(`^(${role.join('|')})$`))]),
order: schema.enum.optional(['asc', 'desc'] as const),
filter: schema.object().members({
name: schema.string.optional(),
parent_role_id: schema.number.optional()
})
})
export const showSchema = schema.create({
params: schema.object().members({
id: schema.number([rules.exists({ table: 't_roles', column: 'id' })])
})
})
export const createSchema = schema.create({
name: schema.string({
trim: true
}, [rules.maxLength(100), rules.minLength(3)]),
parent_role_id: schema.number.optional([
rules.exists({ table: 't_roles', column: 'id' })
])
})
export const updateSchema = schema.create({
name: schema.string.optional({
trim: true
}, [rules.maxLength(100), rules.minLength(3)]),
parent_role_id: schema.number.optional([
rules.exists({ table: 't_roles', column: 'id' })
])
}) |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi,
I'm using the Adonis 5 validator.
App/Validators/MobileValidator
for create this validation works fine but while updating it give me error of mobile already exist, how can I change this behaviour for update.
Beta Was this translation helpful? Give feedback.
All reactions