Reputation: 1343
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.
Upvotes: 1
Views: 852
Reputation: 1632
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.
Upvotes: 2
Reputation: 3345
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.
Upvotes: 1