Form data validation in ASP.NET MVC

Asked

Viewed 706 times

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?

  • I would like a Server Side validation same.

1 answer

3

Good afternoon has way but simple server side validation would be using data Annotation it already has some properties ready but wanting you can customize I’ll put an example below.

using System.ComponentModel.DataAnnotations;

public class EadCapituloModel
{
    [Required(ErrorMessage = "Campo Capitulo é Obrigatorio")]
    [Display(Name = "Capitulo")]
    public string Capitulo { get; set; }
    [Required(ErrorMessage = "Campo tempo é obrigatório")]
    [Display(Name = "Tempo")]
    public string Tempo { get; set; }
    [Required(ErrorMessage = "Campo curso é obrigatório")]
    [Display(Name = "PlataformaId")]
    public int PlataformaId { get; set; }
    [Required]
    public int CursoId { get; set; }
    [Required(ErrorMessage = "Campo Nome do video obrigatório")]
    [Display(Name = "Video")]
    public string Video { get; set; }
    [Required(ErrorMessage = "Campo url é obrigatório")]
    [Display(Name = "URL")]
    [RegularExpression(@"^http(s)?://([\w-]+.)+[\w-]+(/[\w- ./?%&=])?$", ErrorMessage = "Url não é válida")]
    public string URL { get; set; }
}

follows a website that contains some explanations about each Annotation: http://www.entityframeworktutorial.net/code-first/dataannotation-in-code-first.aspx

Browser other questions tagged

You are not signed in. Login or sign up in order to post.