如果返回false,则Cakephp 3将在验证提供程序中更新值

时间:2018-07-09 10:50:02

标签: validation cakephp

在我的模型中,我想在验证提供程序中更新一个值

例如,如果$ value ='4012 *' 它不是字母数字,所以我希望$ value为空以将其保存到数据库中

Model / Table / ProductsTable

public function validationDefault(Validator $validator)
{
    $validator 
        ->allowEmpty('code')
        ->add('code', 'alphaNum', [
              'rule' => 'isalphaNum',
              'message' => 'Code is wrong',
              'provider' => 'table']);
    return $validator;
}

public function isalphaNum($value, array $context)
{
  if (!ctype_alnum($value)) {
    //Update a warning table
    $warning = new Warnings;
    $warning->insert('Code is wrong',
                     $context['data']['code'],
                     $context['field'],
                     $value
                   );
   //UPDATE THE VALUE TO EMPTY
   $value = "";

  } 
    return true;
}

问题是当我在isalphaNum函数中调试值时,它为空,很好!但是在验证之后,并将其保存在数据库中时,这是我在此示例“ 4012 *”中保存的初始值。

我应该在哪里更改我的价值?

0 个答案:

没有答案
相关问题