Filled validation?

how do I apply a validation to a field only when it is present?

https://laravel.com/docs/5.7/validation#rule-filled

1 Like

Hi!

Just don’t specify it as required

const rules = {
  // It can be not present or email, can't be number etc
  email: 'email',
}
2 Likes

For me it not worked with file validator in Route.
I fixed filtering error messages by checking if file field is present in request:

get rules () {
  return {
    cover: 'file|file_ext:png,jpg,jpeg|file_size:2mb|file_types:image'
  }
}

async fails (errorMessages) {
  errorMessages.errors = errorMessages.errors.filter(error => {
    if (error.source.pointer !== 'cover' || this.ctx.request.input('cover', null) !== null) {
      return error
    }
  })

  this.ctx.response.status(400)
  return this.ctx.response.send(errorMessages)
}
1 Like