Skip to content

Commit

Permalink
Merge pull request #14 from BlakvGhost/fix-required-validation-rule-f…
Browse files Browse the repository at this point in the history
…or-missing-fields

Fix required rule validation for missing fields
  • Loading branch information
BlakvGhost authored May 18, 2024
2 parents 8195f32 + b4dc64e commit ead4cce
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/Rules/RequiredRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ public function passes(string $field, $value, array $data): bool
{
// Set the field property for use in the message method.
$this->field = $field;

$data[$field] = isset($data[$field]) ? $data[$field] : '';
// Check if the field is set in the data and not empty.
return isset($data[$field]) && !empty($data[$field]);
return !empty($data[$field]);
}

/**
Expand Down
6 changes: 4 additions & 2 deletions src/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,9 @@ protected function validate()
*/
protected function checkPasses(mixed $validator, string $field, ?string $ruleName = null)
{
if (isset($this->data[$field]) && !$validator->passes($field, $this->data[$field], $this->data)) {
if(!isset($this->data[$field]) && $ruleName !== 'required') return ;

if (!$validator->passes($field, $this->data[$field], $this->data)) {

$assert = isset($ruleName) && isset($this->messages[$field][$ruleName]);

Expand All @@ -140,7 +142,7 @@ protected function checkPasses(mixed $validator, string $field, ?string $ruleNam
*/
protected function validateConstructorInputs()
{
if (empty($this->data)) {
if (!isset($this->data)) {
throw new ValidatorException(
LangManager::getTranslation('validation.empty_data')
);
Expand Down
6 changes: 6 additions & 0 deletions tests/Feature/RuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@

$validator = new Validator(['field' => ''], ['field' => 'required']);
expect($validator->isValid())->toBeFalse();

$validator = new Validator([], ['field' => 'required']);
expect($validator->isValid())->toBeFalse();

expect($validator->getErrors()['field'][0])->toBe(
LangManager::getTranslation('validation.required_rule', ['attribute' => 'field'])
Expand All @@ -21,6 +24,9 @@

$validator = new Validator(['username' => 'value'], ['username' => 'max:5']);
expect($validator->isValid())->toBeTrue();

$validator = new Validator([], ['username' => 'max:5']);
expect($validator->isValid())->toBeTrue();

$validator = new Validator(['username' => 'value_long'], ['username' => 'max:5']);
expect($validator->isValid())->toBeFalse();
Expand Down
8 changes: 4 additions & 4 deletions tests/Feature/ValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
use BlakvGhost\PHPValidator\Validator;
use BlakvGhost\PHPValidator\ValidatorException;

it('throws exception if data is empty', function () {
expect(fn () => new Validator([], ['name' => 'string']))
->toThrow(ValidatorException::class, LangManager::getTranslation('validation.empty_data'));
});
// it('throws exception if data is empty', function () {
// expect(fn () => new Validator([], ['name' => 'string']))
// ->toThrow(ValidatorException::class, LangManager::getTranslation('validation.empty_data'));
// });

it('throws exception if rules are empty', function () {
expect(fn () => new Validator(['name' => 'John'], []))
Expand Down

0 comments on commit ead4cce

Please sign in to comment.