I am creating a custom dotcontrol. I would like to enable some client-side validation to stop a postback when the value is empty, based on another property on the control.
Client-side validation works when I add the Required attribute to the SelectedDate
property in the control’s viewmodel. However, I need to conditionally stop the postback at the client if IsRequired
is true and the value is empty but not when IsRequired
is false. The control will be reused across many pages and sometimes a value will be required while other times it it not.
How do I enable client-side required field validation on SelectedDate
when IsRequired
is true but not when it is is false?
All relevant code follows.
Thanks!
FieldDate.dotcontrol
@viewModel Econometrix.Web.Dotvvm.Controls.Fields.Date.Model.IFieldDate, Econometrix.Web
@baseType Econometrix.Web.Dotvvm.Controls.Fields.Date.FieldDate, Econometrix.Web
@noWrapperTag !
<div class="row align-items-baseline"
Validator.InvalidCssClass="has-error"
Validator.Value="{value: _control.SelectedDate}">
<div class="col-sm-4 col-md-3 col-xl-2 text-sm-right" style="">
<span class="field-label">{{resource: _control.LabelText}}</span>
</div>
<div class="col-sm-8 col-md-9 col-xl-10" style="">
<bp:DatePicker SelectedDate="{value: _control.SelectedDate}" AllowUnselect="false" />
<dot:Validator Value="{value: _control.SelectedDate}">*</dot:Validator>
</div>
</div>
FieldDate.cs
[ControlMarkupOptions(AllowContent = false)]
public class FieldDate : DotvvmMarkupControl
{
public string LabelText
{
get => (string)GetValue(LabelTextProperty);
set => SetValue(LabelTextProperty, value);
}
public static readonly DotvvmProperty LabelTextProperty
= DotvvmProperty.Register<string, FieldDate>(c => c.LabelText, null);
public DateTime? SelectedDate
{
get => (DateTime?)GetValue(SelectedDateProperty);
set => SetValue(SelectedDateProperty, value);
}
public static readonly DotvvmProperty SelectedDateProperty
= DotvvmProperty.Register<DateTime?, FieldDate>(c => c.SelectedDate, null);
/// <summary>
/// Gets or sets whether the control must have a value or not.
/// <remarks>Default is false.</remarks>
/// </summary>
public bool IsRequired
{
get => (bool)GetValue(IsRequiredProperty);
set => SetValue(IsRequiredProperty, value);
}
public static readonly DotvvmProperty IsRequiredProperty
= DotvvmProperty.Register<bool, FieldDate>(c => c.IsRequired, false);
}
FieldDateViewModel.cs
public class FieldDateViewModel : DotvvmViewModelBase, IFieldDate, IValidatableObject
{
#region Control Propeties
//[Required]
public DateTime? SelectedDate { get; set; }
public string LabelText { get; set; }
public bool IsRequired { get; set; }
#endregion Control Propeties
public FieldDateViewModel() { }
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
if (IsRequired && SelectedDate == null)
{
yield return new ValidationResult(
//WebResourceHelper.GetResource(PageResourcing.EndDateMessage).Text,
"Required",
new[] { nameof(SelectedDate)}
);
}
}
}
PS: I am going to start using the topic labeling that MarianV is using as we cannot create our own tags to further group related posts. (Great idea @MarianV).