Archive

Posts Tagged ‘Validation’

XPages: Collection of advices

Hi folks,

here is a small collection of some hints which were written by my colleagues and myself throughout time.

Naming of attributes in Internet Explorer

It seems, in the Internet Explorer, Dojo has some problems finding the correct element, while usingattribute selectors, if there is a string reserved by the system in the attribute. (e.g. name or id). Because Dojo uses the selector engine “Sizzle” like JQuery, the same phenomenon can appear using JQuery.

An example for that is an input-field with the name “username”. If you select dojo.query(‘input[id$=”WFApprovalVFL”]’) the field “username” will be selected, too, although it doesn’t end with WFApprovalVFL.

The same happens with keynames in JSON-objects.

Not rendered datasources will always be computed

If you use a datasource in a custom control, the code of the datasource will always be computed, no matter whether you set rendered or loaded to false. The code will always be computed. This could lead to errors hard to track down.

Furthermore, if you use several datasources in different custom controls, which point to the same document, all these datasources will be saved and, eventually, the document is saved more than once with saveing conflicts. To avoid this, you can reference the datasource in your custom controls directyl via the name, or you can pass on the datasource via a parameter of the custom control (compositeData). Therefore you have to select the class of the datasource, which doesn’t appear in the list of available types. The name of the class of a datasource is com.ibm.xsp.model.ModelDatasource.

Check whether a viewPanel is categorized

If we have a view control in our XPage which name is viewPanel1 the following code:

var model:com.ibm.xsp.model.domino.DominoViewDataModel = getComponent("viewPanel1").getDataModel();
return model.getCategoryIndentLevel();

returns the value 0 if view is categorized, it returns -1 if not.

Use case: If you sort a view, the content of a categorized column gets lost. With that you can check whether the view was sorted and reset the view.

Avoid the onChange bug in Internet Explorer when clicking on a CheckBoxGroup

If you use RadioButtonGroups or CheckBoxGroups in an XPage or a custom control, you could face the problem in the Internet Explorer, that after a partial refresh the submitted values of the group are the values from a previous request. This occurs if you click the label of instead of the box, directly. The reason is that the IE submits the partial refresh before setting the new value from the UI.

rendered="#{javascript:context.getUserAgent().isIE()}"


Example
:

There is a CheckboxGroup with the options A, B, C and D. In the onChange event, a partial refresh will be fired on another panel, which hides or shows some elements in dependence of the chosen value. If you click the label for Option A, there is no submitted value. If you click the label of option B, the value A is submitted. If you click after that on the label of option C, Ab and B will be submitted.


Workaround
:

You can fire the partial refresh a second time in the onComplete event of the onClick event.

 <xp:this.onComplete>
<![CDATA[XSP.partialRefreshPost("#{id:refreshPanelID}");]]>
</xp:this.onComplete>

The workaround above doesn’t work if there are additional actions or server-side code. The code will be executed only on the first refresh. To get this event-handler working again, you have to hang out the click-event from the standard-action and execute it by yourself. You can achieve that with a scriptblock (The name of the control is Output-Script, you can find it if you open the list of other controls in the sidebar of the Domino Designer) Because the checkbox itself is inside the label, you have to seperate it from the own execution.

 <xp:scriptBlock id="scriptBlock1"
        rendered="#{javascript:context.getUserAgent().isIE()}">
        <xp:this.value><![CDATA[dojo.addOnLoad(function(){
    dojo.query("label",dojo.byId("#{id:checkBoxGroupID}")).forEach(function(el){        
        dojo.connect(el,"onclick",el,function(e){
            if (e.target.type != "checkbox") {
                e.preventDefault();
                dojo.query("input",this).forEach(function(el){
                    dojo.attr(el,"checked",!(dojo.attr(el,"checked")));
                });
            }    
        });
    });        
});]]></xp:this.value>
</xp:scriptBlock>

Another advantage of this method, compared to workaround 1, is that you don’t need a second request.

Validation of two dependen input-fields

While validating two fields dependent of each other, you have to read the value of the field which is validated. If you compare the value of the second field, validation will not be fired after the first fail and the validation error persists.

Example:

 <xp:inputText id="recipientNotesID"
   value="#{test.recipientNotesID}"
   disableClientSideValidation="true" required="true">
   <xp:this.validators>
      <xp:validateRequired message="Required value."></xp:validateRequired>
   </xp:this.validators>
