Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Primefaces Chips Web Component

I have a CRUD and I want to change the inputTexArea:

<p:inputTextarea id="tags" value="#{myController.selected.tags}" />

To the new Primefaces chips component:

<p:chips id="tags" value="#{myController.selected.tags}" />

An entity class excerpt:

@Lob
@Size(max = 2147483647)
@Column(name = "tags")
private String tags;
//GETTER AND SETTER OMITTED

The get method works fine, because the tags are displayed in the field as expected:

public List<String> getTags() {
return Arrays.asList(tags.split(","));
}

But the set method is not, because when I click on Save, occurs an Exception:

public void setTags(List<String> tags) {
this.tags = String.join(",", tags);
}

java.lang.ClassCastException: java.util.ArrayList cannot be cast to java.lang.CharSequence
at org.hibernate.validator.internal.constraintvalidators.SizeValidatorForCharSequence.isValid(SizeValidatorForCharSequence.java:33)
at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.validateSingleConstraint(ConstraintTree.java:281)

Can someone help me, please ?

Thanks in advance.

ps.: I've already asked this to Primefaces team (https://forum.primefaces.org/viewtopic.php?f=3&t=51091), and a Primefaces core developer (Thomas Andraschko) orientated me to ask Hibernate validator team.

like image 703
jMarcel Avatar asked Mar 22 '17 21:03

jMarcel


2 Answers

It seems like Hibernate validator is confused with your getter returning List<String> for a String field. Try this:

public String getTags() {
    return tags;
}

public void setTags(String tags) {
    this.tags = tags;
}

public List<String> getTagsList() {
    return Arrays.asList(tags.split(","));
}

public void setTagsList(List<String> tags) {
    this.tags = String.join(",", tags);
}

And then:

<p:chips id="tags" value="#{myController.selected.tagsList}" />
like image 108
chimmi Avatar answered Sep 21 '22 02:09

chimmi


p:chips uses a list as a value, why don't you use this instead:

private String tags = "aaaa,bbb";

public List<String> getTags() {
    return Arrays.asList(tags.split(","));
}

public void setTags(List<String> tags) {
    this.tags = String.join(",", tags);
}
like image 42
Ouerghi Yassine Avatar answered Sep 18 '22 02:09

Ouerghi Yassine