Autocomplete converter with validator problem

UI Components for JSF
Post Reply
warriorshadow
Posts: 42
Joined: 12 Sep 2013, 15:34

27 Jan 2015, 19:31

Hi,

Please I appreciate your help with this issue. I have a autocomplete with an abstract converter. Normally, it works well, except when I use a validator or a f:validator. When I put the validator on the autocomplete, it goes to the generic converter, but in instead of returning the object, it always put the id value in the autocomplete input. Normally it does well this flow, by putting the object and making reference to the attribute of the class in the itemLabel. But only when I put a validator, the behavior is to put the id, making insecure the system by putting a id directly. What could be the problem.

this is the autocomplete code:

Code: Select all


   <p:autoComplete id="menuAutoComplete"                             
                                    dropdown="true"
                                    value="#{menuBean.entity.menu}" 
                                    var="menu"
                                    itemLabel="#{menu.name}" 
                                    itemValue="#{menu}"
                                    completeMethod="#{menuBean.completeMenu}"
                                    converter="#{menuBean.converter}"
                                    forceSelection="false" 
                                    required="#{menuBean.menuNeeded}"
                                    size="100"
                                    validator="menuValidator"
                                    >
                        <p:ajax event="itemSelect"
                                listener="#{menuBean.handleSelectMenu}" 
                                update="systemOutputLabel, systemOutputPanel, moduleOutputLabel, moduleOutputPanel"
                                oncomplete="PF('modulebui').show();PF('systembui').show()"
                                />

this is the abstractConverter

Code: Select all


public class GenericConverter implements Converter {

    protected EntityService service;

    @Autowired
    public void setService(EntityService service) {
        this.service = service;
    }

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        if (value == null || value.length() == 0) {
            return null;
        }
        Long id = Long.parseLong(value);
        return service.find(id);
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        return value instanceof BaseEntity ? ((BaseEntity) value).getId().toString() : "";
    }
}

And this is the validator:

Code: Select all


@Component
@Scope("request")
@FacesValidator("menuValidator")
public class MenuValidator implements Validator {

    @Autowired
    UsuarioService usuarioService;
    @Autowired
    MessagesController messagesController;

    @Override
    public void validate(FacesContext fc, UIComponent uic, Object value) throws ValidatorException {
        if(value == null){
            return;
        }
        
        Menu menu = (Menu) value;
        
         if(menu.getNombre().equals("Menu1") ){
            FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "test error",
                    "test error");
            throw new ValidatorException(msg);
        }
               
    }
}


thanks in advance,

Darío
Primefaces 5.2
Apache Tomcat 7.0.34
NetBeans 7.3.1
Linux Ubuntu

User avatar
andyba
Expert Member
Posts: 2473
Joined: 31 Mar 2011, 16:27
Location: Steinfeld, near Bremen/Osnabrück, DE
Contact:

28 Jan 2015, 14:23

With autocomplete you should only be making valid objects available for choice which begs the question: why do you need to validate the input?
PF 4.x (Elite versions), PF 5, Pf 5.1, PF 6.0
Glassfish 4.1, Mojarra 2.x, Java 8, Payara 4.1.1.
If you haven't read the forum rules read them now

warriorshadow
Posts: 42
Joined: 12 Sep 2013, 15:34

31 Jan 2015, 00:15

andyba wrote:With autocomplete you should only be making valid objects available for choice which begs the question: why do you need to validate the input?
It just an example. The validation could be another thing diferent, that checks if the chosen value is something, do something or update some and even compare if a value is greater or less than other. The problem here is why my converter is having this behavior with the id object.
Primefaces 5.2
Apache Tomcat 7.0.34
NetBeans 7.3.1
Linux Ubuntu

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 45 guests