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

[5.3] Before/After validation hooks for FormRequests #16238

Merged
merged 1 commit into from
Nov 7, 2016
Merged
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
22 changes: 22 additions & 0 deletions src/Illuminate/Validation/ValidatesWhenResolvedTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ trait ValidatesWhenResolvedTrait
*/
public function validate()
{
$this->beforeValidation();
$instance = $this->getValidatorInstance();

if (! $this->passesAuthorization()) {
$this->failedAuthorization();
} elseif (! $instance->passes()) {
$this->failedValidation($instance);
}
$this->afterValidation();
}

/**
Expand Down Expand Up @@ -71,4 +73,24 @@ protected function failedAuthorization()
{
throw new UnauthorizedException;
}

/**
* Provide a hook for taking action before validation
*
* @return void
*/
protected function beforeValidation()
{
// no default action
}

/**
* Provide a hook for taking action after validation
*
* @return void
*/
protected function afterValidation()
{
// no default action
}
}
51 changes: 51 additions & 0 deletions tests/Foundation/FoundationFormRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,35 @@ public function testRedirectResponseIsProperlyCreatedWithGivenErrors()

$request->response(['errors']);
}

public function testValidateFunctionRunsBeforeValidationFunction()
{
$request = FoundationTestFormRequestHooks::create('/', 'GET', ['name' => 'abigail']);
$request->setContainer($container = new Container);
$factory = m::mock('Illuminate\Validation\Factory');
$factory->shouldReceive('make')->once()->with(['name' => 'Taylor'], ['name' => 'required'], [], [])->andReturn(
$validator = m::mock('Illuminate\Validation\Validator')
);
$container->instance('Illuminate\Contracts\Validation\Factory', $factory);
$validator->shouldReceive('passes')->once()->andReturn(true);

$request->validate($factory);
}

public function testValidateFunctionRunsAfterValidationFunctionIfValidationPasses()
{
$request = FoundationTestFormRequestStub::create('/', 'GET', ['name' => 'abigail']);
$request->setContainer($container = new Container);
$factory = m::mock('Illuminate\Validation\Factory');
$factory->shouldReceive('make')->once()->with(['name' => 'abigail'], ['name' => 'required'], [], [])->andReturn(
$validator = m::mock('Illuminate\Validation\Validator')
);
$container->instance('Illuminate\Contracts\Validation\Factory', $factory);
$validator->shouldReceive('passes')->once()->andReturn(true);

$request->validate($factory);
$this->assertSame('Jeffrey', $request->get('name'));
}
}

class FoundationTestFormRequestStub extends Illuminate\Foundation\Http\FormRequest
Expand All @@ -89,6 +118,11 @@ public function authorize()
{
return true;
}

public function afterValidation()
{
$this->replace(['name' => 'Jeffrey']);
}
}

class FoundationTestFormRequestForbiddenStub extends Illuminate\Foundation\Http\FormRequest
Expand All @@ -103,3 +137,20 @@ public function authorize()
return false;
}
}
class FoundationTestFormRequestHooks extends Illuminate\Foundation\Http\FormRequest
{
public function rules()
{
return ['name' => 'required'];
}

public function authorize()
{
return true;
}

public function beforeValidation()
{
$this->replace(['name' => 'Taylor']);
}
}