* 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>, )
| 509 | * @param errors The errors to set. |
| 510 | */ |
| 511 | function setSubmissionErrors( |
| 512 | submittedField: FieldNode, |
| 513 | errors: OneOrMany<ValidationError.WithOptionalFieldTree>, |
| 514 | ) { |
| 515 | if (!isArray(errors)) { |
| 516 | errors = [errors]; |
| 517 | } |
| 518 | const errorsByField = new Map<FieldNode, ValidationError.WithFieldTree[]>(); |
| 519 | for (const error of errors) { |
| 520 | const errorWithField = addDefaultField(error, submittedField.fieldTree); |
| 521 | const field = errorWithField.fieldTree() as FieldNode; |
| 522 | let fieldErrors = errorsByField.get(field); |
| 523 | if (!fieldErrors) { |
| 524 | fieldErrors = []; |
| 525 | errorsByField.set(field, fieldErrors); |
| 526 | } |
| 527 | fieldErrors.push(errorWithField); |
| 528 | } |
| 529 | for (const [field, fieldErrors] of errorsByField) { |
| 530 | field.submitState.submissionErrors.set(fieldErrors); |
| 531 | } |
| 532 | } |