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

Suggestion for conditional validation? #483

Open
brandondurham opened this issue Sep 28, 2017 · 1 comment
Open

Suggestion for conditional validation? #483

brandondurham opened this issue Sep 28, 2017 · 1 comment

Comments

@brandondurham
Copy link

I find myself in a scenario where I have 4 form fields that need validation only under certain conditions, and no validation in others.

Is conditional validation an option?

@brandondurham
Copy link
Author

Currently I just keep validation rules in an object and apply conditionally:

if (validateCreditCart) {
	Object.assign(ccProps, {
		cardExpiration: {
			required: true,
			validations: true
		},
		cardNumber: {
			required: true,
			validationErrors: { isCreditCardNumber: 'Invalid card number' },
			validations: 'isCreditCardNumber'
		},
		cardCvv: {
			required: true,
			validationErrors: { isCreditCardCvv: 'Invalid cvv' },
			validations: 'isCreditCardCvv'
		}
	});
}

…

<FormInput
	classGroup={ classGroup }
	className={ styles.fieldCreditCardNumber }
	label="Card number"
	layout={ layout }
	name="cardNumber"
	onChange={ this.onChange }
	onFocus={ this.clearCCFields }
	{ ...ccProps.cardNumber }
/>

Is there a better way that you recommend?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant