1
Is there a faster and simpler way to validate a form’s data in ASP.NET? so that I don’t 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 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'),
]);
Which already returns to the View all Validator errors, if any:
if($validator->fails())
return redirect()->route('login', $panel->handle)
->withInput($request->except('password'))->withErrors($validator);
Jquery Validate already tried?
– Marconi
I would like a Server Side validation same.
– Rafael Alexandre