-
Available since OmniFaces 1.3
The <o:validateUniqueColumn>
validates if the given UIInput
component in an UIData
component has an unique value throughout all rows, also those not visible by pagination. This validator works directly on the data model and may therefore not work as expected if the data model does not represent all available rows of the UIData
component (e.g. when there's means of lazy loading).
The default message is
{0}: Please fill out an unique value for the entire column. Duplicate found in row {1}
Usage
Usage example:
<h:dataTable value="#{bean.items}" var="item">
<h:column>
<h:inputText value="#{item.value}">
<o:validateUniqueColumn />
</h:inputText>
</h:column>
</h:dataTable>
In an invalidating case, only the first row on which the value is actually changed (i.e. the value change event has been fired on the input component in the particular row) will be marked invalid and a faces message will be added on the client ID of the input component in the particular row. The default message can be changed by the message
attribute. Any "{0}" placeholder in the message will be substituted with the label of the input component. Any "{1}" placeholder in the message will be substituted with the 1-based row index of the data model. Note that this does not take pagination into account and that this needs if necessary to be taken care of in the custom message yourself.
<o:validateUniqueColumn message="Duplicate value!" />
<h:form>
<h3>Please fill out a unique value in all inputs of this column.</h3>
<h:dataTable value="#{validateUniqueColumnBean.entities}" var="entity">
<h:column>
#{component.namingContainer.rowIndex + 1}
</h:column>
<h:column>
<h:inputText id="value" label="Value" value="#{entity.value}" required="true">
<o:validateUniqueColumn />
</h:inputText>
</h:column>
<h:column>
<h:message id="message" for="value" />
</h:column>
</h:dataTable>
<h:commandButton value="Submit">
<f:ajax execute="@form" render="@form" />
</h:commandButton>
<h:outputText value="OK!" rendered="#{facesContext.postback and not facesContext.validationFailed}" />
</h:form>
package org.omnifaces.showcase.validators;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import jakarta.annotation.PostConstruct;
import jakarta.inject.Named;
import org.omnifaces.cdi.ViewScoped;
import org.omnifaces.showcase.model.ExampleEntity;
@Named
@ViewScoped
public class ValidateUniqueColumnBean implements Serializable {
private static final long serialVersionUID = 1L;
private List<ExampleEntity> entities;
@PostConstruct
public void init() {
entities = new ArrayList<>();
entities.add(new ExampleEntity(1L, "one"));
entities.add(new ExampleEntity(2L, "two"));
entities.add(new ExampleEntity(3L, "three"));
entities.add(new ExampleEntity(4L, "four"));
entities.add(new ExampleEntity(5L, "five"));
}
public List<ExampleEntity> getEntities() {
return entities;
}
}
package org.omnifaces.showcase.model;
import java.io.Serializable;
public class ExampleEntity implements Serializable {
private static final long serialVersionUID = 1L;
private Long id;
private String value;
public ExampleEntity() {
//
}
public ExampleEntity(Long id, String value) {
this.id = id;
this.value = value;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
@Override
public boolean equals(Object other) {
return (other instanceof ExampleEntity) && (id != null)
? id.equals(((ExampleEntity) other).id)
: (other == this);
}
@Override
public int hashCode() {
return (id != null)
? (this.getClass().hashCode() + id.hashCode())
: super.hashCode();
}
@Override
public String toString() {
return String.format("ExampleEntity[%d, %s]", id, value);
}
}