调用未定义的方法 Cake\Validation\Validator::money()
Call to undefined method Cake\Validation\Validator::money()
我正在尝试使用验证器方法 money() 来验证应包含美元金额的字段。我收到错误 "Call to undefined method Cake\Validation\Validator::money()"。
我能够访问方法 minLenthBytes() 和 naturalNumber() 而不会出现同样的错误。从 2016 年开始,我确实在 github 上看到了一些关于 money() 的问题,但看起来它们已经关闭了。食谱表明 money() 应该存在 (https://api.cakephp.org/3.8/class-Cake.Validation.Validation.html#_money)。我确实在 \vendor\cakephp\cakephp\src\Validation\Validation.php.
中找到了它
这是我的代码
$validator
->numeric('ship_amt') //no error
->minLengthBytes('ship_amt', 25) //no error, testing only
->money('ship_amt', 'Please enter a valid monetary amount.') //error -> call to undefined method. Same error with or without message.
->naturalNumber('ship_amt') //no error, testing only
->greaterThanOrEqual('ship_amt', 0) //no error, testing only
->notEmptyString('ship_amt'); // no error, testing only
这是调试日志的顶部(如果需要,我可以提供更多):
[Error] Call to undefined method Cake\Validation\Validator::money()
#0 XXX\vendor\cakephp\cakephp\src\Validation\ValidatorAwareTrait.php(178): App\Model\Table\ProjectsTable->validationDefault(Object(Cake\Validation\Validator))
#1 XXX\vendor\cakephp\cakephp\src\Validation\ValidatorAwareTrait.php(151): Cake\ORM\Table->createValidator('default')
#2 XXX\vendor\cakephp\cakephp\src\View\Form\EntityContext.php(593): Cake\ORM\Table->getValidator('default')
#3 XXX\vendor\cakephp\cakephp\src\View\Form\EntityContext.php(504): Cake\View\Form\EntityContext->_getValidator(Array)
#4 XXX\vendor\cakephp\cakephp\src\View\Helper\FormHelper.php(1463): Cake\View\Form\EntityContext->getRequiredMessage('company_id')
#5 XXX\vendor\cakephp\cakephp\src\View\Helper\FormHelper.php(1354): Cake\View\Helper\FormHelper->_magicOptions('company_id', Array, false)
#6 XXX\vendor\cakephp\cakephp\src\View\Helper\FormHelper.php(1171):
Specs: CakePHP 3.8 on Windows Server 2012, PHP 7.3.1
离开几年后,我对 v3.x 很陌生。如果有任何菜鸟错误,我深表歉意。我现在感觉自己像个严肃的菜鸟。
在此先感谢您的帮助。不胜感激。
,Money 验证器的代理似乎已合并,但此后不久又于 2016 年再次删除
Remove this proxy until we have a better money() validation method.
The current one is not very locale aware and isn't great at handling a
variety of currencies. We can re-implement the money() proxy when we
have a better validation method to use.
可能您只需通过 add()
手动调用规则,例如:
$validator->add('ship_amt', 'money', [
'rule' => ['money', 'left'],
'message' => 'Please enter a valid monetary amount.'
]);
来自 CakePHP 书籍:
CakePHP provides a basic suite of validation methods in the Validation
class. The Validation class contains a variety of static methods that
provide validators for several common validation situations.
The API documentation for the Validation class provides a good list of
the validation rules that are available, and their basic usage.
Some of the validation methods accept additional parameters to define
boundary conditions or valid options. You can provide these boundary
conditions and options as follows:
$validator = new Validator();
$validator
->add('title', 'minLength', [
'rule' => ['minLength', 10]
])
->add('rating', 'validValue', [
'rule' => ['range', 1, 5]
]);
使用货币验证:
$validator
->add('ship_amt', 'validValue', [
'rule' => ['money', 'right']
]);
来自 api:
/**
* Checks that a value is a monetary amount.
*
* @param string $check Value to check
* @param string $symbolPosition Where symbol is located (left/right)
* @return bool Success
*/
public static function money($check, $symbolPosition = 'left')
{
$money = '(?!0,?\d)(?:\d{1,3}(?:([, .])\d{3})?(?:\d{3})*|(?:\d+))((?!)[,.]\d{1,2})?';
if ($symbolPosition === 'right') {
$regex = '/^' . $money . '(?<!\x{00a2})\p{Sc}?$/u';
} else {
$regex = '/^(?!\x{00a2})\p{Sc}?' . $money . '$/u';
}
return static::_check($check, $regex);
}
我正在尝试使用验证器方法 money() 来验证应包含美元金额的字段。我收到错误 "Call to undefined method Cake\Validation\Validator::money()"。
我能够访问方法 minLenthBytes() 和 naturalNumber() 而不会出现同样的错误。从 2016 年开始,我确实在 github 上看到了一些关于 money() 的问题,但看起来它们已经关闭了。食谱表明 money() 应该存在 (https://api.cakephp.org/3.8/class-Cake.Validation.Validation.html#_money)。我确实在 \vendor\cakephp\cakephp\src\Validation\Validation.php.
中找到了它这是我的代码
$validator
->numeric('ship_amt') //no error
->minLengthBytes('ship_amt', 25) //no error, testing only
->money('ship_amt', 'Please enter a valid monetary amount.') //error -> call to undefined method. Same error with or without message.
->naturalNumber('ship_amt') //no error, testing only
->greaterThanOrEqual('ship_amt', 0) //no error, testing only
->notEmptyString('ship_amt'); // no error, testing only
这是调试日志的顶部(如果需要,我可以提供更多):
[Error] Call to undefined method Cake\Validation\Validator::money()
#0 XXX\vendor\cakephp\cakephp\src\Validation\ValidatorAwareTrait.php(178): App\Model\Table\ProjectsTable->validationDefault(Object(Cake\Validation\Validator))
#1 XXX\vendor\cakephp\cakephp\src\Validation\ValidatorAwareTrait.php(151): Cake\ORM\Table->createValidator('default')
#2 XXX\vendor\cakephp\cakephp\src\View\Form\EntityContext.php(593): Cake\ORM\Table->getValidator('default')
#3 XXX\vendor\cakephp\cakephp\src\View\Form\EntityContext.php(504): Cake\View\Form\EntityContext->_getValidator(Array)
#4 XXX\vendor\cakephp\cakephp\src\View\Helper\FormHelper.php(1463): Cake\View\Form\EntityContext->getRequiredMessage('company_id')
#5 XXX\vendor\cakephp\cakephp\src\View\Helper\FormHelper.php(1354): Cake\View\Helper\FormHelper->_magicOptions('company_id', Array, false)
#6 XXX\vendor\cakephp\cakephp\src\View\Helper\FormHelper.php(1171):
Specs: CakePHP 3.8 on Windows Server 2012, PHP 7.3.1
离开几年后,我对 v3.x 很陌生。如果有任何菜鸟错误,我深表歉意。我现在感觉自己像个严肃的菜鸟。
在此先感谢您的帮助。不胜感激。
Remove this proxy until we have a better money() validation method. The current one is not very locale aware and isn't great at handling a variety of currencies. We can re-implement the money() proxy when we have a better validation method to use.
可能您只需通过 add()
手动调用规则,例如:
$validator->add('ship_amt', 'money', [
'rule' => ['money', 'left'],
'message' => 'Please enter a valid monetary amount.'
]);
来自 CakePHP 书籍:
CakePHP provides a basic suite of validation methods in the Validation class. The Validation class contains a variety of static methods that provide validators for several common validation situations.
The API documentation for the Validation class provides a good list of the validation rules that are available, and their basic usage.
Some of the validation methods accept additional parameters to define boundary conditions or valid options. You can provide these boundary conditions and options as follows:
$validator = new Validator();
$validator
->add('title', 'minLength', [
'rule' => ['minLength', 10]
])
->add('rating', 'validValue', [
'rule' => ['range', 1, 5]
]);
使用货币验证:
$validator
->add('ship_amt', 'validValue', [
'rule' => ['money', 'right']
]);
来自 api:
/**
* Checks that a value is a monetary amount.
*
* @param string $check Value to check
* @param string $symbolPosition Where symbol is located (left/right)
* @return bool Success
*/
public static function money($check, $symbolPosition = 'left')
{
$money = '(?!0,?\d)(?:\d{1,3}(?:([, .])\d{3})?(?:\d{3})*|(?:\d+))((?!)[,.]\d{1,2})?';
if ($symbolPosition === 'right') {
$regex = '/^' . $money . '(?<!\x{00a2})\p{Sc}?$/u';
} else {
$regex = '/^(?!\x{00a2})\p{Sc}?' . $money . '$/u';
}
return static::_check($check, $regex);
}