Five New Optimizely Certifications are Here! Validate your expertise and advance your career with our latest certification exams. Click here to find out more
AI OnAI Off
Five New Optimizely Certifications are Here! Validate your expertise and advance your career with our latest certification exams. Click here to find out more
I solved this by extending `ChoiceElementBlock` instead of `InputElementBlockBase`. The functionality I am after (required validation) already exists in the `ChoiceElementBlock` so it was easier to just use that one. Here is my model class, cleaning up some elements so that the "checkbox" functionality is built in:
[ContentType(DisplayName = "Checkbox", GroupName = GroupNames.FormElements, GUID = "...")]
public class CheckboxElementBlock : ChoiceElementBlock
{
[ScaffoldColumn(false)]
public override string Label { get; set; }
[ScaffoldColumn(false)]
public override IEnumerable<OptionItem> Items { get; set; }
[ScaffoldColumn(false)]
public override bool AllowMultiSelect { get; set; }
[ScaffoldColumn(false)]
public override string PredefinedValue { get; set; }
[ScaffoldColumn(false)]
public override string Feed { get; set; }
[Display(
Name = "Label",
Order = -6999)]
public virtual XhtmlString Body { get; set; }
public override void SetDefaultValues(ContentType contentType)
{
base.SetDefaultValues(contentType);
Items = new List<OptionItem>() { new OptionItem { Value = "x", Caption = "x" } };
AllowMultiSelect = true;
Label = null;
}
}
Hello, I have read that you can make a checkbox by using a selection element with only one selection item, however, I also needed to add a link to the "label" so I decided to make my own:
[ContentType(DisplayName = "Checkbox", GroupName = GroupNames.FormElements, GUID = "...")] [AvailableValidatorTypes(Include = new Type[] { typeof(RequiredValidator) })] public class CheckboxElementBlock : InputElementBlockBase { [ScaffoldColumn(false)] public override string Label { get; set; } [ScaffoldColumn(false)] public override string PlaceHolder { get; set; } [Display( Name = "Description", Order = -6999)] public virtual XhtmlString Body { get; set; } }
Here is the view:
My problem is that, when I mark this field required (view the RequiredValidator), the validation never passes. It does not matter if the checkbox is checked or not, I still get the message that this field is required. I did some digging (or decompiling) and found this code from the RequiredValidator:
public override bool? Validate(IElementValidatable targetElement) { object submittedValue = targetElement.GetSubmittedValue(); if (submittedValue is string) { return !string.IsNullOrWhiteSpace((string)submittedValue); } if (submittedValue is IEnumerable && !((IEnumerable)submittedValue).GetEnumerator().MoveNext()) { return false; } return submittedValue != null; }
So, I have an idea that the submitted value might be null? Has anyone run into this or know how to solve my issue?