Problem editing registration information that has unicity check

Asked

Viewed 43 times

1

I’m developing a web application using the play framework. In the play itself has the @Unique annotation that ensures that equal data cannot be saved in the database. I added this annotation to the username and email fields of my form, it works correctly, the problem is that by clicking edit my employee and not changing this data the annotation prevents you from doing the procedure. Example, I will change only the name of the employee, without touching the other fields, the annotation checks that email and username is already registered and does not let save the change. So someone can help me solve this problem? inserir a descrição da imagem aqui

I am using the same registration form to make the Forms edition.

 <div class="row">
 <form action="@{funcionarios.salvarFuncionarios}" method="post">
    <div class="col-lg-6">
        <input type="hidden" name="funcionario.id" value="${f?.id}" />
        <div class="form-group">
            <label>Nome completo:</label> <input type="text"
                name="funcionario.nome" class="form-control"
                value="${flash['funcionario.nome'] ? flash['funcionario.nome'] : f?.nome}">
            <span class="alert-danger">#{error 'funcionario.nome' /}</span>
        </div>

        <div class="form-group">
            <label>Email:</label> <input type="text" name="funcionario.email"
                class="form-control"
                value="${flash['funcionario.email'] ? flash['funcionario.email'] : f?.email}">
            <span class="alert-danger">#{error 'funcionario.email' /}</span>
        </div>

        <div class="form-group">
            <label>Função:</label> <select name="funcionario.funcao"
                class="form-control"
                value="${flash['funcionario.funcao'] ? flash['funcionario.funcao'] : f?.funcao}">
                <option>Administrador</option>
                <option>Suporte</option>
                <option>Supervisor</option>
            </select> <span class="alert-danger">#{error 'funcionario.funcao' /}</span>
        </div>

        <div class="form-group">
            <label>Nível de acesso:</label> <select
                name="funcionario.nivelAcesso" class="form-control"
                value="${flash['funcionario.nivelAcesso'] ? flash['funcionario.nivelAcesso'] : f?.nivelAcesso}">
                <option>Administrador</option>
                <option>Suporte</option>
            </select> <span class="alert-danger">#{error 'funcionario.nivelAcesso'
                /}</span>
        </div>
    </div>
    <div class="col-lg-6">
        <div class="form-group">
            <label>Nome de usuário:</label> <input type="text" placeholder="Mínimo 5 caracteres"
                name="funcionario.login" class="form-control"
                value="${flash['funcionario.login'] ? flash['funcionario.login'] : f?.login}">
            <span class="alert-danger">#{error 'funcionario.login' /}</span>
        </div>
        <div class="form-group">
            <label>Senha:</label> <input type="password" placeholder="Mínimo 6 caracteres"
                name="funcionario.senha" class="form-control"
                value="${flash['funcionario.senha'] ? flash['funcionario.senha'] : f?.senha}">
            <span class="alert-danger">#{error 'funcionario.senha' /}</span>
        </div>
        <div class="form-group">
            <label>Confirmar senha:</label> <input type="password" name="senha" placeholder="Mínimo 6 caracteres"
                class="form-control"> <span class="alert-danger">#{error
                'senha' /}</span>
        </div>
    </div>
    <div class="col-lg-12">
        <button type="submit" class="btn btn-success">Salvar</button>
        <button type="reset" class="btn btn-danger"
            onclick="window.location.href='/funcionarios/listagemFuncionarios';">
            Cancelar</button>
    </div>
</form>

My employee controller:

public static void salvarFuncionarios(@Valid Funcionario funcionario, String senha) throws Exception {

    if (validation.hasErrors() || !funcionario.senha.equals(senha)) {
        params.flash();
        validation.keep();
        formFuncionarios();
    }
    funcionario.senha = Crypto.passwordHash(senha);
    String mensagem = "Cadastro realizado com sucesso!";
    flash.success(mensagem);
    funcionario.save();
    listagemFuncionarios(null);
}

public static void editarFuncionarios(Long id) {
    Funcionario f = Funcionario.findById(id);
    renderTemplate("Funcionarios/formFuncionarios.html", f);
}
  • When you try edit a record, by changing your email and user, a new record is created in the bank ?

  • yes, that’s right

1 answer

0


Do so:

`Agente funcionarioBanco = Agente.find("email = ? or login = ?", funcionario.email, funcionario.login).first();

    if (funcionario.id == null) {
        if (funcionarioBanco != null && funcionarioBanco.id != funcionarioBanco.id && funcionarioBanco.foto == null) {
            validation.addError("funcionario.email", "E-mail já existente");
            validation.addError("funcionario.login", "Usuário já existente");
            validation.addError("funcionario.foto", "Imagem já cadastrada");
        }`

Browser other questions tagged

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