Is there any quicker and simpler way to validate a form's data in ASP.NET? so I do not have to be doing as many treatments as in the example below:
[HttpPost]
public ActionResult Attempt(string email, string password, Entities db)
{
ViewBag.email = email;
if (email == null || password == null || password.Length < 5)
{
TempData.Add("error", "E-mail ou senha inválidos.");
return RedirectToRoute("Admin.Login");
}
var admin = db.admins.Where(p => p.email == email).FirstOrDefault();
if (admin == null)
{
TempData.Add("error", "E-mail ou senha inválidos.");
return RedirectToRoute("Admin.Login");
}
if(BCryptHelper.CheckPassword(password, admin.password))
{
FormsAuthentication.SetAuthCookie(email, false);
}
TempData.Add("error", "E-mail ou senha inválidos.");
return RedirectToRoute("Admin.Login");
}
I'm looking for some more practical validation library, like this one in laravel, in php, for example:
$validator = Validator::make($request->all(), [
'email' => 'required|email|exists:users,email',
'password' => 'required|min:5'
], [
'required' => trans('errors.required'),
'min' => trans('errors.min'),
'email' => trans('errors.email'),
'exists' => trans('notifications.login_failed'),
], [
'email' => trans('all.email'),
'password' => trans('all.password'),
]);
That already returns to the View all Validator errors, if there are:
if($validator->fails())
return redirect()->route('login', $panel->handle)
->withInput($request->except('password'))->withErrors($validator);