File upload validation in xPages - validation

According to this article
http://www-10.lotus.com/ldd/ddwiki.nsf/revisions/6A9EDD911827AA13852574EA00388F8F?OpenDocument
simple validation should work for File Upload controls. I am trying to use it in a extLib Form table.
I would like to verify that the user have selected a file, but have not been able to get this to work on serverside validation. Have also tried to use a custom validator, but still with no luck. Other required fields are marked fine, but not the upload control.
Do anyone know how validate that the user have actually selected a file?

The validation works for client side validation only. There are some workarounds:
The easiest way to validate if a file was attached is to add a validation field to your form and set the property computeWithForm="onsave" of your datasource. As soon as you want to save the document a validation error is thrown and the saving is interrupted. The validation field is a simple editable field with a validation formula like this:
#If(#Attachments = 0;#Failure("No File attached!");#Success)
Check your datasource in the querySave event:
if( document1.getAttachmentList("Body").isEmpty() ){
var msg = new javax.faces.application.FacesMessage("No File added!");
facesContext.addMessage( "No File!", msg );
return false;
}
These two workarounds are only working if the document is newly created. As soon a file is attached, these two options are not working anymore.
If you want to check already existing documents, you can use this XSnippet here:
http://openntf.org/XSnippets.nsf/snippet.xsp?id=replace-attachment-when-uploading-a-new-attachment
You then have to modify the XSnippet to fit your requirements and add a message (as shown in the second example).
Hope this helps
Sven

I'm aware that this has been asked and answered several months ago, but I was looking for an answer to the same problem today when I found this.
Although Sven's answers didn't help directly, option #2 gave the final hint to my solution. Maybe it can be of use for others, too:
First of all, my page uses a standard button (not a button of type "Submit" as I need to set some hidden fields along with the editable ones). So, before the final saving is done I added this script to my button code:
var numAtts = myDocDatasource.getAttachmentList("Body").size();
if(numAtts == 0){
var msg = new javax.faces.application.FacesMessage("You need to attach a file");
facesContext.addMessage("File validation error", msg);
return false;
}
//do some more stuff
...
myDocDatasource.save();
I had to realize that the content of the fileUpload control doesn't really matter when it comes to validation as at that stage of the process an uploaded file already is part of the datasource.
The "timing" of this validation step is a bit surprising, though: at least in my situation, validation of other fields is done before the file upload is validated:
in an errorMessages control first only the standard validation errors are listed. Only after all the other fields have been validated successfully my fileUpload validator is displaying its error.

Related

Is there a way to natively validate the customer form fields with Square?

I am using PHP Connect to submit a simple payment via Square. I’ve added a customer object, but do not see any validation settings for fields with the customer object via Square’s API. There must be a way to set custom required fields when dealing with a customer object. Can someone please point me the right way?
Here is the PHP script I am using to connect to Square:
https://github.com/square/connect-api-examples/blob/master/connect-examples/v2/php_payment/index.php
As you can see, there is an onclick event in the button submit:
<button id="sq-creditcard" class="sq-button" onclick="requestCardNonce(event)">
I can remove this event and validate fields via jQuery, but I am having trouble when attempting to run the event on successful validation. Here is one example I have tried:
$(“form”).submit(function(){
$(“input”).each(function(){
if($(this).val()){
good = true;
}else{
good = false;
}
});
if(good){
requestCardNonce(event);
}
});
This is just a stripped down version of what I am attempting. Essentially, just passing the requestCardNonce(event) after everything checks out. But when I do this, it breaks...

Amounts with "$" fail jQuery unobtrusive validation

