Validate field and throw exception in JSF, but attach error message to another field? - validation

I have some fields on my page which I want cross-validated. But I don't want error from this validation to be displayed in <h:message> for this fields.
If I add validator to any of the fields, and validator throws exception, error is displayed in <h:message> for this field. On the other hand I HAVE TO throw exception if I want to suppress page from submitting. Just displaying some error message is not enough.
So I created some hidden field on the form, and attached validator there. This validator has access to UIComponents of the fields I want to validate, so it can validate them. When validator throws exception, error shows in <h:message> for hidden field, which I can place anywhere I want.
Everything works, if I put hidden field after the fields I want to validate. (If I put it before, hidden field validation is triggered before even UIComponents of my fields are updated).
The problem is it's nasty hack :) Is there some better way to do it?

The problem is it's nasty hack :) Is there some better way to do it?
Not for the particular functional requirement. It's very true that JSF allows very little fine grained control for cross-validation of multiple fields.
Everything works, if I put hidden field after the fields I want to validate. (If I put it before, hidden field validation is triggered before even UIComponents of my fields are updated).
Components are during validations phase processed in the order as they appear in the component tree. If you have at some point an UIInput at hands which is still to be processed yet, then you need to grab the submitted value by UIInput#getSubmittedValue(). If it is already been processed, then you need to grab the submitted (and converted and validated) value by UIInput#getValue() instead.
So, if you put the hidden field with the validator after the to-be-validated components, then you need UIInput#getValue() to grab the values. If the hidden field is put before the to-be-validated components, then you need UIInput#getSubmittedValue() to grab the values.

i have got this code working for me:
throw new ValidatorException(new ArrayList());
no errors were displayed.

Related

Sitecore Item Validators

I created an item validator with the validation rule template. I'm using it to check if one field for a date is after another field for a date following this tutorial: https://sitecorejohn.wordpress.com/2010/03/17/validate-that-the-value-of-one-datetime-field-follows-another-with-sitecore. For some reason the item that is pulled with GetItem() in my validator does not have the change that the content editor has made until the item is saved. I thought Sitecore.Data.Validators.BaseValidator.UpdateItem would take care of this but it seems that my control to validate is null. That makes sense since it is an item validator instead of a field validator but that means that if this fires off on blur content editors may see false error messages or not see error messages when they should. I'm also running into an issue where my Evaluate method is firing twice on save; once before the item is actually saved so GetItem() returns with the non-updated values and once after save which has the expected values. If anyone has any insight as to why this might be happening I'd like to know. I have a feeling that the validator executing twice on save might be a config issue but I didn't see anything very obvious in the pipeline.
To get the new value of the field that's being validated you can use BaseValidator's
GetControlValidationValue();

