3. Date Property
When you need to work with calendar dates — like start dates, end dates, or other dates — you should use a DateProperty. This property type is built for date values and helps users easily select a date using a calendar picker. It ensures that your app handles dates correctly and consistently, whether you're using them in forms, filters, or reports.
For example, the DateOfLeave property can be defined using AddDateProperty<DateOnly?>, which means it accepts a date value that can also be left empty (nullable). This will be shown in the browser as an HTML <input type="date">
. The property supports both general configuration options (like display name or validation rules) and date-specific settings such as minimum/maximum allowed dates or custom date formatting. This gives you full control over how date inputs behave in your app.
publicpartialclassEmployee:IBkEntity<Employee,ApplicationDbContext>
{
publicvoidBkConfiguration(IBkEntityDesigner<Employee,ApplicationDbContext>designer)
{
// Note: The AddDateProperty
{
// Sets the display format for the date.// Example: "dd/MM/yyyy" displays the date as day/month/year. c.Format("dd/MM/yyyy");
// Define what type of date picker to use c.HtmlInputDateType(BkDateFieldType.Date);
// Sets the earliest valid date the user can select. c.MinValue(2000,1,1);
// Sets the latest valid date the user can select. c.MaxValue(2100,1,1);
// Sets the default value to the current date.// The second parameter controls whether the user can change the default value. c.DefaultValue(DateOnly.FromDateTime(DateTime.Now),BkCanUserChangeValue.No);
// Sets the label displayed next to the date picker. c.Label("Date of Leave");
// Sets the placeholder shown in the input when no date is selected. c.PlaceHolder("Select a date");
// Adds a tooltip that appears when hovering over the date field. c.ToolTip("Select the date for the employee leave");
// Applies a custom CSS class to the input field. c.CssClass("leave-date-picker");
// Hides the editor in the form by default. c.HideEditor();
// Hides the column in the grid view by default. c.HideGridViewColumn();
// Sets the tab order for the editor in the form. c.TabIndex(3);
// Adds a custom validation rule based on the selected date value.// For example, the leave date must be today or in the future. c.ValidationRuleValue(value=>value>=DateOnly.FromDateTime(DateTime.Today),"Leave date must be today or later","ValidateLeaveDateFuture");
// Adds a custom validation rule using the entire model.// Example: ensure the leave date is after the employee's hire date. c.ValidationRuleModel(model=>model.DateOfLeave>model.HireDate,"Leave date must be after hire date","ValidateLeaveAfterHire");
// Adds a custom validation rule using the database context.// Example: prevent more than one leave record on the same date for the same employee. c.ValidationRuleUsingDbContext((dbContext,model)=>
{
varexists=dbContext.Leaves.Where(x=>x.EmployeeId==model.EmployeeId&&x.Id!=model.Id).Any(x=>x.DateOfLeave==model.DateOfLeave);
return!exists;
},"A leave record already exists for this date","ValidateUniqueLeaveDate");
// Applies an existing .NET validation attribute to the date field.// For example, using RequiredAttribute for non-nullable fields (not needed if nullable). c.ValidationRuleUsingExistingAttribute(newRequiredAttribute());
});
}
}
Configuration Options for AddDateProperty<DateOnly?>
Method | Description | Parameters |
---|---|---|
Format |
Specifies the display format for the date in the input field. | string format |
HtmlInputDateType |
Define what kind of date piker to use | BkDateFieldType enumeration |
MinValue |
Sets the earliest selectable date in the input field. | int year, int month, int day |
MaxValue |
Sets the latest selectable date in the input field. | int year, int month, int day |
DefaultValue |
Sets a default value for the date input and specifies if the user can change it. | DateOnly defaultValue ,BkCanUserChangeValue canUserChange |
Label |
Sets the label text that appears next to the date picker. | string label |
PlaceHolder |
Sets the placeholder text shown when the date is empty. | string placeholder |
ToolTip |
Adds a tooltip that appears when the user hovers over the date field. | string tooltip |
CssClass |
Applies a custom CSS class to the date picker for styling. | string className |
HideEditor |
Hides the date picker from the form by default. | None |
HideGridViewColumn |
Hides the column for this property in the grid view by default. | None |
TabIndex |
Sets the order of the editor in the form when using the Tab key for navigation. | int tabIndex |
ValidationRuleValue |
Adds a custom validation rule based on the value of the date field. | Func<DateOnly?, bool> rule ,string errorMessage ,string attributeName |
ValidationRuleModel |
Applies a custom validation rule using the entire model. | Func<TModel, bool> rule ,string errorMessage ,string attributeName |
ValidationRuleUsingDbContext |
Adds a custom validation rule that uses the database context and the model. Useful for validating business rules like preventing duplicates. | Func<DbContext, TModel, bool> rule ,string errorMessage ,string attributeName |
ValidationRuleUsingExistingAttribute |
Applies an existing .NET validation attribute, such as RequiredAttribute . |
ValidationAttribute attribute |