How to check whether a WD Java Input Field marked as “required” contains any data
This blog assumes that you know how to write a Web Dynpro application and have a functional SAP NetWeaver Java AS or a suitably deployed Tomcat server. It will not provide any further explanation beyond stating what steps need to be performed.
The main functionality for this process is an extension of the generic UITreeVisitor class that is documented in section 10.4 of my book “Inside Web Dynpro for Java” (pages 327-331) If you don’t yet own a copy of my book, then shame on you! 🙂The only coding needed in the component controller is that necessary in wdDoInit() to create two elements in the InputData context node. You can download the wdDoInit() code snippet here .
- Open the message Pool editor and create 2 messages called “EmptyStringField” and “EmptyNumericField”
Notice that both messages are of type “error” and that they have a single parameter identified by ““
After you have created these messages, you must save all your changes by pressing Ctrl-Shift-S. This forces the code generator to run and allows you programmatic access to the declared messages.
- Open the view editor and create the following view layout:
Set the RootElement’s LayoutManager to MatrixLayout
The easiest way to add these UI elements is to right-click on the RootElement in the outline view and select “Apply template”. Then select “Form” and add all the attributes in the node InputData.
- In the outline view, Ctrl-click on each of InputField UI elements in order to select all of them. then change the “state” property from “normal” to “required”. A red star will then appear next to the corresponding label field.
- Create an action in the view controller called “DoButtonPush”. Give this action a parameter called “Action” of type integer. Make sure that this action is a validating action.
- Now add two buttons to the screen. As can be seen from the screen shot above, these buttons are for the “Previous” and “Next” functionality. Assign the same action “DoButtonPush” to both buttons.
- In the Outline view, right-click on the “Previous” button and select “Parameter Mapping”. In the popup window, press the “Create Constant” button and assign the value 0 to the action parameter.
- Repeat step 11 for the “Next” button, but this time, assign the constant value of 1.
- Switch to the Navigator view (the tab next to the Web Dynpro Explorer) and expand the “Resources” tree until you reach the “input_validator” directory. Here, create a new Java file called UiTreeVisitor.java as per the screen shot below
Download the coding for the UiTreeVisitor class and insert it into this file and then save it.
- Open the Java editor for the view controller InputValidatorView. Download the controller coding for each of the following methods and insert it at the appropriate place in the controller:
- “[Others | http://whealy.com/images/Web%20Dynpro/Input%20Validator/View%20Controller%20Others.java]” declarations (at the end of the source code)
- [wdDoInit() | http://whealy.com/images/Web%20Dynpro/Input%20Validator/View%20Controller%20wdDoInit.java]
- [wdDoModifyview() | http://whealy.com/images/Web%20Dynpro/Input%20Validator/View%20Controller%20wdDoModifyView.java]
- [wdDoBeforeAction() | http://whealy.com/images/Web%20Dynpro/Input%20Validator/View%20Controller%20wdDoBeforeAction.java]
- [onActionDoButtonPush() | http://whealy.com/images/Web%20Dynpro/Input%20Validator/View%20Controller%20onActionDoButtonPush.java]
- Now organize the view controller’s imports by pressing Ctrl-Shift-O and save the changes.
- Create a new application called InputValidatorApp.
- Deploy and run this application and you should see something like the following screen
- h2. Running the application
If you simply press the “Next” or “Previous” buttons, you will swap the display between the two elements in node InputData.
Now blank out one of the fields and try pressing “Next” or “Previous”.
You will see an error message that is linked to the UI element that is missing an input value. Click on the error message at the bottom of the screen and the cursor will be relocated to the field in error.
If you blank out multiple fields, then you will get multiple error messges, each linked to the approriate UI element. The error message is tailored to whether the input field should contain a string or numeric value. This functionality can be extended if you wish to create specific messages for time and date fields.
!http://whealy.com/images/Web%20Dynpro/Input%20Validator/ErrorScreen1.png|height=614|alt=Error screen with multiple messages|width=500|src=http://whealy.com/images/Web%20Dynpro/Input%20Validator/ErrorScreen1.png|border=0!
There are a few things to note about this solution!
This is fine as long as you are disciplined to restrict your access to read-only.
Please do not start noodling around with IWDView by adding new UI elements outside the scope of wdDoModifyView()! You risk doing bad things to your data by potentially causing context supply functions run multiple times, or causing the framework to render the screen multiple times.
The vector of messages passed to the UiTreeVisitor class is rather kludgey, but it saved me having to noodle around with reflection in the UiTreeVisitor class in order to discover what messages were contained if I’d passed the component’s IMessage class.