Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mgr/dashboard: Add custom validators #21041

Merged
merged 1 commit into from Apr 11, 2018
Merged

Conversation

votdev
Copy link
Member

@votdev votdev commented Mar 26, 2018

Add custom validators:

  • email: In contrast to the Angular email validator an empty email will not be handled as invalid.
  • requiredIf: Validate a control and mark it as required if the value is empty AND if the specified prereqs are fulfilled. Prerequisites are fulfilled if the specified controls have the specified value.

Example:

import { Validators } from '@angular/forms';
import { CdValidators } from '../../../shared/validators/cd-validators';

@Component({
  selector: 'cd-rgw-user-form',
  templateUrl: './rgw-user-form.component.html',
  styleUrls: ['./rgw-user-form.component.scss']
})
export class RgwUserFormComponent implements OnInit, OnDestroy {
  ...

  createForm() {
    this.userForm = this.formBuilder.group({
      // General
      'user_id': [
        null,
        [ Validators.required ],
        [ this.userIdValidator() ]
      ],
      'display_name': [
        null,
        [ Validators.required ]
      ],
      'email': [
        null,
        [ CdValidators.email ]
      ],
      'max_buckets': [
        null,
        [ Validators.min(0) ]
      ],
      'suspended': [
        false
      ],
      // S3 key
      'generate_key': [
        true
      ],
      'access_key': [
        null,
        [ CdValidators.requiredIf({'generate_key': false}) ]
      ],
      'secret_key': [
        null,
        [ CdValidators.requiredIf({'generate_key': false}) ]
      ],

Signed-off-by: Volker Theile vtheile@suse.com

Copy link
Contributor

@ricardoasmarques ricardoasmarques left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@votdev Apparently CustomValidators does not exist. Can you update your PR description example accordingly?

Validators as AngularValidators
} from '@angular/forms';

export type Prerequisites = { // tslint:disable-line
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a need to export this? Same for the function bellow.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it's not necessary to export the Prerequisites type, this has been fixed now. But i would like to export the isEmptyInputValue function because it is really helpful if you implement local validators in a form component. This is done in the RGW user form for example.

@votdev
Copy link
Member Author

votdev commented Apr 3, 2018

@ricardoasmarques The example has been adapted.

@votdev votdev force-pushed the custom_validators branch 2 times, most recently from 41bb9c9 to a1de489 Compare April 4, 2018 09:20
}
return isEmptyInputValue(control.value) ? {'required': true} : null;
};
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you extend this by a validateIf function? A function that only validates a form element if the condition given is true like this

Copy link
Member Author

@votdev votdev Apr 9, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think this is needed? This validator is based on the origin required validator that simply tests for empty values.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done to enhance the flexibility of this validator.

@votdev votdev force-pushed the custom_validators branch 2 times, most recently from db2d13e to 4ee15b1 Compare April 10, 2018 10:09
@votdev votdev removed the mgr label Apr 10, 2018
@votdev votdev force-pushed the custom_validators branch 2 times, most recently from ba50e16 to 66b1197 Compare April 10, 2018 11:22
Signed-off-by: Volker Theile <vtheile@suse.com>
Copy link

@Devp00l Devp00l left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

Copy link
Contributor

@ricardoasmarques ricardoasmarques left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@LenzGr LenzGr merged commit 6c9c545 into ceph:master Apr 11, 2018
@votdev votdev deleted the custom_validators branch April 13, 2018 07:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
5 participants