diff --git a/src/FubuMVC.Validation.IntegrationTesting/FubuMVC.Validation.IntegrationTesting.csproj b/src/FubuMVC.Validation.IntegrationTesting/FubuMVC.Validation.IntegrationTesting.csproj index 415c7de..fc1d268 100644 --- a/src/FubuMVC.Validation.IntegrationTesting/FubuMVC.Validation.IntegrationTesting.csproj +++ b/src/FubuMVC.Validation.IntegrationTesting/FubuMVC.Validation.IntegrationTesting.csproj @@ -136,6 +136,8 @@ + + diff --git a/src/FubuMVC.Validation.IntegrationTesting/LoFi_diff_models/IntegratedLoFi_diff_models_Endpoint.cs b/src/FubuMVC.Validation.IntegrationTesting/LoFi_diff_models/IntegratedLoFi_diff_models_Endpoint.cs new file mode 100644 index 0000000..1b8a6e7 --- /dev/null +++ b/src/FubuMVC.Validation.IntegrationTesting/LoFi_diff_models/IntegratedLoFi_diff_models_Endpoint.cs @@ -0,0 +1,38 @@ +using FubuMVC.Core.Continuations; +using FubuValidation; + +namespace FubuMVC.Validation.IntegrationTesting.LoFi_diff_models +{ + public class IntegratedLoFi_diff_models_Endpoint + { + public const string GET = "Input data"; + public const string SUCCESS = "Success"; + + public string get_lofi(LoFiInput_get input) + { + return GET; + } + + public string post_lofi(LoFiInput_post input) + { + return SUCCESS; + } + } + + public class LoFiInput_get + { + public string Name { get; set; } + } + + + public class LoFiInput_post : IOverrideInputModelForWhenValidationFails + { + [Required] + public string Name { get; set; } + + public FubuContinuation GetFubuContinuationForWhenValidationFails(LoFiInput_post inputModel) + { + return FubuContinuation.TransferTo(new LoFiInput_get(){Name = inputModel.Name}); + } + } +} \ No newline at end of file diff --git a/src/FubuMVC.Validation.IntegrationTesting/LoFi_diff_models/IntegratedLoFi_diff_models_Tester.cs b/src/FubuMVC.Validation.IntegrationTesting/LoFi_diff_models/IntegratedLoFi_diff_models_Tester.cs new file mode 100644 index 0000000..eca30c8 --- /dev/null +++ b/src/FubuMVC.Validation.IntegrationTesting/LoFi_diff_models/IntegratedLoFi_diff_models_Tester.cs @@ -0,0 +1,44 @@ +using FubuMVC.Core.Endpoints; +using FubuMVC.Validation.IntegrationTesting.LoFi_diff_models; +using FubuTestingSupport; +using NUnit.Framework; + +namespace FubuMVC.Validation.IntegrationTesting.LoFi +{ + [TestFixture] + public class IntegratedLoFi_diff_models_Tester : ValidationHarness + { + private LoFiInput_post postInputModel; + + [SetUp] + public void SetUp() + { + postInputModel = new LoFiInput_post(); + } + + protected override void configure(Core.FubuRegistry registry) + { + registry.Actions.IncludeType(); + registry.Import(); + } + + private HttpResponse theResponse + { + get { return endpoints.PostAsForm(postInputModel); } + } + + [Test] + public void output_from_endpoint_if_validation_succeeds() + { + postInputModel.Name = "Josh"; + theResponse.ReadAsText().ShouldEqual(IntegratedLoFi_diff_models_Endpoint.SUCCESS); + } + + [Test] + public void redirects_to_get_if_validation_fails() + { + postInputModel.Name = null; + theResponse.ReadAsText().ShouldEqual(IntegratedLoFi_diff_models_Endpoint.GET); + } + } +} \ No newline at end of file diff --git a/src/FubuMVC.Validation/ValidationActionFilter.cs b/src/FubuMVC.Validation/ValidationActionFilter.cs index 009c3ff..fa70d00 100644 --- a/src/FubuMVC.Validation/ValidationActionFilter.cs +++ b/src/FubuMVC.Validation/ValidationActionFilter.cs @@ -23,11 +23,16 @@ public static ActionFilter ValidationFor(Expression> method) } } + public interface IOverrideInputModelForWhenValidationFails + { + FubuContinuation GetFubuContinuationForWhenValidationFails(T inputModel); + } + public class ValidationActionFilter { private readonly IValidationFilter _filter; private readonly IFubuRequest _request; - + public ValidationActionFilter(IValidationFilter filter, IFubuRequest request) { _filter = filter; @@ -41,9 +46,14 @@ public FubuContinuation Validate(T input) { return FubuContinuation.NextBehavior(); } - _request.Set(notification); - return FubuContinuation.TransferTo(input, categoryOrHttpMethod: "GET"); + + var inputAsOverride = input as IOverrideInputModelForWhenValidationFails; + if (inputAsOverride == null) return FubuContinuation.TransferTo(input, categoryOrHttpMethod: "GET"); + + var continuation = inputAsOverride.GetFubuContinuationForWhenValidationFails(input); + if (continuation.Type != ContinuationType.Transfer) throw new Exception("The overriden FubuContinuation must be a TransferTo to give validation details."); + return continuation; } } } \ No newline at end of file