Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion demo/AspNetCore/AspNetCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,26 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="TanvirArjel.CustomValidation.AspNetCore" Version="1.2.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\TanvirArjel.CustomValidation.AspNetCore\TanvirArjel.CustomValidation.AspNetCore.csproj" />
<ProjectReference Include="..\..\src\TanvirArjel.CustomValidation\TanvirArjel.CustomValidation.csproj" />
</ItemGroup>

<ItemGroup>
<Compile Update="Resources\Common.Designer.cs">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
<DependentUpon>Common.resx</DependentUpon>
</Compile>
</ItemGroup>

<ItemGroup>
<EmbeddedResource Update="Resources\Common.resx">
<Generator>PublicResXFileCodeGenerator</Generator>
<LastGenOutput>Common.Designer.cs</LastGenOutput>
</EmbeddedResource>
</ItemGroup>

</Project>
21 changes: 20 additions & 1 deletion demo/AspNetCore/Controllers/EmployeeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public ActionResult Index()
// GET: Employee/Create
public ActionResult Create()
{
return View();
return View(new Employee());
}

// POST: Employee/Create
Expand All @@ -29,5 +29,24 @@ public ActionResult Create(Employee employee)

return View(employee);
}

// GET: Employee/Create
public ActionResult CreateWithResx()
{
return View(new EmployeeWithResx());
}

// POST: Employee/Create
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult CreateWithResx(EmployeeWithResx employee)
{
if (ModelState.IsValid)
{
return View();
}

return View(employee);
}
}
}
3 changes: 3 additions & 0 deletions demo/AspNetCore/Models/Employee.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using AspNetCore.Models;
using Microsoft.AspNetCore.Http;
using TanvirArjel.CustomValidation.AspNetCore.Attributes;
using TanvirArjel.CustomValidation.Attributes;
Expand Down Expand Up @@ -70,6 +71,8 @@ public class Employee : IValidatableObject
[RequiredIf(nameof(IsPhoneRequired), ComparisonType.Equal, true)]
public string Phone { get; set; }

public PreviousJobExperience[] PreviousJobs { get; set; } = new PreviousJobExperience[2];

public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
List<ValidationResult> validationResults = new List<ValidationResult>();
Expand Down
96 changes: 96 additions & 0 deletions demo/AspNetCore/Models/EmployeeWithResx.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using AspNetCore.Models;
using AspNetCore.Resources;
using Microsoft.AspNetCore.Http;
using TanvirArjel.CustomValidation.AspNetCore.Attributes;
using TanvirArjel.CustomValidation.Attributes;

namespace AspNetCore.CustomValidation.Demo.Models;

