November Happy Hour will be moved to Thursday December 5th.
AI OnAI Off
November Happy Hour will be moved to Thursday December 5th.
You override the Validate method from ElementValidatorBase, where you have IElementValidatable interface as parameter.
public override bool? Validate(IElementValidatable targetElement)
{
var submittedValue = targetElement.GetSubmittedValue() as string;
//validate submittedValue.
}
after that you kan override BuildValidationModel method to build the model and pass the validation message you want.
public override IValidationModel BuildValidationModel(IElementValidatable targetElement)
{
if (_model == null)
{
_model = base.BuildValidationModel(targetElement);
_model.Message = _localizationService.GetString("/episerver/forms/validators/elementselfvalidator/unexpectedvalueisnotaccepted");
}
return _model;
}
You can also access the model when inherting ElementValidatorBase with "_model" property.
I hope this helps :-)
In the Validate() method in your validator, you should be able to cast targetElement as your custom element type which should allow you to access the properties set on the element. For example:
public class TestValidator : ElementValidatorBase
{
public override bool? Validate(IElementValidatable targetElement)
{
if (targetElement is MyCustomElementType myElement)
{
//add your validation here
var submittedValue = targetElement.GetSubmittedValue() as string;
return (submittedValue.Contains(myElement.submittedValueShouldntContainThisPropertyValue));
}
return true;
}
}
I'm in the process of creating a numeric input custom form element, consisting of a view, a model inheriting from
InputElementBlockBase
, and a validator inheriting fromElementValidatorBase
.And I'm specifying limitations for the element in the model, for the editor to specify when adding the element. But how can I access the model in the validator, to validate the submitted value against the property values in the model?