Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding an interface, IOverrideInputModelForWhenValidationFails, that can... #57

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@
<ItemGroup>
<Compile Include="Ajax\IntegratedAjaxEndpoint.cs" />
<Compile Include="Ajax\IntegratedAjaxTester.cs" />
<Compile Include="LoFi_diff_models\IntegratedLoFi_diff_models_Endpoint.cs" />
<Compile Include="LoFi_diff_models\IntegratedLoFi_diff_models_Tester.cs" />
<Compile Include="LoFi\IntegratedLoFiEndpoint.cs" />
<Compile Include="LoFi\IntegratedLoFiTester.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -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<LoFiInput_post>
{
[Required]
public string Name { get; set; }

public FubuContinuation GetFubuContinuationForWhenValidationFails(LoFiInput_post inputModel)
{
return FubuContinuation.TransferTo(new LoFiInput_get(){Name = inputModel.Name});
}
}
}
Original file line number Diff line number Diff line change
@@ -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<IntegratedLoFi_diff_models_Endpoint>();
registry.Import<FubuMvcValidation>();
}

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);
}
}
}
16 changes: 13 additions & 3 deletions src/FubuMVC.Validation/ValidationActionFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,16 @@ public static ActionFilter ValidationFor<T>(Expression<Func<T, object>> method)
}
}

public interface IOverrideInputModelForWhenValidationFails<T>
{
FubuContinuation GetFubuContinuationForWhenValidationFails(T inputModel);
}

public class ValidationActionFilter<T>
{
private readonly IValidationFilter<T> _filter;
private readonly IFubuRequest _request;

public ValidationActionFilter(IValidationFilter<T> filter, IFubuRequest request)
{
_filter = filter;
Expand All @@ -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<T>;
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;
}
}
}