public class EmployeeWithResx : IValidatableObject
{
////[DisplayName("Name")]
////[FixedLength(5, ErrorMessage = "{0} should be exactly {1} characters long.")]
////[TextEditorRequired]
[Required]
[MinLength(5)]
[AspNetCore.MyCustomAttributes.FooAtrribute]
public string FirstName { get; set; }

[RequiredIf(nameof(FirstName), ComparisonType.Equal, "Tanvir", ErrorMessageResourceType = typeof(Common), ErrorMessageResourceName = "RequiredError")]
public string LastName { get; set; }

[Required]
[Range(1000, 9999)]
[Display(Name = "Starting Year")]
public int? StartingYear { get; set; }

[Required]
[Range(1000, 9999)]
[CompareTo(nameof(StartingYear), ComparisonType.GreaterThanOrEqual, ErrorMessageResourceType = typeof(Common), ErrorMessageResourceName = "GreaterThanOrEqualError")]
[Display(Name = "Ending Year")]
public int? EndingYear { get; set; }

////[DataType(DataType.Date)]
////[DisplayName("Date Of Birth")]
[Required]
[MinAge(15, 11, 3, ErrorMessageResourceType = typeof(Common), ErrorMessageResourceName = "MinAgeError")]
[MaxAge(20, 11, 3, ErrorMessageResourceType = typeof(Common), ErrorMessageResourceName = "MaxAgeError")]
public DateTime DateOfBirth { get; set; }

////[MinDate(2019, 1, 1, ErrorMessage = "{0} should be minimun 2019 January 1.")] // 2019 January 1
////[MaxDate(2019, 10, 1, ErrorMessage = "{0} cannot be greater than {1}.")] // 2019 October 1
////[CompareTo(nameof(DateOfBirth), ComparisonType.GreaterThan)]
[DisplayName("Joining Date")]
////[RequiredIf(nameof(DateOfBirth), ComparisonType.Equal, "01-May-2020")]
public DateTime? JoiningDate { get; set; }

[Display(Name = "First Number")]
[RequiredIf(nameof(SecondNumber), ComparisonType.Equal, null, ErrorMessageResourceType = typeof(Common), ErrorMessageResourceName = "RequiredError")]
public int? FirstNumber { get; set; }

////[RequiredIf(nameof(FirstNumber), ComparisonType.Equal, null)]
[Display(Name = "Second Number")]
[CompareTo(nameof(FirstNumber), ComparisonType.GreaterThan, ErrorMessageResourceType = typeof(Common), ErrorMessageResourceName = "GreaterThanError")]
public int? SecondNumber { get; set; }

////[FileType(new FileType[] { FileType.Mp4, FileType.Mp3 }, ErrorMessage = "{0} should be in {1} formats.")]
////[FileMinSize(10000, ErrorMessage = "{0} should be at least {1}.")]
[FileType(FileType.Jpeg, ErrorMessageResourceType = typeof(Common), ErrorMessageResourceName = "FileTypeError")]
[FileMinSize(1024, ErrorMessageResourceType = typeof(Common), ErrorMessageResourceName = "FileMinSizeError")]
public IFormFile Photo { get; set; }

public TimeSpan? EntryTime { get; set; }

[RequiredIf(nameof(EntryTime), ComparisonType.GreaterThan, "10:00", ErrorMessageResourceType = typeof(Common), ErrorMessageResourceName = "RequiredError")]
public TimeSpan? OutTime { get; set; }

public bool IsPhoneRequired { get; set; }

[RequiredIf(nameof(IsPhoneRequired), ComparisonType.Equal, true, ErrorMessageResourceType = typeof(Common), ErrorMessageResourceName = "RequiredError")]
public string Phone { get; set; }

public PreviousJobExperience[] PreviousJobs { get; set; } = new PreviousJobExperience[2];

public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
List<ValidationResult> validationResults = new List<ValidationResult>();

// FileOptions fileOptions = new FileOptions()
// {
// FileTypes = new FileType[] {FileType.Jpeg,FileType.Jpg},
// MinSize = 124,
// MaxSize = Convert.ToInt32(AppSettings.GetValue("DemoSettings:MaxFileSize"))
// };

// ValidationResult fileValidationResult = validationContext.ValidateFile(nameof(Photo), fileOptions);
// validationResults.Add(fileValidationResult);

// ValidationResult minAgeValidationResult = validationContext.ValidateMinAge(nameof(DateOfBirth), 10, 0, 0);
// validationResults.Add(minAgeValidationResult);
return validationResults;
}
}
19 changes: 19 additions & 0 deletions demo/AspNetCore/Models/PreviousJobExperience.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using System.ComponentModel.DataAnnotations;
using TanvirArjel.CustomValidation.Attributes;

namespace AspNetCore.Models
{
public class PreviousJobExperience
{
[DataType(DataType.Date)]
public DateTime? StartDate { get; set; }

[DataType(DataType.Date)]
[CompareTo(nameof(StartDate), ComparisonType.GreaterThan)]
public DateTime? EndDate { get; set; }

[RequiredIf(nameof(StartDate), ComparisonType.NotEqual, null)]
public string JobName { get; set; }
}
}
126 changes: 126 additions & 0 deletions demo/AspNetCore/Resources/Common.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading