* Flashes validation error messages. Make sure the error is an instance of VineJS ValidationException. * Overrides existing inputErrors. * * @param error - HTTP error containing validation messages * * @example * try { * await request.validate(schema) * } catch (error) {
(error: HttpError, withInput: boolean = true)
| 484 | * } |
| 485 | */ |
| 486 | flashValidationErrors(error: HttpError, withInput: boolean = true) { |
| 487 | const errorsBag = error.messages.reduce((result: Record<string, string[]>, message: any) => { |
| 488 | if (result[message.field]) { |
| 489 | result[message.field].push(message.message) |
| 490 | } else { |
| 491 | result[message.field] = [message.message] |
| 492 | } |
| 493 | return result |
| 494 | }, {}) |
| 495 | |
| 496 | if (withInput) { |
| 497 | this.flashExcept(['_csrf', '_method', 'password', 'password_confirmation']) |
| 498 | } |
| 499 | |
| 500 | /** |
| 501 | * Adding the error summary to the "errorsBag" so that |
| 502 | * we display the validation error globally using |
| 503 | * the "@error" tag. |
| 504 | */ |
| 505 | let summary = 'The form could not be saved. Please check the errors below.' |
| 506 | if ('i18n' in this.#ctx) { |
| 507 | summary = (this.#ctx.i18n as I18n).t( |
| 508 | `errors.${error.code}`, |
| 509 | { |
| 510 | count: error.messages.length, |
| 511 | }, |
| 512 | summary |
| 513 | ) |
| 514 | } |
| 515 | |
| 516 | this.flashErrors({ |
| 517 | [String(error.code)]: summary, |
| 518 | }) |
| 519 | |
| 520 | /** |
| 521 | * Adding to inputErrorsBag for "@inputError" tag |
| 522 | * to read validation errors |
| 523 | */ |
| 524 | this.flash('inputErrorsBag', errorsBag) |
| 525 | } |
| 526 | |
| 527 | /** |
| 528 | * Flashes all form input data to the flash messages store |
no test coverage detected