1
\$\begingroup\$

I have created like this validator for reactive form, all working but I think I used so many statements, can somebody help me to make proper validation

Here is my validator

export function validateIfOneIsRequired(): ValidatorFn {
  return function validateIfOneIsRequired(formGroup: FormGroup) {
    const startTime = formGroup.controls.startTime.value;
    const endTime = formGroup.controls.endTime.value;
    if (startTime === '' && endTime === '') {
      return null;
    }
    if (startTime && endTime) {
      return null;
    }

    if (!startTime && endTime) {
      return { startTimeIsRequired: OperationalTime.startTimeIsRequired };
    }

    if (startTime && !endTime) {
      return { endTimeIsRequired: OperationalTime.endTimeIsRequired };
    }
  };
}
\$\endgroup\$
2
  • \$\begingroup\$ Please edit your question and elaborate what this code exactly is supposed to do, and how it is used in context. \$\endgroup\$ Commented Dec 26, 2019 at 12:41
  • \$\begingroup\$ if you look the code you will see that this is validator for reactive forms? it should validate if one of fields is required \$\endgroup\$ Commented Dec 26, 2019 at 12:43

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.