Skip to content

Validator

The Validator component provides form validation functionality with a variety of validation rules. It allows you to validate form inputs using custom data attributes and handles form submission based on validation results. The component automatically validates form inputs before submission and prevents the form from being submitted if validation fails.

  • Farbe - Used for color validation
<form data-role="validator">
<input type="text" data-validate="required">
<input type="email" data-validate="required email">
<input type="number" data-validate="required number min=0 max=100">
<button type="submit">Submit</button>
</form>
<form data-role="validator" data-interactive-check="true" data-clear-invalid="2000">
<div class="form-group">
<label>Email</label>
<input type="email" data-validate="required email">
</div>
<button type="submit">Submit</button>
</form>
ParameterTypeDefaultDescription
validatorDeferredNumber0Defer initialization of component in milliseconds
submitTimeoutNumber200Timeout before form submission in milliseconds
interactiveCheckBooleanfalseIf true, validation occurs on input change
clearInvalidNumber0Time in milliseconds to automatically clear invalid state (0 = disabled)
requiredModeBooleantrueIf true, all validation rules are applied; if false, empty fields bypass non-required validations
useRequiredClassBooleantrueIf true, adds “required” class to elements with required validation
onBeforeSubmitFunctionMetro.noop_trueTriggered before form submission, return false to prevent submission
onSubmitFunctionMetro.noopTriggered on successful form submission
onErrorFunctionMetro.noopTriggered when an input validation fails
onValidateFunctionMetro.noopTriggered when an input validation passes
onErrorFormFunctionMetro.noopTriggered when form validation fails
onValidateFormFunctionMetro.noopTriggered when form validation passes
onValidatorCreateFunctionMetro.noopTriggered after validator component creation

The validator component exposes its functionality through the global Metro.validator object:

  • validate(element, result, onOk, onError, requiredMode) - Validates a specific element with optional result object, callbacks, and required mode.
  • reset(form) - Resets validation state for all elements in a form.
  • reset_state(element) - Resets the validation state of a specific element.
  • set_valid_state(element) - Sets the valid state for a specific element.
  • set_invalid_state(element) - Sets the invalid state for a specific element.
EventDescription
onBeforeSubmitTriggered before form submission, return false to prevent submission
onSubmitTriggered on successful form submission
onErrorTriggered when an input validation fails
onValidateTriggered when an input validation passes
onErrorFormTriggered when form validation fails
onValidateFormTriggered when form validation passes
onValidatorCreateTriggered after validator component creation

Add validation rules to form elements using the data-validate attribute. Multiple rules can be combined with spaces.

RuleFormatDescription
requiredrequiredField cannot be empty
lengthlength=nField must be exactly n characters long
minlengthminlength=nField must be at least n characters long
maxlengthmaxlength=nField must be at most n characters long
minmin=nMinimum numeric value
maxmax=nMaximum numeric value
emailemailField must be a valid email address
domaindomainField must be a valid domain name
urlurlField must be a valid URL
datedateField must be a valid date. Use data-value-format and data-value-locale for formatting
numbernumberField must be a number
integerintegerField must be an integer
safeIntegersafeIntegerField must be a safe integer
floatfloatField must be a float number
digitsdigitsField must contain only digits
hexcolorhexcolorField must be a valid hex color code
colorcolorField must be a valid color
patternpattern=regexField must match the specified regex pattern
comparecompare=field_nameField value must be equal to the referenced field value
notnot=field_nameField value must not be equal to the referenced field value
notequalsnotequals=field_nameField value must not be strictly equal to the referenced field value
equalsequals=field_nameField value must be strictly equal to the referenced field value
customcustom=func_nameCustom validation function

You can use the validator functions directly:

// Validate a specific element with a result object
Metro.validator.validate(element, result, onOk, onError, requiredMode);
// Reset validation state for all elements in a form
Metro.validator.reset(form);

You can globally configure the validator component using the Metro.validatorSetup method:

Metro.validatorSetup({
interactiveCheck: true,
clearInvalid: 2000,
requiredMode: false,
useRequiredClass: false
});

The validator component doesn’t use specific CSS variables for styling, but it adds CSS classes to elements for styling purposes.

  • .required - Added to elements with required validation (if useRequiredClass is true)
  • .invalid - Added to elements that fail validation
  • .valid - Added to elements that pass validation

For controls with data-role attributes (like select or input), these classes are added to the parent element.

/* Custom styling for validation states */
.invalid {
border-color: #ff0000;
background-color: #ffeeee;
}
.valid {
border-color: #00ff00;
background-color: #eeffee;
}
.required::after {
content: "*";
color: #ff0000;
}
<form data-role="validator">
<div class="form-group">
<label>Name</label>
<input type="text" data-validate="required">
</div>
<div class="form-group">
<label>Email</label>
<input type="email" data-validate="required email">
</div>
<div class="form-group">
<label>Age</label>
<input type="number" data-validate="required number min=18 max=100">
</div>
<button type="submit">Submit</button>
</form>
<form data-role="validator">
<div class="form-group">
<label>Password</label>
<input type="password" name="password" data-validate="required minlength=6">
</div>
<div class="form-group">
<label>Confirm Password</label>
<input type="password" data-validate="required compare=password">
</div>
<button type="submit">Submit</button>
</form>
<form data-role="validator" data-interactive-check="true">
<div class="form-group">
<label>Email</label>
<input type="email" data-validate="required email">
</div>
<button type="submit">Submit</button>
</form>
<script>
function validateCustomField(val) {
// Your custom validation logic
return val === "correct value";
}
</script>
<form data-role="validator">
<div class="form-group">
<label>Custom Field</label>
<input type="text" data-validate="required custom=validateCustomField">
</div>
<button type="submit">Submit</button>
</form>