English 中文(简体)
fubumvc——使用FailureValidationPolicy进行简单验证
原标题:fubumvc - simple forms validation using IFailureValidationPolicy

I ve been trying to implement form validation correctly and a discussion on fubu mailing list has been the most helpful (http://groups.google.com/group/fubumvc-devel/browse_thread/thread/d54b135fe0254653/12180cd86e9dc50b). I m still not entirely clear on certain points, I m a newbie so I m going through some yak shaving.

It seems like the example given in the discussion performed the validation within the controller itself using IsValid(model).

我试图避免这种情况,将我的投入模式与必要的验证特性加以校正,然后利用验证组合在失败时进行转让(通过政策)。

this.Validation(x => {
                            x.Actions
                                .Include(call => call.HasInput && call.InputType().Name.EndsWith("Input"));

                            x.Failures                                    
                                .ApplyPolicy<AccountValidationFailedPolicy>();
                        });

这里的等级是对政策施加影响:

public class AccountValidationFailedPolicy : IValidationFailurePolicy {

    public bool Matches(ValidationFailure context) {
        return (context.InputType() == typeof (RegisterAccountInput));
    }

    public void Handle(ValidationFailure context) {
        var incomingRequest = (RegisterAccountInput) context.InputModel;

        var failedValidation = new RegisterationFailedNotification {
            CVV = incomingRequest.CVV,
            AcceptTerms = incomingRequest.AcceptTerms,
            Countries = incomingRequest.Countries,
            PhoneNumber = incomingRequest.PhoneNumber,
            PIN = incomingRequest.PIN
        };

        FubuContinuation.TransferTo(failedValidation);
    }
}

处理只是试图通过新的模式向另一个行动转移,把价值复制到新的模式中,以便我能够再次以形式展示这些价值观。

I must be doing something wrong here, because it s not transferring anywhere. I have a class with this method which I was hoping would handle it.

public AccountViewModel New(RegisterationFailedNotification notification) { .... }

我在这里看一看,或者我没有看到什么基本东西? 也许政策不是在这里做什么?

最佳回答

@stantona

政策机制将在此发挥作用。 我要向你通报我计划如何使这一简单化(很快)的细节,并指出你利用“富布约”。 转让 简单地说,它就没有执行。

在这方面,你需要:

public class AccountValidationFailedPolicy : IValidationFailurePolicy {
private readonly IFubuRequest _request;
private readonly IValidationContinuationHandler _handler;

public AccountValidationFailedPolicy(IFubuRequest request, IValidationContinuationHandler handler) {
    _request = request;
    _handler = handler;
}

public bool Matches(ValidationFailure context) {
    return (context.InputType() == typeof (RegisterAccountInput));
}

public void Handle(ValidationFailure context) {
    var incomingRequest = (RegisterAccountInput) context.InputModel;

    var failedValidation = new RegisterationFailedNotification {
    CVV = incomingRequest.CVV,
    AcceptTerms = incomingRequest.AcceptTerms,
    Countries = incomingRequest.Countries,
    PhoneNumber = incomingRequest.PhoneNumber,
    PIN = incomingRequest.PIN
    };

    var continuation = FubuContinuation.TransferTo(failedValidation);
    _request.Set(continuation);

    _handler.Handle();
}
}
问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签