When returning validation error in the form, object gets the values entered

0

On the screen I have a datatable that I fill in with a list of objects. Using f: setPropertyActionListener I hedge a list object to an auxiliary object and show it in a dialog. In the save method I say some validations, when it returns an error, the list object gets the values that I typed in the auxiliary object (both have the same reference).

Button where I hide the value in the auxiliary object:

<p:commandButton icon="fa fa-edit" title="Editar" process="@this" update="dialogEmailForm" oncomplete="abrirJanela('adicionarEmail'); return false;" styleClass="btn btn-flat btn-default ui-button-grid">
    <f:setPropertyActionListener value="#{email}" target="#{clienteCadastroBean.clienteEmailSelecionado}" />
</p:commandButton>

Dialog:

<p:dialog header="Email" id="adicionarEmail" widgetVar="adicionarEmail" modal="true" resizable="false" closeOnEscape="true" draggable="false" styleClass="dialog-size-pequena" position="top" onShow="pageScroll('hide')" onHide="pageScroll('show')">   

        <div id="adicionarEmailConteudo" style="overflow-x: hidden;overflow-y: auto;">  
            <h:form id="dialogEmailForm">
                <p:messages autoUpdate="true" closable="true" />
                <div class="row">
                    <div class="col-md-12">
                        <p:outputLabel value="Contato" for="contato" />
                        <p:inputText id="contato" value="#{clienteCadastroBean.clienteEmailSelecionado.contato}" maxlength="30" class="form-control" />
                    </div>
                </div>
                <div class="row">
                    <div class="col-md-12">
                        <p:outputLabel value="E-mail" for="email" />
                        <p:inputText id="email" value="#{clienteCadastroBean.clienteEmailSelecionado.email}" maxlength="100" class="form-control minusculo" />
                    </div>
                </div>
                <div class="row">
                    <div class="col-md-12">
                        <p:outputLabel value="Observação" for="observacao" />
                        <p:inputText id="observacao" value="#{clienteCadastroBean.clienteEmailSelecionado.observacao}" maxlength="50" class="form-control" />
                    </div>
                </div>
                <div align="right" style="padding-top: 20px">       
                    <p:commandButton actionListener="#{clienteCadastroBean.salvarEmail()}" class="btn btn-success" value="Salvar" 
                        update=":formClienteCadastro:emailGrid :dialogEmailForm" />
                    <p:spacer height="5" /> 
                    <p:commandButton process="@this" class="btn btn-default" value="Fechar" 
                        oncomplete="fecharJanela('adicionarEmail'); return false;" />
                </div>
            </h:form>
        </div>

</p:dialog>

Save method:

public void salvarEmail() {
    if (clienteEmailSelecionado.getId() == null) {  
        Long tamanhoLista = cliente.getClienteEmails().stream().filter(e -> clienteEmailSelecionado.getEmail().equals(e.getEmail())).count(); 
        verificaExistente(tamanhoLista);
        if (cliente.getId() != null) {
            ClienteEmail clienteEmailSavad = clienteEmailService.salvar(clienteEmailSelecionado);
            cliente.getClienteEmails().add(clienteEmailSavad);
        } else {
            cliente.getClienteEmails().add(clienteEmailSelecionado);
        }   
    } else {
        Long tamanhoLista = cliente.getClienteEmails().stream().filter(e -> clienteEmailSelecionado.getEmail().equals(e.getEmail())).filter(e -> !clienteEmailSelecionado.equals(e)).count();
        verificaExistente(tamanhoLista);               
        if (cliente.getId() != null) {
            clienteEmailService.salvar(clienteEmailSelecionado);
        }            
    }
    closeDialog("adicionarEmail");   
}
private void verificaExistente(Long tamanhoLista) {
    if (tamanhoLista > 0) {
        throw new TaskDoValidateException("Este endereço de e-mail já está cadastrado.");
    }    
}

When I edit a record, and I report an email that already exists when I click save it gives the warning on the screen, and updates the record already.

    
asked by anonymous 22.09.2017 / 18:35

1 answer

0

Try to do this:

public void salvarEmail() {
    if (clienteEmailSelecionado.getId() == null) {  
        Long tamanhoLista = cliente.getClienteEmails().stream().filter(e -> clienteEmailSelecionado.getEmail().equals(e.getEmail())).count(); 
        if(verificaExistente(tamanhoLista)) {
            throw new TaskDoValidateException("Este endereço de e-mail já está cadastrado.");
            return;
        }
        else
        {
            if (cliente.getId() != null) {
                ClienteEmail clienteEmailSavad = clienteEmailService.salvar(clienteEmailSelecionado);
                cliente.getClienteEmails().add(clienteEmailSavad);
            } else {
                cliente.getClienteEmails().add(clienteEmailSelecionado);
            }
        }
        return;
    } else {
        Long tamanhoLista = cliente.getClienteEmails().stream().filter(e -> clienteEmailSelecionado.getEmail().equals(e.getEmail())).filter(e -> !clienteEmailSelecionado.equals(e)).count();
        if(verificaExistente(tamanhoLista)) {
            throw new TaskDoValidateException("Este endereço de e-mail já está cadastrado.");
            return;
        }
        else
        {               
            if (cliente.getId() != null) {
                clienteEmailService.salvar(clienteEmailSelecionado);
            }       
        } 

        return;    
    }
    closeDialog("adicionarEmail");   
}

private bool verificaExistente(Long tamanhoLista) {
    if (tamanhoLista > 0) {
        return true;
    }
    return false;
}
    
22.09.2017 / 18:49