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.
Related
I have added errors manually to my input using the link in here Here and the example in Here but when I try $("#myshowErrors").valid() after I added the errors it becomes true?
here is my code
var validator = $( "#myshowErrors" ).validate();
validator.showErrors({
"firstname": "I know that your firstname is Pete, Pete!"
});
I am not able to make the client validation fail. How can I make form.valid() to return false?
I don't want to make form.valid()=false; manually I want that to be taken care of by just setting the errors.
It's entirely unclear by the lack of code in your question, but I think you might misunderstand where to attach the .valid() method.
This method only gets attached to a selector representing a single <form> element...
$('#myform').valid(); // triggers validation on the entire form
Or to any selector that represents a single form input element...
$('#myform input[name="myinput"]').valid(); // triggers validation on one input element
When .valid() is called, validation is triggered on the selected element, error message(s) is/are displayed, and the method will return true or false.
The .valid() method can never be attached to a selector that represents more than one element, or a collection of elements without also using a jQuery .each().
EDITS:
showErrors is just for showing existing errors. You cannot "invalidate" a field by calling showErrors. Fields are either valid or invalid based solely on what is contained within that field.
You've tagged the question with Unobtrusive Validation. Since jQuery Validation is handled automatically via the Unobtrusive Validation plugin, you are not allowed to call your own instance of the .validate() method. That's because the Unobtrusive plugin automatically constructs and calls .validate(). Once it's called, the plugin is initialized, it cannot be called again, and all subsequent calls are always ignored.
I have a form with method="get" set on the form. I have a control (DropDownList) with the SelectMethod attribute defined to populate the list's contents. On first load the SelectMethod is called and works (and I can verify this with a breakpoint). However, if I submit the form I can see the browser update and the query string get populated but SelectMethod is not called/I don't hit the breakpoint.
I'm more or less following the example from Microsoft here http://www.asp.net/web-forms/overview/presenting-and-managing-data/model-binding/using-query-string-values-to-retrieve-data
only I'm using a form submission with a GET instead of a link with a mutated query string.
Lets say I have couple of filters in my deployment descriptor and each request to the app has to pass through those. There is form with a formbean attached to it. Once I fill the form and click submit, when does the html parameters in the form are binded to the form bean? Before the request is passed through the filters or after passing thorugh the filters?
The reason I am asking is.. I have a form with 20 fields. Once I click submit, 17 values are being binded to the bean properly but 3 values are missing by the team request reaches validate method. I am absolutely clueless has to where the values are missing. I tried to print an enumeration of all the request parameters in validate method and those three fields are there in request object. AFAIK, There is no issue with those three elements name because the app works perfecly in my local IDE but brakes up in test env which makes it all the more difficult to pin-point the issue :(.
I would like to validate both single field and multiple field data from a CakePHP form.
The single field validation should be done on blur from each field while the multiple field validation should be done on submitting the form.
I would like to use the $validate property declared in the Model for validating data and I would like to display the errors near each field (single field validation) and on top of the form (for multiple field validation).
My main goal is to achieve this the most "caky" way (if there is one for validating data with jQuery). I couldn't find any useful advice out there and I'm asking you for some help to get this going.
One of my concerns is how shall I pass data from the form to jQuery and then to the action that does the validation and also how shall I return and display the errors, if there are any.
Thank you in advance!
I'd suggest first making sure everything works without jQuery, then use the jQuery Form plugin to submit your forms via AJAX. If you include the RequestHandler component in your AppController, you should find that your controllers distinguish automatically between AJAX and synchronous requests.
OK, so I coded my own solution to this, but I am still waiting for a more "caky" approach.
I made two generic jQuery functions, one for single field validation and one for multiple field validation. The function should grab the data from the specified form and send it to the form's action via AJAX, to a specially created controller method which will attempt to validate data and output an AJAX response ("" for validation has passed and errors for errors in validation). Then, the result is checked in the jQuery function and the default form behaviour is triggered only if the validation has passed. Otherwise, display the errors and return false; to prevent default submission.
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.