* 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>, )
| 531 | * @param errors The errors to set. |
| 532 | */ |
| 533 | function setSubmissionErrors( |
| 534 | submittedField: FieldNode, |
| 535 | errors: OneOrMany<ValidationError.WithOptionalFieldTree>, |
| 536 | ) { |
| 537 | if (!isArray(errors)) { |
| 538 | errors = [errors]; |
| 539 | } |
| 540 | const errorsByField = new Map<FieldNode, ValidationError.WithFieldTree[]>(); |
| 541 | for (const error of errors) { |
| 542 | const errorWithField = addDefaultField(error, submittedField.fieldTree); |
| 543 | const field = errorWithField.fieldTree() as FieldNode; |
| 544 | let fieldErrors = errorsByField.get(field); |
| 545 | if (!fieldErrors) { |
| 546 | fieldErrors = []; |
| 547 | errorsByField.set(field, fieldErrors); |
| 548 | } |
| 549 | fieldErrors.push(errorWithField); |
| 550 | } |
| 551 | for (const [field, fieldErrors] of errorsByField) { |
| 552 | field.submitState.submissionErrors.set(fieldErrors); |
| 553 | } |
| 554 | } |