jsf- validator with parameters from input

10,309

Solution 1

I don't think that the OP is interested in the problem any more, but if someone else finds this question:

int id=(Integer) component.getAttributes().get("foo");

Will not work as you get a String. A little strange that you write that you always get null. You should get a:

java.lang.String cannot be cast to java.lang.Integer

If this is the case, try:

int id = Integer.parseInt((String) component.getAttributes().get("foo"));

Solution 2

In the attribute you need to set the value of the expression

<f:attribute name="foo" value="controller.mitarbeiter.id" />

it should be

<f:attribute name="foo" value="#{controller.mitarbeiter.id}" />
Share:
10,309
JavaNullPointer
Author by

JavaNullPointer

Updated on June 17, 2022

Comments

  • JavaNullPointer
    JavaNullPointer almost 2 years

    here a part of my .xhtml page:

    <h:inputText id="kartNumIn"  value="#{controller.mitarbeiter.kartenNummer}">
                    <f:attribute name="foo" value="controller.mitarbeiter.id" />
                    <f:validator validatorId="kartVal" binding="#{kartVal}" disabled="#{!controller.noUpdate}"/>
                </h:inputText>
    

    here my validate-method():

    @Override
        public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
    
            int id=(Integer) component.getAttributes().get("foo"); //always 0
            int temp = (Integer) value;
    
            if (!(value instanceof Integer)) {
                throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "Eingabefehler", "FEHLER:Bitte geben Sie eine Zahl ein!"));
            }
    
           System.out.print("Input"+value+"Aktuelle"+component.getAttributes().get("foo").toString());
    
            if (getAlleKartennummern().contains(temp) && temp!=id) {
                throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "Kartennummerfehler", "FEHLER:Kartennumer bereits vergeben!"));
            }
        }
    

    for my validator i need a second value. Here i need the mitarbeiter.id! for component.getAttributes().get("foo") i get always null....

  • BalusC
    BalusC over 11 years
    I believe this is careless preparation of the question. OP mentioned that he got null, which would be impossible with a static string. The cause is more likely that the attribute value is dependent on a variable which is only known during view render time, such as the var of a <h:dataTable>.