How to add Validation in Asp.net core form | Data Annotation in asp.net core| validate form
Hello & As-salam u alikum ! , In this article we will learn to validate the form fields in asp.net core.
Note : For Beginner Guide Articles :
https://dotnetcorecommunity.blogspot.com/p/aspnet-core-tutorials-paracticals.html
Step# 1:
Watch previous article for creating a new record and upload Image in asp.net core
Step# 2:
Go to the model folder ---> tbl_user and add the following annotations that I have added below.
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
namespace WebApplication7.Models
{
public partial class TblUser
{
public TblUser()
{
TblComments = new HashSet<TblComments>();
TblFriendListFlReceiverNavigation = new HashSet<TblFriendList>();
TblFriendListFlSenderNavigation = new HashSet<TblFriendList>();
TblPost = new HashSet<TblPost>();
TblPostsLiked = new HashSet<TblPostsLiked>();
TblVerificationcode = new HashSet<TblVerificationcode>();
}
[Key]
public int UId { get; set; }
[Display(Name ="Email")]
[RegularExpression(@"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$", ErrorMessage ="Invalid Email")]
[StringLength(50,ErrorMessage ="Email Should be less than 50 characters or greater than 7 characters",
MinimumLength = 15)]
[Required(ErrorMessage ="*")]
public string UEmail { get; set; }
[Display(Name = "Password")]
[Required(ErrorMessage = "*")]
public string UPassword { get; set; }
[Required(ErrorMessage = "*")]
[Display(Name = "Name")]
[StringLength(25, ErrorMessage = "Name Should be less than 50 characters or greater than 7 characters",
MinimumLength = 3)]
public string UUserName { get; set; }
[Display(Name = "Profile Picture")]
public string UProfilePicture { get; set; }
[Display(Name = "Added Date")]
[Required(ErrorMessage = "*")]
public DateTime? UAddedDate { get; set; }
[Display(Name = "Modified Date")]
public DateTime? UModifiedDate { get; set; }
[Display(Name = "Is Login")]
[Required(ErrorMessage = "*")]
public int? UIsuserlogin { get; set; }
[Display(Name = "Is Approved")]
[Required(ErrorMessage = "*")]
public int? UIsuseraproved { get; set; }
public virtual ICollection<TblComments> TblComments { get; set; }
public virtual ICollection<TblFriendList> TblFriendListFlReceiverNavigation { get; set; }
public virtual ICollection<TblFriendList> TblFriendListFlSenderNavigation { get; set; }
public virtual ICollection<TblPost> TblPost { get; set; }
public virtual ICollection<TblPostsLiked> TblPostsLiked { get; set; }
public virtual ICollection<TblVerificationcode> TblVerificationcode { get; set; }
}
}
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
namespace WebApplication7.Models
{
public partial class TblUser
{
public TblUser()
{
TblComments = new HashSet<TblComments>();
TblFriendListFlReceiverNavigation = new HashSet<TblFriendList>();
TblFriendListFlSenderNavigation = new HashSet<TblFriendList>();
TblPost = new HashSet<TblPost>();
TblPostsLiked = new HashSet<TblPostsLiked>();
TblVerificationcode = new HashSet<TblVerificationcode>();
}
[Key]
public int UId { get; set; }
[Display(Name ="Email")]
[RegularExpression(@"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$", ErrorMessage ="Invalid Email")]
[StringLength(50,ErrorMessage ="Email Should be less than 50 characters or greater than 7 characters",
MinimumLength = 15)]
[Required(ErrorMessage ="*")]
public string UEmail { get; set; }
[Display(Name = "Password")]
[Required(ErrorMessage = "*")]
public string UPassword { get; set; }
[Required(ErrorMessage = "*")]
[Display(Name = "Name")]
[StringLength(25, ErrorMessage = "Name Should be less than 50 characters or greater than 7 characters",
MinimumLength = 3)]
public string UUserName { get; set; }
[Display(Name = "Profile Picture")]
public string UProfilePicture { get; set; }
[Display(Name = "Added Date")]
[Required(ErrorMessage = "*")]
public DateTime? UAddedDate { get; set; }
[Display(Name = "Modified Date")]
public DateTime? UModifiedDate { get; set; }
[Display(Name = "Is Login")]
[Required(ErrorMessage = "*")]
public int? UIsuserlogin { get; set; }
[Display(Name = "Is Approved")]
[Required(ErrorMessage = "*")]
public int? UIsuseraproved { get; set; }
public virtual ICollection<TblComments> TblComments { get; set; }
public virtual ICollection<TblFriendList> TblFriendListFlReceiverNavigation { get; set; }
public virtual ICollection<TblFriendList> TblFriendListFlSenderNavigation { get; set; }
public virtual ICollection<TblPost> TblPost { get; set; }
public virtual ICollection<TblPostsLiked> TblPostsLiked { get; set; }
public virtual ICollection<TblVerificationcode> TblVerificationcode { get; set; }
}
}
Go to the controller and add the following condition:
this condition will let your code execute when every field value is fine and validated.
this condition will let your code execute when every field value is fine and validated.
[HttpPost]
public async Task<IActionResult> Create(TblUser ui, IFormFile file)
{
if (ModelState.IsValid)
{
TblUser u = new TblUser();
if (file == null || file.Length == 0)
{
u.UProfilePicture = "noimage.png";
}
else
{
string filename = System.Guid.NewGuid().ToString() + ".jpg";
var path = Path.Combine(
Directory.GetCurrentDirectory(), "wwwroot", "img", filename);
using (var stream = new FileStream(path, FileMode.Create))
{
await file.CopyToAsync(stream);
}
u.UProfilePicture = filename;
}
u.UUserName = ui.UUserName;
u.UPassword = ui.UPassword;
u.UAddedDate = DateTime.Now;
u.UIsuseraproved = ui.UIsuseraproved;
u.UIsuserlogin = ui.UIsuserlogin;
u.UEmail = ui.UEmail;
db.TblUser.Add(u);
await db.SaveChangesAsync();
return RedirectToAction("Index");
}
else
{
return View();
}
}
public async Task<IActionResult> Create(TblUser ui, IFormFile file)
{
if (ModelState.IsValid)
{
TblUser u = new TblUser();
if (file == null || file.Length == 0)
{
u.UProfilePicture = "noimage.png";
}
else
{
string filename = System.Guid.NewGuid().ToString() + ".jpg";
var path = Path.Combine(
Directory.GetCurrentDirectory(), "wwwroot", "img", filename);
using (var stream = new FileStream(path, FileMode.Create))
{
await file.CopyToAsync(stream);
}
u.UProfilePicture = filename;
}
u.UUserName = ui.UUserName;
u.UPassword = ui.UPassword;
u.UAddedDate = DateTime.Now;
u.UIsuseraproved = ui.UIsuseraproved;
u.UIsuserlogin = ui.UIsuserlogin;
u.UEmail = ui.UEmail;
db.TblUser.Add(u);
await db.SaveChangesAsync();
return RedirectToAction("Index");
}
else
{
return View();
}
}
Step# 4:
Run the application and submit the button with wrong and blank input fields.You will find the errors as shown below.
0 Comments:
Post a Comment
Do not Add Spam links in the Comment Box
Subscribe to Post Comments [Atom]
<< Home