I'm using jQuery unobtrusive validation version 2.0 for MVC3. I also have the latest jquery.validate (v 1.9). I have a popup form with this code:
$(document).ready(function () {
$('#createForm').submit(function () {
$.validator.unobtrusive.parse($('#createForm'));
(more)
(The third line is necessary so that the form fields added by javascript will be validated.)
The validation works fine except that a value such as $1,000.00 entered into an input tag that is bound to a decimal property in the viewmodel is invalid, while 1,000.00 is valid. Clearly the "$" is casting the value as a string in the eyes of the validator.
I have researched this for many hours and I have only found one other similar question posted (also on SO and it was unanswered). I can't believe that this has been encountered by every MVC3 developer who handles currency values in a modal dialog, otherwise we would surely have some resolution by now, right?
I have resolved the issue on the server side by creating a DecimalBinder. Now I need a solution for the client-side validation. I have been looking hard at the API for jquery.validate.unobtrusive but I can't seem to find a hook. I do not want to modify any standard javascript library.
How about a custom validation method which first looks to strip off any $ characters prior to validating?
var currentVal = (control's actual value);
currentVal = currentVal.replace('$','');
Then continue with validation. The obvious downside is the need for custom validators.

MVC3 Razor Ajax partial page validation

I have a wizard style page that I want to validate a page at a time. So before the user can move off the (partial) page I submit the inputs from the div for validation. Client side validation works OK but on some pages there are one or more fields I need to validate on the server. Yes I know about remote validation.
I can get the server side validation done without a problem. The issue I have is how do I display the errors on the correct fields? I can locate the fields in the div and find the span where the error message is suppose to go. But I just can't get the message to display.
I must be missing something when updating the field span. I would have thouht that there is a jQuery routine to add error information to a field. I need something similar to the controllers AddModuleError. So when I get return from my $.Post I can set error text on the appropriate fields.
Any suggestions?
A potential solution to your problem might be in this article "Client side validation after Ajax Partial View result in ASP.NET MVC 3"
Basically, once you get your html from the post you can invoke validation using jQuery.validator.unobtrusive.parse()
As per the example in the article;
$.post("YourAction", { data: <your form data> },
function(htmlContent){
$('#container').html(htmlContent);
jQuery.validator.unobtrusive.parse('#content')
}
Worth looking into
If you are using the included RemoteAttribute, or the version I linked to in my answer to your other remote validation question, then you should not have to worry about displaying the error, as both work with the ValidationMessage helpers to automatically display errors.
Have you added Html.ValidationMessage(...) or Html.ValidationMessageFor(...) for each field to be validated?

Manually bind JQuery validation after Ajax request

I'm requesting an ASP.net MVC view into a live box and the view contains form fields that have been marked up with attributes to be used by JQuery's unobtrusive validators plug-in.
The client script is not however working and my theory is that its because the validation framework is only being triggered on page load which has long since passed by the time the MVC view has been loaded into the live box.
Thus how can I let the validation framework know that it has new form fields to fix up?
Cheers, Ian.
var $form = $("form");
$form.unbind();
$form.data("validator", null);
$.validator.unobtrusive.parse(document);
// Re add validation with changes
$form.validate($form.data("unobtrusiveValidation").options);
You may take a look at the following blog post. And here's another one.
Another option, rather trick, which worked for me. Just add following line in the beginning of the partial view which is being returned by ajax call
this.ViewContext.FormContext = new FormContext();
Reference
For some reason I had to combine bjan and dfortun's answers...
So I put this in my view:
#{
this.ViewContext.FormContext = new FormContext();
}
And this execute this after the ajax call finishes:
var form = $("#EnrollmentForm");
form.unbind();
form.data("validator", null);
$.validator.unobtrusive.parse(document);
form.validate(form.data("unobtrusiveValidation").options);
I had a similar issue. I had a form that was using Ajax requests to re-display a part of the form with different form fields. I used unobtrusive validation by manually doing it on the client side using the
#Html.TextBoxFor
for my text boxes. For some reason the validation works when attempting to submit with invalid fields (i.e., the text boxes get outlined in red and the appropriate error messages display with the content I put in the
data_val_required
attribute, for example.
However, after I click a button that makes an Ajax request to modify the form with different fields and then submit again, only the red outline on the invalid fields display, but no error messages are rendered.
bjan's trick worked for me, but I still can't see what was causing the issue. All the HTML necessary to carry out the client-side validation was there I just can't figure out why the error message attribute values wouldn't display.
All I can think of is that the jQuery validation code doesn't make a second attempt to check the form fields after a submit was made.

MVC3 Submit Ajax Form

I've read lots of different posts on this topic and I will continue to do so and try various things. There's so much variation in the information though that, with my relative inexperience with MVC, etc, I feel like I'm going round in circles a bit.
Here's the situation. I am displaying a (Razor) view in an MVC3 app that contains records in a jqGrid. Double-clicking a record opens that record in a jQuery UI dialog. The original view contains just the that represents the dialog. The invoked action returns a partial view that becomes the content for the dialogue div.
function editItem(gridID, url) {
var rowID = getSelectedRowID(gridID);
if (rowID == null) {
alert('No row selected.\r\nPlease select a row and try again.');
return;
}
else {
var grid = getGrid(gridID);
$("#editDialogue").load(url + "/" + grid.SelectedRowID, function (html) {
$("#editDialogue")[0].value = html;
$("#editTabs").tabs();
$("#editDialogue").dialog("open");
});
}
}
That part works, although I have a feeling it could stand a little bit of cleanup. My real issue is later submitting the form contained in that dialog. It's supposed to post back to an action with the same name, which it does. That action is supposed to validate and save or return the same partial view with validation errors. At the moment I'm trying to just return the view without doing anything else. The view is returned alright but the entire page is replaced, instead of just the contents of the dialog.
I've tried various things, including using Ajax.BeginForm and specifying the id of the dialog div as the UpdateTargetId of the AjaxOptions. Nothing has worked so far. Note that the form being submitted is inside the div so it will be replaced too. Not sure if this is part of the problem.
I'm packing up for the day now so I thought I'd post and see what happens overnight before attacking the problem again in the morning.
jmcilhinney,
You were right to set the AjaxOptions.UpdateTargetId, but you also need to set AjaxOptions.InsertionMode to InsertionMode.Replace.
I had the same issue occurring as well. The fix was to make sure that the jquery.unobtrusive-ajax.is file is on the page. That fixed my issue. Just double check to be sure. I was confident I had all the js files, but found out I was missing that one.

Resources