Skip to content
Closed
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
18 changes: 7 additions & 11 deletions src/Illuminate/Foundation/Http/FormRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,15 @@ protected function getValidatorInstance()
*/
protected function createDefaultValidator(ValidationFactory $factory)
{
$messages = [];

if (method_exists($this, 'messages')) {
$messages = $this->container->call([$this, 'messages']);
}

return $factory->make(
$this->validationData(), $this->container->call([$this, 'rules']),
$this->messages(), $this->attributes()
$messages, $this->attributes()
);
}

Expand Down Expand Up @@ -180,16 +186,6 @@ public function validated()
})->unique()->toArray());
}

/**
* Get custom messages for validator errors.
*
* @return array
*/
public function messages()
{
return [];
}

/**
* Get custom attributes for validator errors.
*
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Validation/ValidatesWhenResolvedTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function validateResolved()

$instance = $this->getValidatorInstance();

if (! $instance->passes()) {
if ($instance->fails()) {
$this->failedValidation($instance);
}
}
Expand Down
53 changes: 53 additions & 0 deletions tests/Foundation/FoundationFormRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
use Exception;
use Mockery as m;
use PHPUnit\Framework\TestCase;
use Illuminate\Auth\AuthManager;
use Illuminate\Routing\Redirector;
use Illuminate\Container\Container;
use Illuminate\Routing\UrlGenerator;
use Illuminate\Http\RedirectResponse;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\ValidationException;
use Illuminate\Contracts\Translation\Translator;
use Illuminate\Validation\Factory as ValidationFactory;
use Illuminate\Contracts\Validation\Factory as ValidationFactoryContract;
Expand Down Expand Up @@ -71,6 +73,38 @@ public function test_prepare_for_validation_runs_before_validation()
$this->createRequest([], FoundationTestFormRequestHooks::class)->validateResolved();
}

public function test_method_injection_in_form_request_methods()
{
$container = tap(new Container, function (Container $container) {
$container->instance(
ValidationFactoryContract::class,
$this->createValidationFactory($container)
);
$translator = m::mock(Translator::class);
$translator->shouldReceive('trans')->with('messages.foobar')->andReturn('The name is required.');
$container->instance(
Translator::class,
$translator
);
$authManager = m::mock(AuthManager::class);
$authManager->shouldReceive('check')->andReturn(true);
$container->instance(
AuthManager::class,
$authManager
);
});

$request = FoundationTestFormRequestWithInjectedServicesStub::create('/', 'GET', ['name' => '']);
$request->setRedirector($this->createMockRedirector($request))->setContainer($container);

try {
$request->validateResolved();
$this->fail('A Illuminate\Validation\ValidationException was supposed to be thrown.');
} catch (ValidationException $exception) {
$this->assertTrue(in_array('The name is required.', $exception->errors()['name']));
}
}

/**
* Catch the given exception thrown from the executor, and return it.
*
Expand Down Expand Up @@ -222,3 +256,22 @@ public function prepareForValidation()
$this->replace(['name' => 'Taylor']);
}
}
class FoundationTestFormRequestWithInjectedServicesStub extends FormRequest
{
public function rules(Translator $translator)
{
return ['name' => 'required'];
}

public function authorize(AuthManager $authManager)
{
return $authManager->check();
}

public function messages(Translator $translator)
{
return [
'name.required' => $translator->trans('messages.foobar'),
];
}
}