Validation in a Presenter

Hey all,

Has anyone put validation into a presenter to prevent submission with a custom message.

I’ve got a date presenter I want to add the limit future and past dates checkboxes into.

Thanks

Hi @robinli ,

When you create a new Code Studio component within Create, we deploy a Blueprint which has commented lines, used to provide useful information to you when creating your component.

Inside of the Presenter blueprint, we describe that you can actually handle validation within your presenters using the validate function. This is really awesome because it validates your input value before and after form submission and you can handle it how you please.

This is how you’d define validation.

validate: function(value) {
	// your validation logic
}

Here is an example code snippet of a presenter where I am validating against an integer value in my code - I want to ensure that only an integer between 1-100 is entered.

validate: function (value) {
		if (value == 0 || value > 100) {
			return {
				valid: false,
				error: "Please enter an integer between 1-100"
			}
		}
		else {
			return true;
		}
	}

Hope this helps! :slight_smile:

1 Like