Cells and columns can be validated individually as well as marked required.
Source
<script type="text/javascript">
var myRegex = /^\d+$/; // regex only digits
// function must return callback(bool)
function validateEquals6(value, callback) {
if (value != 6) {
callback(false);
} else {
callback(true);
}
}
</script>
<pe:sheet id="sheet" value="#{sheetAjaxController.assets}" var="row" height="400" rowKey="#{row.assetId}"
showRowHeaders="true" sortBy="#{row.assetId}" sortOrder="ascending" width="1000"
widgetVar="sheetWidget">
<p:ajax event="change" listener="#{sheetAjaxController.cellChangeEvent}"/>
<f:facet name="header">
<h:outputText value="Assets"/>
</f:facet>
<pe:sheetcolumn headerText="Id (readOnly)" readOnly="true" value="#{row.assetId}" colWidth="150"/>
<pe:sheetcolumn headerText="Required" value="#{row.hostName}" colWidth="140"
required="true" requiredMessage="This field is required!"/>
<pe:sheetcolumn id="colBigDecimal" headerText="BigDecimal(1-10)" value="#{row.purchasePrice}" colWidth="100" styleClass="htRight">
<f:converter converterId="javax.faces.BigDecimal"/>
<f:validateDoubleRange minimum="1.7" maximum="10.5" for="sheet"/>
</pe:sheetcolumn>
<pe:sheetcolumn headerText="Integer" value="#{row.value1}" colWidth="100" styleClass="htRight" onvalidate="numeric">
<f:converter converterId="javax.faces.Integer"/>
</pe:sheetcolumn>
<pe:sheetcolumn headerText="Regex" value="#{row.value2}" colWidth="100" styleClass="htRight" onvalidate="myRegex">
<f:converter converterId="javax.faces.Integer"/>
</pe:sheetcolumn>
<pe:sheetcolumn headerText="JS Exactly 6" value="#{row.value3}" colWidth="100" styleClass="htRight" onvalidate="validateEquals6">
<f:converter converterId="javax.faces.Integer"/>
</pe:sheetcolumn>
<pe:sheetcolumn headerText="Faces Exactly 5" value="#{row.value4}" colWidth="100" styleClass="htRight"
validator="#{sheetAjaxController.validateExactly5}">
<f:converter converterId="javax.faces.Integer"/>
</pe:sheetcolumn>
<f:facet name="footer">
<h:outputText value="#{sheetAjaxController.assets.size()} Records"/>
</f:facet>
</pe:sheet>
@Named
@ViewScoped
public class SheetAjaxController extends SheetController {
private static final long serialVersionUID = 20120224L;
/**
* Ajax callback from the Sheet component when a cell value is changed.
*/
@Override
public void cellChangeEvent(final SheetEvent event) {
final Sheet sheet = event.getSheet();
final List<SheetUpdate> updates = sheet.getUpdates();
// only show 1 update
SheetUpdate sheetUpdate = IterableUtils.first(updates);
final Long id = (Long) sheetUpdate.getRowKey();
final Object oldValue = sheetUpdate.getOldValue();
final Object newValue = sheetUpdate.getNewValue();
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Update Success",
String.format("Asset %s updated. Old Value = %s, New Value = %s", id, oldValue, newValue)));
sheet.commitUpdates();
}
/**
* Ajax callback from the Sheet component when a column is selected.
*/
public static void columnSelectEvent(final SheetEvent event) {
final Sheet sheet = event.getSheet();
final int column = sheet.getSelectedColumn() + 1;
FacesContext.getCurrentInstance().addMessage(null,
new FacesMessage("Column Selected", String.format("Column %d selected.", column)));
}
/**
* Ajax callback from the Sheet component when a row is selected.
*/
public static void rowSelectEvent(final SheetEvent event) {
final Sheet sheet = event.getSheet();
final int row = sheet.getSelectedRow() + 1;
FacesContext.getCurrentInstance().addMessage(null,
new FacesMessage("Row Selected", String.format("Row %d selected.", row)));
}
public static void validateExactly5(final FacesContext context, final UIComponent comp, final Object value) {
if (context == null || comp == null) {
return;
}
final Integer integer = (Integer) value;
if (integer.intValue() != 5) {
final FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error",
"Value must only be 5 exactly!");
throw new ValidatorException(message);
}
}
}