* Sets a list of submission errors to their individual fields. * * @param submittedField The field that was submitted, resulting in the errors. * @param errors The errors to set.
( submittedField: FieldNode, errors: OneOrMany<ValidationError.WithOptionalFieldTree>, )
| 546 | * @param errors The errors to set. |
| 547 | */ |
| 548 | function setSubmissionErrors( |
| 549 | submittedField: FieldNode, |
| 550 | errors: OneOrMany<ValidationError.WithOptionalFieldTree>, |
| 551 | ) { |
| 552 | if (!isArray(errors)) { |
| 553 | errors = [errors]; |
| 554 | } |
| 555 | const errorsByField = new Map<FieldNode, ValidationError.WithFieldTree[]>(); |
| 556 | for (const error of errors) { |
| 557 | const errorWithField = addDefaultField(error, submittedField.fieldTree); |
| 558 | const field = errorWithField.fieldTree() as FieldNode; |
| 559 | let fieldErrors = errorsByField.get(field); |
| 560 | if (!fieldErrors) { |
| 561 | fieldErrors = []; |
| 562 | errorsByField.set(field, fieldErrors); |
| 563 | } |
| 564 | fieldErrors.push(errorWithField); |
| 565 | } |
| 566 | for (const [field, fieldErrors] of errorsByField) { |
| 567 | field.submitState.submissionErrors.set(fieldErrors); |
| 568 | } |
| 569 | } |