Why #Html.TextBoxFor and other helpers would skip jquery.validation attributes and create elements without those? Am I missing some references or something? It's MVC 3.0 project.
If I add them manually like that:
#Html.TextBoxFor(x => x.Name,
new { data_val="true", data_val_required="Need that field" })
then unobtrusive validation works. But it's suppose build elements and put those attributes based on Model's DataAnnotations. Model is there, textbox inside of a form body, and still doesn't work. What could it be?
upd: UnobtrusiveJavaScriptEnabled set to true in web.config
Please ensure these two lines in your web.config file
<appSettings>
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>
For unobtrusive validation to work you need to ensure that UnobtrusiveJavaScriptEnabled is set to true in your Web.config and that you have included these 3 scripts:
jQuery (~/Scripts/jquery-1.8.2.js)
jQuery Validate (~/Scripts/jquery.validate.js)
the MVC plugin for unobtrusive client validation with jQuery Validate (~/Scripts/jquery.validate.unobtrusive.js).
1.if the text box is loading via ajax, it's may not be parsed for Unobtrusive validation.
2.check rendered html if there is a data-val-required attribute for the text box to determine if it's a server side or client side problem.
Related
I have a dropdown with the "Select Country" default option, which I'd like to validate as invalid, if they choose "Select Country". The ajax and unobtrusive scripts are all in place. It actually works after the submit button is clicked, because of my [Required] attribute in the model class which deals with this particular page.
However, it does not work exactly how I'd like before the submit button is pressed: when I choose a valid option and tab away, it turns green as expected. But then I can select the default option again and it will remain green. In fact, some of the other text boxes also remained green when blank, but I wasn't actually able to reproduce this behavior unlike in the dropdown list.
And yet, after the form is submitted once and all the validation messages pop up, it works just as expected. Anyone else have such an experience.
I know this is easily solved with some javascript, but if there's something I'm fundamentally missing, I'd like to know.
This image is captured after:
input text into the City field
then tabbing out
then deleting all text from City field
selecting a valid item from Country drop down
tabbing out of Country drop down
selecting the default option in the Country drop down
However, after the submit element is clicked and unobtrusive validation reveals all the validation messages for all elements, the drop down take up expected behavior. Now when the default option is selected, the element is red. Why?
The POBox is not a required field, so green is Ok on that one.
Hi If you have problem with client side validation, make sure that the following lines of codes available/correct in your project.
<script src="#Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="#Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
OR if you are using bundling then make sure
#Scripts.Render("~/bundles/jqueryval")
And finally in the Webconfig file:
<appSettings>
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>
Hope the above will be helpful like checklist.
<appSettings>
<add key="UnobtrusiveJavaScriptEnabled" value="true"/>
</appSettings>
Currently, we have UnobtrusiveJavaScriptEnabled set to true in the appSettings
(This can be also be defined in global.asax)
We are looking at cleaning up the appSettings and removing configuration that we do not need to explicitly define (Especially framework stuff), but not sure what it defaults to.
The default value is true, so unobtrusive javascript is enabled by default. You can remove the setting if you wish. And by the way it is the same in ASP.NET MVC 3.
When using MVC3 unobtrusive validation, the user initially fills in the form without any validation occurring. After the form is submitted and validation errors occur, validation then occurs in real-time as the user types and moves between fields. Is it possible to have this real-time validation behaviour switched on before the form is submitted?
You could call $form.valid() to validate the whole form on page load but then they get a validation summary at the top (assuming you've enabled it) along with validation messages next to each input.
How about validating each field as it is changed? You could attach to the change event like so: $fields.change(function (event) { $(event.target).valid() }); which would run valid() on the field after it is changed. You can of course substitute another event for change to match more closely the function you need.
I don't know of an official way to mimic the post-submit-attempt validation exactly...
Make sure both ClientValidationEnabled and UnobtrusiveJavaScriptEnable are set to true in your web.config.
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
Try this:
var isValid = $('#formId').validate().form();
I'm new to Struts 2 and facing this problem in keeping the layout of my page:
<s:form action="abc.action"><br>
<s:textfield key="name" label="Name" /><%--here I need to display errormessage for `name`--%>
<br>
<s:textfield key="email" label="Email" /><%--here I need to display errormessage for `email`--%>
<br>
<s:submit>
</s:form>
I'm using xml-validator for my action class, this works fine. but the validation-error messages appear over the fieldname and text-box. but i want it to come afterwards respective text-box (inside another html-container). Kindly advise.
If you're used to writing HTML, switch to the simple theme.
In struts.xml is probably the best place:
<struts>
<constant name="struts.ui.theme" value="simple" />
</struts>
Then just use the fielderror tag to put the error for the field where you want it.
It's a good to be familiar with the Struts2 tags: http://struts.apache.org/release/2.3.x/docs/tag-reference.html
That is the default according to Struts2 default templating. To change it, see http://www.mkyong.com/struts2/working-with-struts-2-theme-template/
You Can also Use Struts2 validation Framework
Validation framework comes with set of useful routines to handle form validation automatically and it can handle both server side as well as client side form validation. If certain validation is not present, you can create your own validation logic by implementing java interface.
com.opensymphony.xwork2.Validator
Validator uses XML configuration files to determine which validation routines should be installed and how they should be applied for a given application. validators.xml file contains all common validators declaration. If validators.xml file is not present in classpath, a default validation file is loaded from path
com/opensymphony/xwork2/validator/validators/default.xml
Validators Scope
There are two types of Validators in Struts2 Validation Framework.
Field Validators
Non-field validators
Field validators, as the name indicate, act on single fields accessible through an action. A validator, in contrast, is more generic and can do validations in the full action context, involving more than one field (or even no field at all) in validation rule. Most validations can be defined on per field basis. This should be preferred over non-field validation wherever possible, as field validator messages are bound to the related field and will be presented next to the corresponding input element in the respecting view.
<validators>
<field name="bar">
<field-validator type="required">
<message>You must enter a value for bar.</message>
</field-validator>
</field>
</validators>
Non-field validators only add action level messages. Non-field validators are mostly domain specific and therefore offer custom implementations. The most important standard non-field validator provided by XWork is ExpressionValidator.
<validators>
<validator type="expression">
<param name="expression">foo lt bar</param>
<message>Foo must be greater than Bar.</message>
</validator>
</validators>
For whole detail example visit this link link to struts2 validation
My project changed version of struts from struts-2.1.8.1 to struts-2.2.1.
We don't use suffix ".action" for naming, after migration it is appeared. For older version html code looks like:
<form id="Login" name="Login" action="/fm2/Login" method="post">
But new struts renders the same form:
<form id="Login" name="Login" action="/fm2/Login.action" method="post"
So difference that .action has been added. What's wrong with new release?
This is the default extension (and should be in 2.1.8.1 too).
You can change it in your struts.xml:
<constant name="struts.action.extension" value="whatever" />
I have a similiar problem when change to struts-2.2.1 from struts-2.1.8.1.
Struts-2.2.1 will add the ".action" extension automatically for redirectAction result.
This is very annoying.
This has not changed, AFAIK. Be sure to understand the difference between the "struts action" and the "action attribute of a HTML FORM" element
Typically, to render a FORM tag in Struts2 you'd use a (Struts2) form tag - its action attribute corresponds to the name of a Struts2 action, which corresponds to a url without the suffix (by default '.action', but you can change it)
So, the Struts2 tag
<s:form action="/fm2/Login">
would typically produce the HTML output
<form action="/fm2/Login.action">
I removed the struts2-convention-plugin-2.1.8.1 jar from my web-inf/lib and it started to work fine.
Hope this helps...
cheers...