1

NetBeans 7.1.1 JSF2.1

When using converter="convK" attribute in h:selectManyCheckBox it all works well. But I tried to use @FacesConverter(forClass=className.class) form and it keeps giving me "Validation is not Valid" errors. I've tried changing it to forClass=packageName.className.class but no help.

This is converter:

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
 @FacesConverter( "convK")
  public class KorisnikConverter implements Converter{
@Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
     if (value==null) return value;
     if (value.isEmpty()) return value;
        for (int i=0; i<Arhiva.getSviKor().size(); i++) {
            if (Arhiva.getSviKor().get(i).getUsername().equals(value)) {
                return Arhiva.getSviKor().get(i);
            }
        }
     return value;
}
@Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
    if (value==null) return "";
    if (value instanceof Korisnik) return ((Korisnik)value).getUsername();
    return "";
}   
}

I have a class called Korisnik which has couple text fields, username is unique one. In my main managing bean I have couple arrayList of those objects. Goal is to use selectManyCheckBox to chose just some of users and put them in a separate arraylist for some other uses. I wanted to push entire objects around (I can always easily work with strings and have object creation and management in my controler beans but wanted to try custom converters to get selectItems to work with objects)

In my class I've overridden equals and hashCode (as there is a lot of talk about custom converters giving blah blah Validation is not valid errors).

@Override
public boolean equals (Object obj) {
    if (obj==null) return false;
    if (!(obj instanceof Korisnik)) return false;
    Korisnik k = (Korisnik)obj;
    return (this.username==k.username);
}

@Override
public int hashCode() {
    return this.username.hashCode();
}

Edit. When I'm using it as named converter and using said converter only in that one instance with selectManyCheckbox it works fine even without overriding equals and hashCode.

This is checkbox code

  <h:selectManyCheckbox value="#{kontrolg.izabrAut}" layout="pageDirection" converter="convK" >
              <f:selectItems value="#{kontrolg.moguciAut}" var="it" itemLabel="#    {it.ime}&#160;#{it.prezime}" itemValue="#{it}"/>
      </h:selectManyCheckbox>

What I don't know is whether I'm failing to properly use forClass="whatever" in converter annotation or my converter actually works ok with that one selectManyCheckbox, but when I specify it in forClass form it gets used for all instances of that object and causes some other code that worked nice before adding custom converters to now give "validation is not valid" error?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
user1493545
  • 51
  • 1
  • 1
  • 4

1 Answers1

0

The value is not valid validation error will be thrown when the equals() method on the selected item has not returned true for any of the available items.

And indeed, your equals() method is broken. The following line is wrong:

return (this.username==k.username);

I'll assume that username is a String, which is an Object. The == compares Objects by reference, not by their value. In other words, when performing == on two Objects, you're basically testing if they point to exactly the same instance. You're not checking if they represent the same value (say, the Object instance's internal representation). You should be using the Object's equals() method instead, the String#equals() method, here's an extract of relevance from its javadoc:

equals

public boolean equals(Object anObject)

Compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object.

The == is only applicable when comparing primitives like boolean, int, long, etc or when testing for null.

So, to fix your problem, replace the wrong line by the following line:

return username.equals(k.username);

Or, when they can possibly be null:

return (username == null) ? (k.username == null) : username.equals(k.username);

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thanks for reply, but it isn't working still. I changed check to equals (I forgot about that because I was reusing equals() code I saw in help answers for other people asking about Validation Error: Value is not valid. – user1493545 Jul 01 '12 at 09:43
  • I also tried using NetBeans to generate equals and hashCode.... same error. And I'm completely puzzled why it works 100% even without overriding equals and hash code when I specify converter by name as attribute of h:selectManyCheckBox. Also, forgot to mention. I don't get error on page load, just when I click submit on that checkbox form. Which works without errors if I name the converter and dont use forClass... – user1493545 Jul 01 '12 at 09:54