How to use Remote Validation correctly?

Asked

Viewed 202 times

0

I’m trying to use the Remote Validation to check if the email is already registered, the problem is that whenever I inform an email for example [email protected] does not return that the email is already registered and it is registered, and if I put [email protected] ai returns the validation that the email is already registered, very strange that.

How to solve this problem ?

I’m trying like this.

User class

[Serializable]
public class Usuario{

    public virtual long id              { get; set; }

    [Required(ErrorMessage = "Informe o nome")]
    [StringLength(50, MinimumLength = 5, ErrorMessage = "Campo nome deve ter de 5 a 50 caracteres")]        
    public virtual String nome          {get;set;}

    [Remote("isExistEmail", "Usuario", ErrorMessage = "Email já cadastrado!")]
    [EmailBrasil(EmailRequerido=true)]        
    public virtual String email         {get;set;}

    [Required(ErrorMessage="Informe a senha")]
    [SenhaBrasil(SenhaTamanhoMinimo = 8, SenhaTamanhoMaximo = 8, SenhaForteRequerida = false, CaracterEspecialRequerido = false)]
    public virtual String senha         {get;set;}

    public virtual int status           {get; set;} //1 ativo, 2 inativo, 0 aguardando

    public Usuario() { }

 }

Method that checks if the email is registered in the user controller.

public JsonResult isExistEmail(String email){
     Boolean isExist = dao.isExist(email);            
     if (isExist){                
         return Json(true, JsonRequestBehavior.AllowGet);
     }else{
         return Json(false, JsonRequestBehavior.AllowGet);
     }
}

DAO checking the existence of the email in the database

//verifica se ja existe o email cadastrado
public Boolean isExist(String email){
    ISession _session = getSession();
    IList<Usuario> list = _session.CreateQuery("FROM Usuario u WHERE u.email = :email")
       .SetParameter("email", email)            
        .SetMaxResults(1)
        .List<Usuario>();
    if (list.Count > 0){
        return true;
    }
    return false;
}

HTML

<div class="form-group">
     <label for="email" class="cols-sm-2 control-label">Email <img src="~/Imagens/required.png" height="6" width="6"></label>
      <div class="cols-sm-10">
          <div class="input-group">
             <span class="input-group-addon"><i class="glyphicon glyphicon-envelope" aria-hidden="true"></i></span>
                 @Html.TextBoxFor(model => Model.email, new { Class = "form-control", placeholder = "Informe o email", maxlength = 255 })
          </div>
                 @Html.ValidationMessageFor(model => model.email)
      </div>
</div>

1 answer

1


Solved, by what I read in the documentation when there is the occurrence, in case the email, the return in JSON should be false and not true as I was trying, I made the change and it worked 100%.

I did so.

public JsonResult isExistEmail(String email){
            Boolean isExist = dao.isExist(email);            
            if (isExist){                
                return Json(false, JsonRequestBehavior.AllowGet);
            }else{
                return Json(true, JsonRequestBehavior.AllowGet);
            }
        }
  • Try to edit the record?

  • @Virgilionovic edit the record ?

  • Use routine now to change the record? It’s just a concern...

  • @Virgilionovic didn’t quite understand what you say to edit the record. But the registration is being changed normally, the email is unique for system access and the user cannot change this email. But whenever I change a record I pick it up at Nhibernate’s Séssion and change it with the update. Ta blazinha this, running 100%

  • 1

    OK Fernando. Congratulations

Browser other questions tagged

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