开发者

ASP.NET MVC Client-side validation with MvcContrib FluentHtml

What's the recommended way to do client-side validation using the开发者_如何转开发 built-in MVC2 code with MvcContrib's FluentHtml builders? We're using the jQuery client-side validation code, not the default Microsoft AJAX stuff, if that matters (though I don't think it should).

It seems the client-side validation only gets registered with jQuery Validate when you place a validation message (Html.ValidationMessageFor(x => x.FirstName)) on the page. MvcContrib's FluentHtml this.ValidationMessage(x => x.FirstName) only works with ModelState on the server side, doesn't write out any HTML if there's no error, and doesn't register the given property with jQuery Validate on the client-side.

So my question: is there a way to make the current trunk build of MvContrib work with MVC2's built-in client-side validation somewhat painlessly right now? If so, how? If not, is there another client-side validation that's recommended (other than xVal, which we're currently using and has been depreciated)? Should this be patched in MvcContrib so it works properly? A last resort would be to move to using ASP.NET MVC's built-in input builders, but we already invested a lot in MvcContrib's and would rather not.

Thanks!


Im in the exact same situation...i came across this post with in interesting comment further down although I couldn't quite get it to work.

http://lunaverse.wordpress.com/2008/11/24/mvcfluenthtml-fluent-html-interface-for-ms-mvc/

If you can make any sense of it would be good to post it back up here.

Paul


I got the comment from that blog article working Paul, and modified it to use all the known MVC validation adapters instead of just the Required one (basically mimicking much of what's in the framework itself). It gets kind of hairy with how it displays the error message and working with what we already have, and I implemented a patch for MVC Contrib to work with it, but in the end I'm giving up for now until MVC3 is finialized and MVC Contrib builds against it. No point in going through all this when there's an updated release coming soon.

Here's what I ended up with (FluentViewPage<T> is where we add behaviors):

public class ClientsideValidationBehavior<T> : IBehavior<IMemberElement> where T : class
{
    private readonly FluentViewPage<T> _viewPage;

    public ClientsideValidationBehavior(FluentViewPage<T> viewPage)
    {
        _viewPage = viewPage;
    }

    public void Execute(IMemberElement element)
    {
        var attribute = element.GetAttribute<ValidationAttribute>();

        if (attribute == null)
        {
            return;
        }

        var formContext = _viewPage.ViewContext.FormContext;
        var fieldMetadata = formContext.GetValidationMetadataForField(UiNameHelper.BuildNameFrom(element.ForMember), true);

        var modelMetadata = ModelMetadata.FromStringExpression(element.ForMember.Member.Name, _viewPage.ViewData);
        var validators = ModelValidatorProviders.Providers.GetValidators(modelMetadata, _viewPage.ViewContext);

        validators.SelectMany(v => v.GetClientValidationRules()).ForEach(fieldMetadata.ValidationRules.Add);

        fieldMetadata.ValidationMessageId = element.ForMember.Member.Name + "_Label";
    }
}

Hope that helps some.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