mirror of
https://github.com/vlucas/valitron.git
synced 2025-12-30 23:01:52 +00:00
Merge pull request #325 from TomOudeRengerink/disable-auto-prepend-field-label
Allow disabling of auto prepending the field labels.
This commit is contained in:
commit
81515dcc95
27
README.md
27
README.md
@ -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.
|
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
|
```php
|
||||||
// this rule set would work for either data set...
|
// this rule set would work for either data set...
|
||||||
|
|||||||
@ -83,6 +83,11 @@ class Validator
|
|||||||
*/
|
*/
|
||||||
protected $stop_on_first_fail = false;
|
protected $stop_on_first_fail = false;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var bool
|
||||||
|
*/
|
||||||
|
protected $prepend_labels = true;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Setup validation
|
* Setup validation
|
||||||
*
|
*
|
||||||
@ -144,6 +149,14 @@ class Validator
|
|||||||
return static::$_langDir ?: dirname(dirname(__DIR__)) . '/lang';
|
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
|
* Required field validator
|
||||||
*
|
*
|
||||||
@ -1471,7 +1484,9 @@ class Validator
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} 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;
|
return $message;
|
||||||
|
|||||||
@ -12,6 +12,19 @@ class ErrorMessagesTest extends BaseTestCase
|
|||||||
$this->assertSame(array("Name is required"), $v->errors('name'));
|
$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()
|
public function testAccurateErrorMessageParams()
|
||||||
{
|
{
|
||||||
$v = new Validator(array('num' => 5));
|
$v = new Validator(array('num' => 5));
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user