Permalink
Browse files

Added Messages and Attibutes in Validation

  • Loading branch information...
1 parent c77bf44 commit c353020d1dd73c65dc034c51f1f258bf624a5ee6 @moura137 moura137 committed Jan 11, 2017
Showing with 158 additions and 17 deletions.
  1. +87 −3 src/NwLaravel/Validation/BaseValidator.php
  2. +71 −14 tests/Validation/BaseValidatorTest.php
@@ -2,28 +2,47 @@
namespace NwLaravel\Validation;
use Illuminate\Validation\Factory;
-use Prettus\Validator\LaravelValidator;
+use Prettus\Validator\AbstractValidator;
/**
* Class BaseValidator
* @abstract
*/
-abstract class BaseValidator extends LaravelValidator
+abstract class BaseValidator extends AbstractValidator
{
/**
+ * Validator
+ *
+ * @var \Illuminate\Validation\Factory
+ */
+ protected $validator;
+
+ /**
* @var string
*/
protected $keyName;
/**
+ * @var array
+ */
+ protected $messages = [];
+
+ /**
+ * @var array
+ */
+ protected $attributes = [];
+
+ /**
* Construct
*
* @param \Illuminate\Validation\Factory $validator
*/
public function __construct(Factory $validator)
{
$this->validator = $validator;
- $this->rules = array_merge_recursive($this->rules, (array) $this->makeRules());
+ $this->rules = array_merge_recursive((array) $this->rules, $this->makeRules());
+ $this->messages = array_merge_recursive((array) $this->messages, $this->makeMessages());
+ $this->attributes = array_merge_recursive((array) $this->attributes, $this->makeAttributes());
}
/**
@@ -37,6 +56,50 @@ protected function makeRules()
}
/**
+ * Make Messages
+ *
+ * @return array
+ */
+ protected function makeMessages()
+ {
+ return [];
+ }
+
+ /**
+ * Make Attributes
+ *
+ * @return array
+ */
+ protected function makeAttributes()
+ {
+ return [];
+ }
+
+ /**
+ * Get Messages
+ *
+ * @param array $messages
+ *
+ * @return array
+ */
+ public function getMessages()
+ {
+ return $this->messages;
+ }
+
+ /**
+ * Get Attributes
+ *
+ * @param array $attributes
+ *
+ * @return array
+ */
+ public function getAttributes()
+ {
+ return $this->attributes;
+ }
+
+ /**
* Get Validator
*
* @return \Illuminate\Validation\Factory
@@ -79,6 +142,27 @@ public function getRules($action = null)
}
/**
+ * Pass the data and the rules to the validator
+ *
+ * @param string $action
+ * @return bool
+ */
+ public function passes($action = null)
+ {
+ $rules = $this->getRules($action);
+ $messages = $this->getMessages();
+ $attributes = $this->getAttributes();
+ $validator = $this->validator->make($this->data, $rules, $messages, $attributes);
+
+ if ($validator->fails()) {
+ $this->errors = $validator->messages();
+ return false;
+ }
+
+ return true;
+ }
+
+ /**
* Parser Validation Rules
*
* @param array $rules
@@ -14,19 +14,80 @@ public function testConstructInstanceOf()
$base->with(['id' => '420']);
$base->setKeyName('id_foo');
- $this->assertInstanceOf('Prettus\Validator\LaravelValidator', $base);
+ $this->assertInstanceOf('Prettus\Validator\AbstractValidator', $base);
+ $this->assertAttributeEquals($factory, 'validator', $base);
$this->assertEquals($factory, $base->getValidator());
$this->assertAttributeEquals('id_foo', 'keyName', $base);
$this->assertEquals([], $base->getRules('bar'));
+
+ $messages = ['email.email' => 'Email Errado'];
+ $this->assertAttributeEquals($messages, 'messages', $base);
+ $this->assertEquals($messages, $base->getMessages());
+
+ $attributes = ['email' => 'Email Address'];
+ $this->assertAttributeEquals($attributes, 'attributes', $base);
+ $this->assertEquals($attributes, $base->getAttributes());
$base->setId(33);
$expected = [
'email' => ['required', 'email', 'unique:users,email,33,id_foo'],
'foo_id' => ['exists:tablename,id,id,420'],
- 'name' => ['required'],
];
$this->assertEquals($expected, $base->getRules('create'));
}
+
+ public function testPassesShouldReceiveTrue()
+ {
+ $factory = m::mock('Illuminate\Validation\Factory');
+ $validator = m::mock('Illuminate\Validation\Validator');
+ $validator->shouldReceive('fails')->once()->andReturn(false);
+ $validator->shouldReceive('messages')->never();
+
+ $data = ['email' => 'foo@bar.com', 'name' => 'Renato'];
+ $messages = ['email.email' => 'Email Errado'];
+ $attributes = ['email' => 'Email Address'];
+ $rules = [
+ 'email' => ['required', 'email', 'unique:users,email,33,id_foo'],
+ 'foo_id' => ['exists:tablename,id,id,420'],
+ 'name' => ['required'],
+ ];
+ $factory->shouldReceive('make')
+ ->once()
+ // ->with($data, $rules, $messages, $attributes)
+ ->andReturn($validator);
+
+ $base = new FooValidator($factory);
+ $base->with($data);
+
+ $this->assertTrue($base->passes('create'));
+ $this->assertAttributeEquals([], 'errors', $base);
+ }
+
+ public function testPassesShouldReceiveFails()
+ {
+ $factory = m::mock('Illuminate\Validation\Factory');
+ $validator = m::mock('Illuminate\Validation\Validator');
+ $validator->shouldReceive('fails')->once()->andReturn(true);
+ $validator->shouldReceive('messages')->once()->andReturn(['ErrorMessages']);
+
+ $data = ['email' => 'foo@bar.com', 'name' => 'Renato'];
+ $messages = ['email.email' => 'Email Errado'];
+ $attributes = ['email' => 'Email Address'];
+ $rules = [
+ 'name' => ['required'], ['exists:[other]'],
+ ];
+ $factory->shouldReceive('make')
+ ->once()
+ // ->with($data, $rules, $messages, $attributes)
+ ->andReturn($validator);
+
+ $base = new FooValidator($factory);
+ $base->with($data);
+
+ $this->assertFalse($base->passes('update'));
+ $this->assertAttributeEquals(['ErrorMessages'], 'errors', $base);
+
+ }
}
class FooValidator extends BaseValidator
@@ -36,19 +97,15 @@ class FooValidator extends BaseValidator
'email' => 'required|email|unique:users',
'foo_id' => 'exists:tablename,id,id,[id]',
],
- 'update' => ['name' => 'required|unique:foo'],
+ 'update' => ['name' => 'required|exists:[other]'],
'delete' => ['id' => 'not_exists:foo'],
];
- /**
- * MakeRules
- *
- * @return array
- */
- public function makeRules()
- {
- return [
- 'create' => ['name' => 'required'],
- ];
- }
+ protected $messages = [
+ 'email.email' => 'Email Errado'
+ ];
+
+ protected $attributes = [
+ 'email' => 'Email Address',
+ ];
}

0 comments on commit c353020

Please sign in to comment.