</xp:inputText>
<xp:inputText id="recipientNotesPW"
   value="#{test.recipientNotesPW}"
   disableClientSideValidation="true" required="true">
   <xp:this.validators>
      <xp:validateExpression>
         <xp:this.expression><![CDATA[#{javascript:((getComponent("recipientNotesPW").getSubmittedValue()||"")!=(getComponent("recipientNotesID").getValue()||""))}]]>
         </xp:this.expression>
         <xp:this.message><![CDATA[#{javascript:"Values must not be equal."}]]></xp:this.message>
      </xp:validateExpression>
      <xp:validateRequired message="Required value."></xp:validateRequired>
   </xp:this.validators>
</xp:inputText>

Stolperfalle: Validierung

26/09/2011 1 comment

Heute bin ich auf ein faszinierendes Phänomen gestoßen.

Problem war folgendes:

In einer Anwendung hatten wir 2 Comboboxen. Beide wurden mit Werten aus einem Setup-Dokument befüllt. Zusätzlich fügten wir per Javascript (serverseitig) noch einen Leerstring und den Wert New… hinzu. Bei Auswahl von New… wurde ein Dialog aufgerufen.

1. Phänomen: Schloss man den Dialog so stand der Wert in der Combobox noch auf New… Wollte man nun speichern, so wurde die serverseitige Validierung angestoßen, dort tauchte die Meldung “Entry not found in Index” auf. Keiner wusste woher diese Meldung kam. Ein paar Printouts verrieten uns, dass beim ersten Speichern noch der alte Wert im Frontenddokument stand, also der Wert der vor der Auswahl von New… in der Combobox war. Konsequenz daraus, beim ersten Submit auf ein Feld wird bei fehlgeschlagener Validierung der Wert nicht ins Backend submittet, denn selbst ein getComponent(“feldname”).value brachte uns noch den alten Wert zurück. Beim zweiten Submit, wird der wert dann doch übertragen und erneut validiert. Dies kann zu merkwürdigen und schwer nachzu vollziehenden Fehlern führen.

2. Phänomen: Wo kam die Meldung “Entry not found in Index” her? Nach einigem hin und her probieren wurde klar woher er kam: Aus der Form.
Unglaublich aber war, die serverseitige Validierung schleift Fehlermeldungen von anderen Feldern aus der Form durch. Wir hatten in der Datasource des Dokuments definiert, dass beim Speichern der Datasource die Form computed werden soll. In der Form wiederum hatten wir ein Feld, welches aus der Referenz (der Wert der Combobox entsprach per Alias einer UNID) einen Klartextwert für die Volltextsuche machte. Dort wurde ein @DBLookup durchgeführt. Dieser nahm sich den Wert aus dem Feld (New…) und sucht entsprechend danach. Komischerweise gelangt der Wert nicht ins Backend, aber in den Einflussbereich dieser berechneten Formel. Da der Wert nicht da war wurde ein Fehler geworfen. Nämlich “Entry not found in Index”. Dieser wurde dann durchgeschliffen und auf der XPage angezeigt.

Daraus ergab sich die Möglichkeit in dem eigentlichen Feld, wo der Wert aus der Combobox gespeichert wird, in der InputValidation eine Formel zu hinterlegen welche unseren Feldwert validiert und entsprechend mit @Failure() eine Fehlermeldung zurückgibt. Auch diese wurde dann in der XPage angezeigt. So ergeben sich völlig neue Möglichkeiten die Validierung zu erweitern.

Stumbling Stone: FileUpload-Control

Today I had a tiny problem. Not even a bug, only a small booby trap.

On my page, I had three FileUpload controls and one FileDownload control. It was specified that a user can upload a maximum of three attachments. Therefore I added a rendering condition to all FileUpload controls, which chekcs the number of attachments in the document.
WARNING: The formula @Attachments doesn’t seem to work properly. It seems like @Attachment only checks the backend-document. I want to know how much attachments were uploaded right now to constrain the possibility to upload something. The function “doc.getAttachmentList(‘feldname’).size()” saved my day.

The three controls are placed below another, the first will be hidden if there are thee attachments, the second if there are two and the last one if there is at least one attachment. All this linked to a refresh, which is fired when you delete one attachment, everything works fine.

However, I faced the effect, if I add 3 attachments at once, only two were saved. The attachment from the last FileUpload control was never saved.

Get ready for the reason:

Controls seem to be processed sequential, connected with an internal refresh. That means, the first control is processed and the file is uploaded -> new there is a refresh -> second control is processed, everything is fine, so the second attachment is uploaded -> there is a refresh again -> third control wants to be processed, but there is more than one attachment in the document, so the control is not rendered anymore, therefore, the third attachment goes to hell.

The solution is quite easy, you only have to reverse the order of the number of attachments in the render condition. That means, the first control should be hidden if there is more than one attachment, the second is hidden with two attachments and the third with three attachments. Because the controls are processed in order they are in the code, everything works fine.

Another interesting fact is, that if you have some mandatory fields or have another server-side validation on your form, attachments are uploaded when you click the save button. After that the validation yells. The backend document doesn’t get bothered, but in the frontend document, there are your attachments uploaded, yet. In the context of my problem above, this could be quite confusing, but if you know that fact you can work around that.

XPages: Validation of Tabbed Panels

This article shall start my Blog about the horror we call XPages.

One or another has certainly noticed that, when you use a TabbedPanel on an XPage, the whole validation of the form is screwed. If you change to another tab, where are no fields which should be vaildated, you can save the whole document without filling the mandatory fields on the other tabs.

This is, IBM-typically no bug, it’s a feature.
If the tab is not shown, it isn’t rendered, so he doesn’t exist. Therefore it is clear that the validation of this tab can’t be executed.

IBM knows this problem, there is already an Enhancement-Request since February 2010… unfortunately there hasn’t happen anything
This is the official workaround: 10.Lotus.com

Unfortunately, this isn’t quite elegant.

Here is another solution:

If you add a save-event in the onClick-event of every tab in the tabbedPanel, the XPage tries to save the document everytime you want to change the tab, which fires the validation. The catch is, of cource, you have to accept that the document is saved on every change of the tab. This solution is only vaild, if you have all your mandatory fields on one tab.

Attention: You shouldn’t check “Process Data without validation”. Also you should pay attention, that the document is only saved in the edit-mode.

%d bloggers like this: