-
Couldn't load subscription status.
- Fork 0
Using the Library: Custom Implementors
In case the built-in implementor is not exactly right to your needs, or you are using a totally different client-side validation strategies, you may use your own implementor by implementing the IValidationImplementor interface. The snippet below shows the contents of the BootstrapValidationImplementor class, which you may base your own off of.
The entry point for an implementor is the AttachValidators(List<IValidatorAttribute> validators, object control) function. This function accepts the command from the main library to decorate a control based on the validator type(s) passed to it.
public class BootstrapValidationImplementor : IValidationImplementor
{
public void AttachValidators(List<IValidatorAttribute> validators, object control)
{
foreach (var validator in validators)
{
if (validator is StringValidatorAttribute)
AttachValidators(validator as StringValidatorAttribute, control);
else if (validator is DateValidatorAttribute)
AttachValidators(validator as DateValidatorAttribute, control);
else if (validator is IntValidatorAttribute)
AttachValidators(validator as IntValidatorAttribute, control);
else if (validator is DoubleValidatorAttribute)
AttachValidators(validator as DoubleValidatorAttribute, control);
else if (validator is EmailValidatorAttribute)
AttachValidators(validator as EmailValidatorAttribute, control);
}
}
private void AttachValidators(StringValidatorAttribute validator, Object control)
{
WebHelper.ApplyWebControlAttribute(control, "minlength", validator.MinSize.ToString(CultureInfo.InvariantCulture));
WebHelper.ApplyWebControlAttribute(control, "maxlength", validator.MaxSize.ToString(CultureInfo.InvariantCulture));
if (validator.IsRequired)
WebHelper.ApplyWebControlAttribute(control, "required", "required");
WebHelper.ApplyWebControlAttribute(control, "data-error", "test message");
}
private void AttachValidators(DateValidatorAttribute validator, Object control)
{
WebHelper.ApplyWebControlAttribute(control, "type", "date");
if (validator.IsRequired)
WebHelper.ApplyWebControlAttribute(control, "required", "required");
WebHelper.ApplyWebControlAttribute(control, "max", validator.MaximumDate.ToString(CultureInfo.InvariantCulture));
WebHelper.ApplyWebControlAttribute(control, "min", validator.MinimumDate.ToString(CultureInfo.InvariantCulture));
if (!validator.AllowFutureDate && validator.MaximumDate > DateTime.UtcNow)
WebHelper.ApplyWebControlAttribute(control, "max", DateTime.Now.Date.ToString(CultureInfo.InvariantCulture));
WebHelper.ApplyWebControlAttribute(control, "data-error", "test message");
}
private void AttachValidators(IntValidatorAttribute validator, Object control)
{
WebHelper.ApplyWebControlAttribute(control, "type", "number");
WebHelper.ApplyWebControlAttribute(control, "min", validator.MinValue.ToString(CultureInfo.InvariantCulture));
WebHelper.ApplyWebControlAttribute(control, "max", validator.MaxValue.ToString(CultureInfo.InvariantCulture));
if (validator.IsRequired)
WebHelper.ApplyWebControlAttribute(control, "required", "required");
WebHelper.ApplyWebControlAttribute(control, "data-error", "test message");
}
private void AttachValidators(DoubleValidatorAttribute validator, Object control)
{
WebHelper.ApplyWebControlAttribute(control, "type", "number");
WebHelper.ApplyWebControlAttribute(control, "step", Convert.ToString(1.0 / Math.Pow(10.0, validator.Decimals)));
WebHelper.ApplyWebControlAttribute(control, "min", validator.MinValue.ToString(CultureInfo.InvariantCulture));
WebHelper.ApplyWebControlAttribute(control, "max", validator.MaxValue.ToString(CultureInfo.InvariantCulture));
if (validator.IsRequired)
WebHelper.ApplyWebControlAttribute(control, "required", "required");
WebHelper.ApplyWebControlAttribute(control, "data-error", "test message");
}
private void AttachValidators(EmailValidatorAttribute validator, Object control)
{
WebHelper.ApplyWebControlAttribute(control, "type", "email");
WebHelper.ApplyWebControlAttribute(control, "minlength", validator.MinSize.ToString(CultureInfo.InvariantCulture));
WebHelper.ApplyWebControlAttribute(control, "maxlength", validator.MaxSize.ToString(CultureInfo.InvariantCulture));
if (validator.IsRequired)
WebHelper.ApplyWebControlAttribute(control, "required", "required");
WebHelper.ApplyWebControlAttribute(control, "data-error", "test message");
}
}
The WebHelper.ApplyWebControlAttribute() function takes care of adding the custom attributes for web controls. The web controls are passed into the functions as Objects, not explicit WebControl or Control. This is to keep the framework lightweight and portable.
You may base your custom implementors from this example, doing whatever is needed for each type of validation attribute.