1
votes

I have a form, there is a field called uppercase_value. In that field, I put @UpperCase(uppercase_value) in Input Translation because I need the value in that field is uppercase only.

In the xpage, I have a edit box and a button. The edit box uses simple data binding (bind to uppercase_value) and the button is for save the value.

Now I have to prevent the button saves duplicate value or empty value, I put a computed field near the edit box and put the following code in the button.

//get the value from the edit box
var newvalue = getComponent("inputText1").getValue();

//if edit box is empty, show message in the computed field
if(newvalue =="" || newvalue ==null)
{
    getComponent("computedField1").setValue("The field is empty");
}
else
{
    //check the value whether it is already existed or not
    //use @DbLookup to find, newvalue is the keyword 
    var existedvalue = @DbLookup(@DbName(),"myview", newvalue,3 );

    //if the newvalue is same as the existedvalue, show message in the computed field
    if(newvalue == existedvalue)
    {
        getComponent("computedField1").setValue("Duplicate value");
    }
    else
    {
        document1.save();
    }
}

I run the code, I type the value in lowercase, it saves it in uppercase as expected (for example, I type abc in edit box, it returns the value is ABC). I refresh the xpage and type the same value again, it can save the value and I can find there are two same value in the view.

I double check the view in @DbLookup, the first column is sorted so I am pretty sure the @DbLookup works fine.

I think the reason it can save duplicate value is in run form validation, I choose on document save. Therefore everytime I click the save button after type some values in the edit box, it changes the value to uppcase, then saves it. So if I refresh the page and type some value again, since I type in lowercase, the @DbLookup "thinks" it is a new value so it saves the value and thus there are duplicate values in the view.

But if I change on document save to on document load in run form validation. It does not work. I mean it does not change the value to uppercase and saves the lowercase value.

Actually I have another idea about change to uppercase. I intend to use onkeypress event in the edit box. I try to force the user to turn on the caps locks button when input value in the edit box

I follow this table and put the following code in the onkeypress event.

 if  (thisEvent.keyCode == 20 )
{
    //alert("caps lock button pressed");
    return true; // not work, still can type lowercase
//   event.returnValue = true; // cannot type anything 
}
else
{
    return false;
//    event.returnValue = false; // cannot type anything
}

However, it does not work properly, I can type lowercase in the edit box.

So how can I save the value in uppercase and prevent to save duplicate value?

1

1 Answers

2
votes

There are a variety of options.

  • Change upper-casing to onBlur event
  • Use Dojo Text Box component, which allows you to set format in All Properties (uppercase="true")
  • Use @UpperCase() before comparing the value with the result from @DbLookup
  • Use a String comparison library that allows case insensitive comparison (personally I work with Java and always include Apache's StringUtils, but there is an in-built IBM Java StringUtil class that may provide that functionality)
  • Use the Domino Object Model (database.getView(), View.getDocumentByKey(value, true)) to get any match, then check the UNID is not the same. OpenNTF Domino API provides this functionality out-of-the-box with View.checkUnique()

As a bonus, if you're running Domino 9.0.x (or 8.5.3 with the Extension Library) there is @ErrorMessage() which allows you to throw a validation error message that will appear in your Display Errors control rather than using a computed field. getComponent("intputText1").setValid(false) will also mark that component as in error.