Validation of Data Input
Any UI component on a web page that can be used to input data can be turned into a field where input is required.
You get this by setting the modeling option set DataEntryRequired = true;
.
Modeling¶
The following example makes input being required for the text field ‘Input1’.
layout PersonView {
link Children = PersonData;
}
display PersonData {
component Input1 : TextField {
set DataEntryRequired = true;
}
...
}
Generated Code in Web Fragment¶
The following code fragments are related to the above example model.
In PersonViewBean.java you find the following:
private boolean requiredInput1PersonData = true;
...
public boolean isRequiredInput1PersonData() {
...
}
In PersonViewBeanCustomization.java you find the same kind of code, which has the purpose of setting a default value and allowing to override that default setting from a web application.
private boolean requiredInput1PersonData = true;
...
public boolean isRequiredInput1PersonData() {
...
}
In personView_generated.xhtml you find the following code that uses the above methods. Please note that some code has been left out and a few line breaks are added here to make the sample code more readable.
...
<p:panelGrid
id="mainForm_personDataPanelGrid">
...
<p:inputText
id="personDataPanelGrid_input1"
...
required="#{personViewBeanCustomization.isRequiredInput1PersonData()
and
personViewBean.requiredInput1PersonData}"/>
...
</p:panelGrid>
If the isRequired...()
method in the PersonViewBeanCustomization
class returns false, then the corresponding UI component can be left empty before submitting the form in the browser.
Customize the code in that class when you do not want to force a user to provide input for the component.
If you want to force a user to provide input for the component depending on application logic, then set the field required...
in PersonViewBean
class accordingly.
Finally, if you want to overwrite the default settings for UI components, you have to manually write code in classes that are generated by the web application generator.
Generated Code in Web Application¶
The web application contains a generated Java class AppPersonViewBeanCustomization
. That class has the following code:
public Boolean isRequiredInput1PersonData() {
return null;
}
By default these methods return null
. When you do not want to force a user to provide input for a UI component, then let the corresponding method return false (return Boolean.FALSE;
).