Tag Archives: OnRequesting

Field validation using JScript in OIS100/A

In this post I’ll give an example of how you can validate a field in OIS100 using JScript. The scenario is as follows: All orders with order type “E50” has to be from faclity “FC5”. How can you validate input in OIS100/A? This was the question from one of our readers. The script is pretty straightforward as you have all the data that you need to check on the panel so I created a small example to illustrate the use of OnRequesting. Please note that you could make this script better by using script arguments.

The scripts illustrates the following:

  • Cancelling navigation to the next panel
  • How to set a field value
  • How to read a field value
  • How to show a message (in the status bar or dialog depending on setting)
  • How to log to the client log file
  • How do disconnect the event handler

If the order type and the facility does not match the required condition the facility is updated and a message is displayed.

OIS100A

Below is the script:

import System;
import System.Windows;
import System.Windows.Controls;
import MForms;
import Mango.UI.Services;
import Mango.Core.Util;

package MForms.JScript {
  class FieldValidation {
    var logger : log4net.ILog = Mango.Core.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType)
      var controller,
    debug,
    content;
    var checkOrderType = "E50"; // Should be a parameter
    var mandatoryFacility = "FC5" // Should be a parameter

      public function Init(element : Object, args : Object, controller : Object, debug : Object) {
      debug.WriteLine("Script Initializing.");
      this.controller = controller;
      this.debug = debug;
      this.content = controller.RenderEngine.Content;

      // Attach event handlers to be able to detach event handlers and list to page down requests.
      controller.add_Requesting(OnRequesting);
    }

    public function OnRequesting(sender : Object, e : CancelRequestEventArgs) {
      try {
        LogDebug("onrequesting " + e.CommandType + " " + e.CommandValue);
        if (e.CommandType == "KEY" && e.CommandValue == "ENTER") {
          // Do not disconnect events on page down.
          var orderType = GetValue("OAORTP");
          LogDebug("Order type: " + orderType);

          if (orderType != null && orderType == checkOrderType) {
            var facilityElement = ScriptUtil.FindChild(content, "OAFACI");
            if (facilityElement != null) {
              var value = MFormsUtil.GetControlValue(facilityElement);
              LogDebug("Facility " + value);
              if (value != mandatoryFacility) {
                LogDebug("Set facilty to " + mandatoryFacility);
                facilityElement.Text = mandatoryFacility;
                e.Cancel = true;
                // Here we could show a dialog as well
                var message = "When using order type " + checkOrderType + " you must use facility " + mandatoryFacility;
                controller.RenderEngine.ShowMessage(message);
                // Return so we don't disconnect since we cancelled the request
                return
              }

            }
          }
          
        }
      } catch (ex) {
        LogDebug(ex);
      }
      controller.remove_Requesting(OnRequesting);
    }

    private function GetValue(fieldName) {
      var element = ScriptUtil.FindChild(content, fieldName);
      if (element != null) {
        var value = MFormsUtil.GetControlValue(element);
        return value;
      }
      debug.WriteLine("Could not find a field named " + fieldName + " on the current panel");
      return null;
    }

    private function LogDebug(message : String) {
      if (message != null) {
        logger.Debug(message);
        if (debug) {
          debug.WriteLine(message);
        }
      }
    }
  }
}