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 };
  • \$\begingroup\$ Please edit your question and elaborate what this code exactly is supposed to do, and how it is used in context. \$\endgroup\$ – πάντα ῥεῖ Dec 26 '19 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\$ – Miomir Dancevic Dec 26 '19 at 12:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.