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

Asked

Viewed 56 times

0

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

Button where Seto 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>

Method save:

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.");
    }    
}

inserir a descrição da imagem aqui

When I go to edit a record, and inform an email that already exists when I click to save it from the notice on the screen, and update the record already.

1 answer

0

Try to do it that way:

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;
}

Browser other questions tagged

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