Is there any possibility that we can give customized names for validation XMLs in Struts2.
Generally If our action class name is TestAction then validation XML name should be TestAction-actionName-validation.xml or TestAction-validation.xml.
But I want give validation in an XML like Abc.xml. Is it possible?
Related
How to get all of default (English) validation messages, so I could translate them. I know I can specify resource name in validation attribute, but I just want to know all the messages.
To localize the default error messages in ASP.NET MVC you need to set the following properties in Global.asax Application_Start method,
ClientDataTypeModelValidatorProvider.ResourceClassKey = "MyResources";
DefaultModelBinder.ResourceClassKey = "MyResources";
Next, you need to create a MyResources.resx resource file and your culture specific resource file MyResources.xx.resx inside App_GlobalResources folder where you can override the following messages:
PropertyValueInvalid
FieldMustBeDate
FieldMustBeNumeric
PropertyValueRequired
For instance, I have a field to enter telephone number and i want it to be validated. I.e. check if the value entered is numeric and check if the entered value is having 10 digits.
I achieved this by creating a validation.xml.
But my question is How can i use the same set of validation rules(the same file) for another field? Is that possible in Struts 2?
Not sure if this is what you are looking for:
Create a new file called validators.xml in WEB-INF/classes and define the new validator.
<validator name="newvalidator" class="FQCN"/>
Create a class which extends FieldValidatorSupport to implement your own validation logic.
Use the new validator in validation xml files
<field-validator type="newvalidator">
I would like to replace validation message for all model properties that have [Required] attribute from the default "The XY field is required." to "*". I want to do that site wide in one place without having to add a custom message to Required attributes or passing additional parameter to ValidationMessageFor() Html helper.
Any ideas?
See the answer to my question here :
Localizing Error Messages In ASP.NET MVC 2 (Default Validation Attributes)
I'd like to highlight field names instead of showing a separate error message when there is a validation error.
Is there any convenient way of doing this other than checking the ModelState Errors collection and wrapping each .LabelFor() in an if?
Also, I'd like to format labels as either bold, or add an asterisk if the model metadata has a [Required] attribute.
You'll need use a custom Object.ascx file by creating either /Shared/DisplayTemplates/Object.ascx or /Shared/EditorTemplates/Object.ascx
I can't answer any better than this guide at Brad Wilson's blog: http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-4-custom-object-templates.html
How do we populate\retrieve information from\into multiple forms in same action class in Struts2 ? With ModelDriven approach, action is tied to one form. With ScopedModelDriven interceptor jsp becomes dirty as one has to write model.property to access form properties. Is there a better way of doing it?