Validations

This week I have been trying to learn how to do validations with the UIFramework. This is the last piece I need for finishing step one before it is ready to send off for code review and testing.

So one of the things that is helpful to know how to do is to use fragments. If you include a fragment for the fields you wish to validate rather than putting the field directly in your page it makes readability and validation much easier. For example, I have a new field I want required so I created a fragment called conceptClasses.gsp under (yourmodule)/omod/src/main/webapp/fragments and a controller called ConceptClassesFragmentController.java under (yourmodule)/omod/src/main/java/org/openmrs/module/(yourmoduleid)/fragment/controller. In the gsp I have the label and input field that looks like this (so far but may change as I work through the validation):

<%
config.require(“label”)
config.require(“formFieldName”)
%>

<p <% if (config.left) { %> <% } %> >

<label for=”${ config.id }-field”>
${ config.label } <% if (config.classes && config.classes.contains(“required”)) { %><span>(${ ui.message(“emr.formValidation.messages.requiredField.label”) })</span><% } %>
</label>

<input type=”text” id=”${ config.id }-field” name=”${ config.formFieldName }” value=”${ config.initialValue ?: ” }”
<% if (config.classes) { %>class=”${ config.classes}” <% } %>
autocomplete=”off” />

${ ui.includeFragment(“uicommons”, “fieldErrors”, [ fieldName: config.formFieldName ]) }
<% if (config.optional) { %>
${ ui.message(“emr.optional”) }
<% } %>
</p>

I can then include by adding the following code to my page:

${ ui.includeFragment(“conceptmanagementapps”, “field/conceptClasses”, [
label: “concept classes”,
size: 1,
formFieldName: conceptClass,
left: true,
classes: “required”

])}

Creating the form like this <form class=”simple-form-ui” method=”post”> will make it so that required fields show up with red.

Just for clarification. I did not end up using the UIFramework standard validation. It seems to only work when there is a need for the navigation on the side when there is dialogue that depends on the answers to other dialogue. You can see how this works by looking at the code to register a patient. There you can not leave the field for a person name until it is entered and then you can move on to other fields to finish answering the questions to register a patient.

I ended up just creating my own quick function to validate my 2 fields. However, it was important to me to understand how it should be done with the new framework if there is dialogue in case I need it in the future.

For showing errors in  the upload file we have decided to create a new spreadsheet and listing only the rows that failed and putting a column that shows the reason why it failed.

I may have to update this post with more info later as I am still working through the validation process.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s