I'm using struts2 and validation using annotations in the action. I do use another action to fill the select lists when i fill the form with missing data it returns to the input result of the action but with the missing list and shows me an error.
See the FAQ entry about repopulating form controls when validation fails.
The nutshell version is that if you have lists they need to be repopulated using either the Preparable interface or with <s:action> tags. Personally, I think Preparable is the way to go.
Related
I'm sure this has come up many, many times before but I am at my wit's end with this problem. I'll try and be as descriptive as possible without bloating the problem statement.
I am using the jQuery validate plugin for form validation. All form rules and messages are retrieved via ajax and then initialised. All form validation works like a charm until I start cloning fieldsets inside the form to give the user the ability to add additional entries.
The input fields react exactly as they should to changing values (error highlights and displaying messages etc), but when I call the .valid() method on the form it ignores the additional fieldset cloned from the first.
I have added test rules after cloning the fieldset, and again, the input field responds correctly to the newly assigned rule but gets ignored when calling .valid() on the form element.
I will add code snippets should anyone be willing to help me find the problem.
Thanks in advance!
Fred
Edit:
I have found that the .valid() method only uses the first fieldset in the form. Take note that all input names of cloned fieldsets are the same as the original. For example, say i have a field called productName in my original fieldset and i clone it, I will now have two inputs in the form with the same name attribute.
Could it be that jQuery validate only cares about the first instance it reaches and validates on that? I have seen posts about jQuery validate not playing nice with duplicate input names on forms but i believe it was resolved.
I have a dropdown list in yii that depending on the id renders some additional fields (that requests other models).
How can I use the $form field in those (it's Yii-Bootstrap) so that I can have the validation messages (via $form->errorSummary() ) and not implementing my own javascript messages?
I'm not entirely sure what your asking for, but it seems like it's related to rendering form fields/errors and validation. As far as specifing custom selector fields goes, look into CHthml::activeDropDownList, it'll let you define your drop list items accordingly. As for validation, you can validate that drop down by making it have it's own validation rule and error as described here:Custom Model Validators. This will allow you to use CHtml::errorSummary($model) as the dropdown will return an appropriate error message if it's validation fails...
Cheers,
Fy
Not sure if this helps, but renderPartial() has serious issues with AJAX:
http://www.yiiframework.com/forum/index.php?/topic/10427-ajax-clientscript
I've been learning ExtJS4 after having done quite a bit of dev in ExtJS3. I'm quite intrigued by the new class Ext.data.Models, but I would love to integrate these validations with the validation function in Ext.grid.Panel.
Can anyone point me in the direction of any examples of using the validations property of Ext.data.Model in a Grid panel?
I've tried adding the validations to the model and putting invalid values in the grid, but it doesn't seem to throw an errors or the normal red lines.
Any ideas?
Model validation against grid data is not supported out of the box currently.
Here is a working extension for model validation against form fields though.
And here is an incomplete attempt for model validation against a grid (what you were going for).
#Drew
The grid provides RowEditing and CellEditing plugins for row/cell editing. In the background these plugins use Form panel for the validation of the input. So, you can use the form panel extension that #Geronimo has mentioned along with the extensions of RowEditing and CellEditing classes and use them in your grid to validate the data entered in the grid against the model associated with the row. And since, the validate() method is on a model, which can be used to validate a complete row data or a particular cell data. In case you are looking for bulk validation, you can override the sync() method of the Ext.data.Store class to achieve that.
I'm trying to build a fairly complex form which as a couple of cascading selects - i.e. the user selects a value in one combo and the other combo is populated according to their first selection.
I've followed a tutorial on how to handle the cascading but the problem I have is that I now have nested forms (the code in the tutorial uses forms inside partial views to POST to a controller action to load the 2nd combo). I have my main form on which I want to collect the input values but also the nexted forms for the cascading select boxes. The problem I have is that the cascading selection doesn't post to the correct controller action, but instead posts to my main (outer) form's action.
I understand this is the correct behaviour for a browser (as nested forms apparently aren't supported) but what's the correct way to implement this?
The correct way is to only have one form. Then use AJAX to populate the cascading drop down list. The are 100s of examples online how to do this with JSON
use this to have multiple submit buttons on one form which each have different controller actions to post to:
http://iwayneo.blogspot.co.uk/2013/10/aspnet-mvc-action-selector-with-list.html
as for cascading stuff - i would focus on populating these without Ajax 1st - then you can worry about adding this sort of flare - if it doesn't work without JS anyway you're in a bad place.
I would have the 1st dropdown populated when you initially load the form and have a "next" button to populate the next dropdown in the cascade. this submit can use the method above to post to an action which then populates the second data set based on the selection of the 1st dropdown.
make sense?
Then how you ajax that after the point is up to you but you'll have a very solid foundation to build up stuff like that as you will have it working in the minimal tech scenario.
w://
Each form in the application has a set of radiobuttons. Once selected, only certain fields associated with that radiobutton will need to be validated.
I am using MVC 3 and need the validation to work client side.
Simply using DataAnnotations I can only validate all fields on the form.
IValidatableObject doesn't work clientside.
IClientValidatable looks like it might do the job, but it seems I would have to write a new attribute for every standard DataAnnotation attribute.
RemoteValidation works with one field at a time.
Another option would be to drop MVC3 validation and do it all using jQuery. I don't have a problem with this as such but would like to use MVC3 and reduce coding/maintenance in preparation for a much larger project.
Could I still use MVC3 validation but then use jQuery to add/remove validation fields from validation whenever a radiobutton is selected?
If anyone can help with some suggestions as to the best way to approach this, it would be much appreciated.
MVC 3 uses jQuery's validation plug-in by default and that plug-in will not validate disabled fields. Are the fields that you don't want to validate no longer needed if certain radio buttons are selected? If so, then you can just disable those elements and they won't be validated (and note that those disabled fields won't be posted to the server either).
e.g.
$('input').attr('disabled', 'disabled');
For complex validation it is best to hand code these.
Data Annotations work great for 90% of your validation needs, but fail dismally with What/If scenarios.
For the client side use an event driven custom validation presented via jQuery Validation Plugin. For the server, use the CustomValidation attribute:
http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.customvalidationattribute(v=vs.95).aspx
Using IClientValidatable is great if you have reusable custom validation, however it is wasted time for one off validations.
Alternatively use RemotValidation with a CustomValidation attribute that invalidates multiple fields.