I have a field in my Razor:
<div class="form-group">
@Html.LabelFor(m => m.CreateContractStep1.StartDate, new { @class = "control-label col-md-2" })
<div class="col-md-4">
<div class="admin-form theme-primary">
<label for="StartDate" class="field prepend-icon mbn">
@Html.TextBoxFor(m => m.CreateContractStep1.StartDate, new { id = "StartDate", @class = "gui-input datepicker", placeholder = "From...", data_bind = "value: CreateContractStep1.StartDate" })
@Html.ValidationMessageFor(m => m.CreateContractStep1.StartDate, string.Empty, new { @class = "text-danger fw400" })
<label class="field-icon">
<i class="fa fa-calendar-o"></i>
</label>
</label>
</div>
</div>
</div>
Currently it has some validation present so that the field cannot be left blank. However I would like to add some validation. Based on another field called Termination Date which is in the Model.
I have tried this in my Model:
[Display(Name = "From")]
[Required]
[GreaterThan("TerminationDate", true, "Termination date must be greater than or equal to Notification Date")]
public DateTime? StartDate { get; set; }
But it's not really working as expected. It does not bring back the Validation Message. I am thinking that I may need a Custom Validator Attribute.
Model:
#region Create Contract Step 1
public class ContractStep1ViewModel
{
public ContractStep1ViewModel()
{
// Default named type to contract.
NamedType = ContractNamedType.Contract;
}
[Display(Name = "Name *")]
[Required]
[StringLength(250, ErrorMessage = "The {0} has a maximum of {1} characters.")]
public string Name { get; set; }
[Display(Name = "Description")]
[StringLength(1000, ErrorMessage = "The {0} has a maximum of {1} characters.")]
public string Description { get; set; }
[Display(Name = "Contract Type *")]
[Required]
public ContractType? ContractType { get; set; }
[Display(Name = "Contract Or Sideletter *")]
[Required]
public ContractNamedType NamedType { get; set; }
[Display(Name = "Currency Used *")]
[Required]
public string Currency { get; set; }
[Display(Name = "From *")]
[Required]
[Compare("End Date")]
[DataType(DataType.DateTime)]
public DateTime? StartDate { get; set; }
[Display(Name = "To")]
[GreaterThan("StartDate", true, "End Date must be greater than or equal to Start Date")]
public DateTime? EndDate { get; set; }
[Display(Name = "Signed")]
public DateTime? SignedDate { get; set; }
}
public class TerminateContractViewModel
{
[Required]
public Guid ContractId { get; set; }
[Required]
public Guid RowVersion { get; set; }
[Display(Name = "Name")]
[Required]
public string Name { get; set; }
[Required]
public DateTime ContractStartDate { get; set; }
[Display(Name = "Notification Date")]
[Required]
[GreaterThan("ContractStartDate", false, "Notification date must be greater than or equal to Contract Start Date")]
public DateTime NotificationDate { get; set; }
[Display(Name = "Termination Date")]
[Required]
[GreaterThan("NotificationDate", true, "Termination date must be greater than or equal to Notification Date")]
public DateTime TerminationDate { get; set; }
[Display(Name = "Post Term Collection End Date")]
[Required]
[GreaterThan("TerminationDate", true, "Post term collection date must be greater than or equal to Termination Date")]
public DateTime PostTermCollectionEndDate { get; set; }
// search
public ContractSearchViewModel SearchModel { get; set; }
// pagination
public PagingModel PagingInfo { get; set; }
}
StartDateshould beLessThan("TerminationDate",...)and notGreaterThan)@Html.ValidationSummary(true)in page?