File validation on XPages - xpages

I am working on application in XPages and I need to do file upload validation on the server side.
I read that file upload only works on client side, so I tried to validate another filed that stores file name attached. I did it on the on change event of the file upload. The problem occurs when I delete an attached file- there is no on change event on file download control… any suggestions? Or maybe different way?
Thanks

Unsaved list of attachments can be accessed from the getAttachmentList("RTFieldName") method of the data source.
I suggest using a hidden input and custom validator to have more control, like:
<xp:message
id="message1"
for="inputHidden1"></xp:message>
<xp:br></xp:br>
<xp:inputHidden
id="inputHidden1"
value="arbitrary">
<xp:this.validators>
<xp:customValidator>
<xp:this.validate><![CDATA[{javascript:
// RTField the name of the rich text field that holds attachments
if(document1.getAttachmentList("RTField").size()==0) {
// You might want to do more checks here.
var inputHidden1 = getComponent("inputHidden1");
inputHidden1.setValid(false);
return "You have to upload a file!" // your error message
}}]]></xp:this.validate>
</xp:customValidator>
</xp:this.validators>
</xp:inputHidden>
Remember, value="arbitrary" is important. Empty fields will not trigger custom validators.

Related

xpages: how to simulate form's auto launch properties

Is there a quick and easy way to convert forms that have auto launch first attachment and/or launch URL?
I know it can be coded in SSJS but I was just wandering if someone had a quick way of doing this.
Thanks
The following Domino URL command will open an attachment ...
http://Host/DatabaseName/View/DocumentName/$File/fileattachmentname
... where the DocumentName is effectively the lookup value shown in the first column which is sorted.
Add an xp:link control and code it open the attachment (in this example in a new window). For example, the following is an xp:link that could be added in an xp:viewColumn or xp:repeat or any iterator control. In this example the var for the iterator is set to "rowData" and the name for the link is returned from the ListName column and the url to launch the attachment is in the cLinkUrl column.
<xp:link escape="true" id="link1" target="_blank">
<xp:this.text><![CDATA[#{javascript:rowData.getColumnValue("ListName");}]]></xp:this.text>
<xp:this.value><![CDATA[#{javascript:rowData.getColumnValue("cLinkUrl")}]]></xp:this.value>
</xp:link>
Since I wanted the code to be in the xpage, as it opens up either the document or the attachment depending on roles, I ended up adding this to the beforepageload event of the xpage (still need to add the role check to this though):
<xp:this.beforePageLoad>
<xp:executeScript>
<xp:this.script><![CDATA[#{javascript:var url = currentDocument.getDocument().getHttpURL();
var attachmentName = #AttachmentNames();
facesContext.getExternalContext().redirect(url.replace("?OpenDocument","/$File/"+attachmentName+"?OpenElement&target=_new"));}]]></xp:this.script>
</xp:executeScript>
</xp:this.beforePageLoad>

XPage file upload control loses filename on refresh

I'm sure I must be just missing something simple here but I've searched everywhere for an answer and no luck.
I have a bog standard file upload control on my xpage showing initially the standard "No file chosen", I select the "Choose file" button which overwrites the "No file chosen" with the selected filename - all good! BUT on any subsequent partial or full refresh of the page the filename is disappearing and reverting to "No file chosen". If I submit the form anyway the file is still uploaded correctly to the server but this is obviously no good for the user who thinks they have to reselect the file.
Can someone just tell me what I'm doing wrong here??
<xp:fileUpload id="songAlbumUpload1" value="#{submitfile.File1}">
<xp:eventHandler event="onchange" submit="true"
refreshMode="complete" disableValidators="true">
</xp:eventHandler>
</xp:fileUpload>
Note this was my latest attempt using a full refresh but I've tried all other partial refresh options too. Also my upload control is in a formTableRow so I tried moving it out into the main panel but the issue still occurs.
(I have also discovered the bug where a refresh ends up uploading multiple instances of the same file which as I understand it is fixed by a partial refresh of the panel the upload control is contained in - but of course this required refresh then causes the above issue anyway)!
Many thanks.
As indicated by Frantisek, upon refresh the fileUpload returns to a "No File Chosen" display indicating its ready to upload another file. Try removing the eventHandler to preserve the uploaded filename:
<xp:fileUpload id="songAlbumUpload1" value="#{submitfile.File1}"></xp:fileUpload>
Or add a fileDownload and bind it to the same RTF to display the status of the uploaded files.
<xp:fileDownload rows="30" id="fileDownload1" value="#{submitfile.File1}" displayLastModified="true"
allowDelete="true" hideWhen="false" style="width:100.0%">
</xp:fileDownload>

Xpages button validation

I am using the multiple uploader from openntf. I want to add an error message if the user has selected the files but not uploaded them.
I am thinking about some kind of "must click" button, and if the user is not uploading the files, it should give a message while saving the document saying: "Attachments are not uploaded. Do you want to continue?"
How can I check if the span tag : ynFileUploadInfo contains something or not? And depending on that, trigger the validation.
Add following client side JavaScript (CSJS) code to your save button:
var files = document.getElementById('ynFileUploadMulti').files;
if (files && files.length > 0) {
if ( ! confirm('Attachments are not uploaded. Do you want to continue?')) {
return false;
}
}
return true;
It will ask the user if listed (additional) attachments really don't have to be uploaded.
If user clicks "OK" then document will be saved without the listed attachments.
If user clicks "Cancel" then document won't be saved and user can click "Upload now" to upload the listed attachments.
Remark: if CSJS code returns false in an event then submit and server side JavaScript (SCJS) code won't get executed.

Xpages File Upload Control does nothing

Background: One xpage bound to document1. On this xpages is a tabbed table contain several tabs. Each tab contains an panel bound to other data sources incl document2. Document2 is created and made a child of document1. In each panel is a table to allow the user to create the new document, as well as two view controls. Everything works beautifully...until...
Problem: I was asked to add a way to upload an attachment to the child document. I first created an rich text field on the bound document called "ScannedInvoice". I then create a File Upload Control from the core controls and bound it to document2/ScannedInvoice. By rule, I only want a single attachment, otherwise I would consider the openNTF upload control.
I cannot figure out why nothing appears on the back end when I upload a file and create a new document. Everything else works, but there is no attachment in the ScannedInvoice field and also no $FILE fields attached to the document either.
Thanks in advance for any tips. I have searched quite a bit and have not found any answers. - Steve
<xp:fileUpload id="fileUpload1"
value="#{document2.ScannedInvoice}" useUploadname="false"
style="font-size:8pt" filename="scannedinvoice.jpg"
mimetype="image/jpeg">
</xp:fileUpload>
Note: The field ScannedInvoice on the Invoice form is rich text.
Code to save button: document2.save()
Document2 has scope=request and ignoreRequestParam=true. Both of these have to be set like this in order to work. parentID=# document1.getNoteID()
a file upload needs a full refresh of the page to be able to upload the file. Make sure that you are doing that.
======================================
Update Domino 9.0.1 supports partial refresh uploads

Using standard validation for read only edit boxes?

Some of my recent posts have to do with the fact that I am doing all of my validation in my Submit button.
The reason I am doing this is that I have a button that sets some read only edit boxes. Well you can configure a validation for a read only edit box but it does not execute at run time.
Since I could not figure this out and wanted to have a consistent look for validation I used my own validation on my Submit button.
Is there a way to validate read only fields?
One nice thing about putting all of the code in the Submit button is that all of the validation code is all in the same place but I can see where it also can cause portability issues when using custom controls.
Also another question is how to fire off validation if my Submit button is not marked as a Submit button.
As Dec says, the ReadOnly flag causes the content of the field to be rendered without the <input> tag. This makes validation impossible on the client side and since there is no data being submitted back to the JVM, validation coded on the field is ignored on the submit.
However, the data source QuerySaveDocument is triggered. Put your validation in there and/or put it in the fields that are rendered (readOnly=false) and be sure to set disableClientSideValidation="true" on all fields with validators on them.
Your QuerySaveDocument code looks something like this (assuming location is the field which is readOnly).
if (personDoc.getItemValueString("Location") == "") {
#ErrorMessage("The inherited location is blank and that is bad.");
return false;
}
return true;
With this, the field based validators will fire first and if they are all successful the QuerySaveDocument fires. This means if any field based validators fail, their messages will appear in your message area but the QuerySaveDocument message will not appear. QuerySaveDocument messages ONLY appear after all field based validators succeed.
When a read only field is rendered to the web browser it does not render using <input> tags but rather a simple <span> tag.
Validation can only be performed on proper input tags so the scenario you are experiencing is correct. There is no field for it to validate in read-only mode.
There is an option to 'display disabled in read only' which will render an <input disabled="true"> type tag for the field but I'm not sure off the top of my head is validation will work for those fields either because if a field is read-only then there really should be no need for any validation because your programmatically putting the value into the field and you should be validating it programmatically before you add the value.

Resources