Skip to content

Commit

Permalink
Merge pull request #325 from TomOudeRengerink/disable-auto-prepend-fi…
Browse files Browse the repository at this point in the history
…eld-label

Allow disabling of auto prepending the field labels.
  • Loading branch information
willemwollebrants committed Dec 1, 2020
2 parents 9268ade + 9f29a1b commit 81515dc
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 1 deletion.
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,33 @@ V::lang('ar');

```

Disabling the {field} name in the output of the error message.

```php
use Valitron\Validator as V;

$v = new Valitron\Validator(['name' => 'John']);
$v->rule('required', ['name']);

// Disable prepending the labels
$v->setPrependLabels(false);

// Error output for the "false" condition
[
["name"] => [
"is required"
]
]

// Error output for the default (true) condition
[
["name"] => [
"name is required"
]
]

```

You can conditionally require values using required conditional rules. In this example, for authentication, we're requiring either a token when both the email and password are not present, or a password when the email address is present.
```php
// this rule set would work for either data set...
Expand Down
17 changes: 16 additions & 1 deletion src/Valitron/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ class Validator
*/
protected $stop_on_first_fail = false;

/**
* @var bool
*/
protected $prepend_labels = true;

/**
* Setup validation
*
Expand Down Expand Up @@ -144,6 +149,14 @@ public static function langDir($dir = null)
return static::$_langDir ?: dirname(dirname(__DIR__)) . '/lang';
}

/**
* @param bool $prepend_labels
*/
public function setPrependLabels($prepend_labels = true)
{
$this->prepend_labels = $prepend_labels;
}

/**
* Required field validator
*
Expand Down Expand Up @@ -1471,7 +1484,9 @@ protected function checkAndSetLabel($field, $message, $params)
}
}
} else {
$message = str_replace('{field}', ucwords(str_replace('_', ' ', $field)), $message);
$message = $this->prepend_labels
? str_replace('{field}', ucwords(str_replace('_', ' ', $field)), $message)
: str_replace('{field} ', '', $message);
}

return $message;
Expand Down
13 changes: 13 additions & 0 deletions tests/Valitron/ErrorMessagesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,19 @@ public function testErrorMessageIncludesFieldName()
$this->assertSame(array("Name is required"), $v->errors('name'));
}

/**
* Test the disabling of prepending the field labels
* to error messages.
*/
public function testErrorMessageExcludeFieldName()
{
$v = new Validator(array());
$v->setPrependLabels(false);
$v->rule('required', 'name');
$v->validate();
$this->assertSame(array("is required"), $v->errors('name'));
}

public function testAccurateErrorMessageParams()
{
$v = new Validator(array('num' => 5));
Expand Down

0 comments on commit 81515dc

Please sign in to comment.