* 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)
| 445 | * } |
| 446 | */ |
| 447 | flashValidationErrors(error: HttpError, withInput: boolean = true) { |
| 448 | const errorsBag = error.messages.reduce((result: Record<string, string[]>, message: any) => { |
| 449 | if (result[message.field]) { |
| 450 | result[message.field].push(message.message) |
| 451 | } else { |
| 452 | result[message.field] = [message.message] |
| 453 | } |
| 454 | return result |
| 455 | }, {}) |
| 456 | |
| 457 | if (withInput) { |
| 458 | this.flashExcept(['_csrf', '_method', 'password', 'password_confirmation']) |
| 459 | } |
| 460 | |
| 461 | /** |
| 462 | * Adding the error summary to the "errorsBag" so that |
| 463 | * we display the validation error globally using |
| 464 | * the "@error" tag. |
| 465 | */ |
| 466 | let summary = 'The form could not be saved. Please check the errors below.' |
| 467 | if ('i18n' in this.#ctx) { |
| 468 | summary = (this.#ctx.i18n as I18n).t( |
| 469 | `errors.${error.code}`, |
| 470 | { |
| 471 | count: error.messages.length, |
| 472 | }, |
| 473 | summary |
| 474 | ) |
| 475 | } |
| 476 | |
| 477 | this.flashErrors({ |
| 478 | [String(error.code)]: summary, |
| 479 | }) |
| 480 | |
| 481 | /** |
| 482 | * Adding to inputErrorsBag for "@inputError" tag |
| 483 | * to read validation errors |
| 484 | */ |
| 485 | this.flash('inputErrorsBag', errorsBag) |
| 486 | } |
| 487 | |
| 488 | /** |
| 489 | * Flashes all form input data to the flash messages store |
no test coverage detected