FluentValidation check start date greater than end date - fluentvalidation

I have this C# code with FluentValidation rule which check start date should be less than end date. When I run to put start date greater than end date, it should show error "End date must after Start date".
RuleFor(r => r.StartDate)
.NotEmpty()
.WithMessage("Start Date is Required");
RuleFor(r => r.EndDate)
.NotEmpty().WithMessage("End date is required")
.GreaterThan(r => r.StartDate.Value)
.WithMessage("End date must after Start date")
.When(m => m.StartDate.HasValue);
In my model, these 2 properties defined as below:
[DataType(DataType.Date)]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:yyyy-MM-dd}")]
public DateTime? StartDate { get; set; }
[DataType(DataType.Date)]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:yyyy-MM-dd}")]
public DateTime? EndDate { get; set; }

Related

Date formatting in MVC view

I have below entity in a mvc model:
[Display(Name = "Date Of Birth")]
public DateTime? DateOfBirth { get; set; }
and I am using it in a view as:
<td title="#u.CreatedDate">
#if (#u.DateOfBirth != null)
{
#u.DateOfBirth
}
</td>
This is working piece of code but I want to show this date in dd/mm/yyyy format.
I have tried something like this:
1. string.Format("dd/MM/yyyy", u.DateOfBirth);
2. [Display(Name = "Date Of Birth")]
[DisplayFormat (DataFormatString="dd/MM/yyyy")]
public DateTime? DateOfBirth { get; set; }
But nothing works fine for me. Please help
3 options you can use
#Html.DisplayFor(m => u.DateOfBirth) // assumes you have [DisplayFormat(DataFormatString="{0:dd/MM/yyyy}")]
#String.Format("{0:dd/MM/yyyy}", u.DateOfBirth)
#u.DateOfBirth.Value.ToString("dd/MM/yyyy")
In the first 2 cases, you do not need the #if (#u.DateOfBirth != null) check

Date coming up as 12/30/1899 in MVC 4

In my model I have:
[DisplayName("Title Date")]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = ("{0:d}"))]
public Nullable<System.DateTime> DateOfTitle { get; set; }
My View has:
<td>#Html.DisplayFor(modelItem => Model.DateOfTitle)</td>
The value in the database for this field is null. I would like the DisplayFor to show nothing unless there is a valid date for this field. It displays as 12/30/1899.
Try using this..
[DisplayFormat(NullDisplayText = "",
ApplyFormatInEditMode = true, DataFormatString = ("{0:d}"))]

Overriding the default date format validation on the AccountModel in MVC4 Application

I have created a windows form based application and am using the simple membership AccountModel. On the registration form I am having problems with the date format when the application is uploaded to azure.
Locally I am able to enter the format "31/10/2013" but on the azure hosted application I must enter it in the format "10/31/2013". How can I override the validation so that it allows the use of DD/MM/YYYY format
My register model is as follows:
public class RegisterModel
{
[Required]
[Display(Name = "Username")]
public string UserName { get; set; }
[Required]
[Display(Name = "Forename")]
public string Forename { get; set; }
[Required]
[Display(Name = "Surname")]
public string Surname { get; set; }
[Required]
[Display(Name = "DOB")]
[DataType(DataType.Date)]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd.MM.yyyy}")]
public DateTime DOB { get; set; }
[Required]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
[DataType(DataType.Password)]
[Display(Name = "Confirm password")]
[Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
public string ConfirmPassword { get; set; }
[Required]
[DataType(DataType.Password)]
[Display(Name = "Secret Answer")]
public string SecretAnswer { get; set; }
}
I tried applying a dateformat to the DOB property as above but on azure it is still validating against MM/DD/YYYY
The FormatAttribute of the Mvc Controls Toolkit enables the developer to define the format to be used in the representation of a property on the client side, as shown in the example below :-
Format(Prefix="Date of birth is: ", ClientFormat = "d", NullDisplayText="No date of birth available")]
public DateTime? BirthDate { get; set; }
This way, the strings can be globalized as explained here.
Below the list of all supported date format:
Format Meaning "en-US"
f Long Date, Short Time dddd, MMMM dd, yyyy h:mm tt
F Long Date, Long Time dddd, MMMM dd, yyyy h:mm:ss tt
G Short Date, Long Time M/d/yyyy h:mm:ss tt
t Short Time h:mm tt
T Long Time h:mm:ss tt
d Short Date M/d/yyyy
D Long Date dddd, MMMM dd, yyyy
Y Month/Year MMMM, yyyy
M Month/Day yyyy MMMM

Time Input Field in ASP.NET MVC

I'm having trouble trying to write a form with time field using ASP.NET MVC 4.
Model:
class Abcde {
[DataType(DataType.Time)]
public DateTime ATime { get; set; }
}
View.cshtml
...
#Html.EditorFor(m => m.ATime)
#Html.ValidationMessageFor(m => m.ATime)
The validation always failed when I tried to input a time (hh:nn:ss), and said it was not in the corrent format, so i have to input a date instead.
My Question is should I change the type to TimeSpan? Or is there any other way?
[Required]
[DataType(DataType.Time)]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:H:mm}")]
public DateTime Time { get; set; }`
Try this, you can format in the DataFormatString the way you want it to work for you, im using it in a project now and it's working for me.
Actually this is what worked for me.
[DataType(DataType.Time)]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:HH:mm}")]
public DateTime Hour { get; set; }
[Required]
[DataType(DataType.Time)]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:HH:mm}")]
public DateTime Time { get; set; }
This code is correct. But it only works when you use EditorFor instead of TextBoxFor.
Use:
[Required]
[DataType(DataType.Time)]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:HH:mm:ss}")]
public DateTime FieldTime{ get; set; }
You need to match your date validation with the format that works internally for this annotation. So, adding the ApplyFormat passing a String Pattern you can get it.

Castle Validators how to Validate Is Lesser/Greater Than Or Equal

I see the ValidateIsLesser and ValidateIsGreater attributes. But what if I want to do a ValidateIsLesserOrEqual and/or ValidateIsGreaterOrEqual. Do I just need to write these custom attributes or is there pre-built capabilities for this that I am having a hard time finding?
[ValidateNonEmpty]
[ValidateDate]
[ValidateIsLesser(IsLesserValidationType.Date, "EndDate", "Start Date must be before End Date.")]
public DateTime StartDate { get; set; }
[ValidateNonEmpty]
[ValidateDate]
[ValidateIsGreater(IsGreaterValidationType.Date, "StartDate", "End Date must be after the Start Date.")]
public DateTime EndDate { get; set; }
You can use the ValidateSelf attribute and supply your own validation for the OrEqual comparisons:
[ValidateNonEmpty]
[ValidateDate]
public DateTime StartDate { get; set; }
[ValidateNonEmpty]
[ValidateDate]
public DateTime EndDate { get; set; }
[ValidateSelf]
public void ValidateDate(ErrorSummary errors)
{
if (StartDate >= EndDate)
errors.RegisterErrorMessage("StartDate", "Start date must be earlier than end date.");
}