I would like to define a component as optionally required (based on another field's value)

I built two custom components using built-in CQ components. The components' JSP's, along with some custom javascript, have a line like below in order to leverage the built-in components (and their formatting, labels, etc.).
<cq:include path="." resourceType="foundation/components/form/dropdown" />
I have one each of my custom dropdown components on a form. I can mark field A and field B as both being required in their respective components' editor dialog. I can provide required messages so that when either field is blank and the form is submitted, I get a message that the fields are required (with my custom messages). However, what I really want to do is hide or disable field B based on the value supplied in field A. I'm handling this manually via jQuery. However, this of course presents a problem on validation. I want field B to be NOT required when it is hidden/disabled and for it to be required when it is enabled/visible. Since the showing/hiding is done client-side, the server-side validation has know knowledge of the change and still expect a value to be provided for field B.
I'm been trying to poke around in the CQ Widgets API to find if there is something I can do on the client side to set/unset the required property so that when the form gets posted it is handled correctly. I'm guessing that there isn't since the validation seems to be happening on post instead of client-side.
Any ideas/thoughts/options?
It turns out that the link from my comment gave me the idea that I can have custom server-side validation for my form, I just wasn't sure how to connect it to the field. It turns out that all I had to do was create a servervalidation.jsp file in my "B" component. CQ will see the servervalidation.jsp and automatically invoke it for my custom component.
I was then able to examine the submitted value of field "A" (by getting the parameter from the request object) and doing some other custom logic for my needs.
I was then able to optionally do this:
FieldDescription field = FieldHelper.getConstraintFieldDescription(slingRequest);
FieldHelper.checkRequired(slingRequest, field);

difference between partialsubmit and singlesubmit in ICEfaces

I know the partial submit is used in icefaces 1.x, singlesubmit in icefaces 2.x and the tag in icefaces 3.x.
May someone tell me what is the substantial difference between them?
thanks.
Both partialSubmit and singleSubmit does the same thing. But in different ways.
Here is a typical form scenario:
user sees a form and starts interacting. Those forms have some fields. Some of those fields are required and are necessary to process the form. Other fields are optional.
When using partialSubmit, when a user leaves a field(onblur), iceface internally makes all other fields as non required so that the overall form can be submitted. Now since all other fields are optional and only the present field that you onblurred was required, icefaces can process the form. So it does all the validation checks and changes other elements that might have been affected and renders the whole page again with new changes. But here is the thing. The other fields that were deliberately made optional by icefaces, they also have their own validation mechnanisms. So when the form is processed, those field will show errors that they are not filled, or the password field cannot be null and all that. But since the user has not engaged other fields of the form, these errors should not be triggerred. This was the drawback of partialSubmit.
In singleSubmtit, they corrected this. In here, your field is taken and is sepately validated without affecting other fields or triggering their errors.
Hope you have understood this. If not, this link will help
www.icesoft.org/wiki/display/ice/single+submit
http://www.icesoft.org/wiki/display/ICE/Using+Single+Submit
The SingleSubmit tag is a replacement by PartialSubmit, according to ICESoft information.
Cheers!

How to force Wicket "onchange" AJAX events to be triggered if fields fail validation conditions

The specific case I've got in mind is as follows: an AjaxFormComponentUpdatingBehavior("onchange") is added to a TextField in a form. The behavior verifies the text for certain conditions (either the model object or the form component model, doesn't matter), based on which it might display a message (or hide it, if it has already been shown).
The problem is, there are also validators added to the TextField. One of the possible (and likely) scenarios consists of the user typing in, first, a value that causes the message to be displayed by the AJAX request. If, then, he/she types in a value that doesn't pass validation, the message should disappear, but it does not.
Apparently, either the onUpdate() method for the AJAX behavior is not called at all, or I am failing in my attempts to insert a check for non-validated entries (I have tried to test for both null values and empty strings, to no avail; I have no idea what exactly Wicket's validators do to models when data is invalid).
I am wondering if someone who actually understands validators (or AJAX, actually) has any ideas on where the problem could be.
I can post edit and post code if someone tells me this is not a general issue tying validators and AJAX, but most likely a programming mistake. I still believe the former and thus I'll refrain from posting code sections, in order to keep the discussion on an API/theoretical frame.
Thanks.
When using an AjaxFormComponentUpdatingBehavior, if any of the IValidators fail their validation, onError() will be called instead of onUpdate(). Wicket will effectively prevent invalid user input from reaching the IModels in your components, so the component's ModelObject will not be changed at all. The invalid input will probably remain available by means of getInput()/getConvertedInput() (not sure if it will in an AJAX scenario, it sure is in a traditional form submission).
However, take into account that IFormValidators are not executed when using this mechanism. If you've got any, you might be interested in overriding getUpdateModel() so that AjaxFormComponentUpdatingBehavior will not bring maybe-invalid user input into your IModels, and set modelobjects manually when you're certain user input is valid.
Regarding your specific case, you could perform all the required logic in onError() (or rely on Models that will grab data from somewhere else), and just add the components that need refreshing to the AjaxRequestTarget. This is probably what's missing in your scenario.

Struts2 parameter refilling on validation error

Suppose I have struts2 select tag in a form, when the form is submitted, according to the validation xml there's an error so input is returned.
Now my question is when input is returned the select box becomes empty. How to prevent it from becoming empty since because in validation error the execution never reaches the action's method (where we can possibly fill the select again).
One possible solution could be to validate the form within the action's method but I want to check the validation through XML only.

Resources