Problem editing records information that has uniqueness verification

1

I'm developing a web application using the Play Framework. In the play itself has the @Unique annotation that guarantees me that the same data can not be saved in the bank. I added this note to the username and email fields of my form, it works correctly, the problem is that when I click edit my employee and do not change this data the annotation prevents me from doing the procedure. Example, I will change only the name of the employee, without touching the other fields, the annotation verifies that email and user name is already registered and does not let save the change. So can anyone help me solve this problem?

Iamusingthesameregistrationformtoperformeditingofforms.

<divclass="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 controller officers:

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);
}
    
asked by anonymous 18.07.2017 / 16:22

1 answer

1

Do this:

'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");
        }'
    
05.01.2018 / 16:26