动态更改验证规则

发布于 2024-12-11 06:32:33 字数 1198 浏览 2 评论 0 原文

我正在开发一个包含用户数据的表单,特别是电话号码字段。通常不需要电话号码,因此模型中唯一的验证规则是 usphone 规则。但是,如果用户提交此表单,则需要电话号码。我以为我可以简单地添加一个 validate 规则,设置模型并调用 validates 方法,但要么我做错了,要么它是没有按我预期的方式工作。

在我的控制器中:

# Update a few validation rules that are specific to this context
$this->Proposal->Requestor->validate['phone_number']['notempty'] = array(
  'rule'       => 'notEmpty',
  'message'    => 'Please enter a phone number so can can contact you with any questions about the work.',
  'allowEmpty' => false,
  'required'   => true,
);

$validationErrors = array();
$this->Proposal->Requestor->set( $this->data['Requestor'] ); # $this->data['Requestor']['phone_number'] only (no other requestor data)
if( !$this->Proposal->Requestor->validates( array( 'fieldList' => array( 'phone_number' ) ) ) ) {
  $validationErrors['Requestor'] = $this->Proposal->Requestor->validationErrors;
}

即使我将电话号码字段留空,也不会报告任何错误。在这种情况下,我向用户请求的唯一信息是他们的电话号码,因此请求者数据的其余部分为空,但我尝试合并其余的用户数据并我得到同样的结果。如果我删除 fieldList 选项,我会在不同的字段上收到错误,但空电话号码上仍然没有任何内容。

知道我在这里缺少什么吗?我已经在这个问题上折腾了好几个小时了,但还没有找到正确的答案。

谢谢。

I'm working on a form that contains user data, specifically a phone number field. The phone number typically isn't required so the only validation rule in the model is the usphone rule. However, if the user is submitting this form, the phone number becomes necessary. I thought I'd be able to simply add a validate rule on the fly, set the model and call the validates method, but either I'm doing it wrong or it's not working the way I expected.

In my controller:

# Update a few validation rules that are specific to this context
$this->Proposal->Requestor->validate['phone_number']['notempty'] = array(
  'rule'       => 'notEmpty',
  'message'    => 'Please enter a phone number so can can contact you with any questions about the work.',
  'allowEmpty' => false,
  'required'   => true,
);

$validationErrors = array();
$this->Proposal->Requestor->set( $this->data['Requestor'] ); # $this->data['Requestor']['phone_number'] only (no other requestor data)
if( !$this->Proposal->Requestor->validates( array( 'fieldList' => array( 'phone_number' ) ) ) ) {
  $validationErrors['Requestor'] = $this->Proposal->Requestor->validationErrors;
}

No errors are reported, even if I leave the phone number field empty. In this case, the only information I'm requesting from the user is their phone number, so the rest of the Requestor data is empty, but I've tried merging in the rest of the user data and I get the same result. If I remove the fieldList option, I get an error on a different field, but still nothing on the empty phone number.

Any idea what I'm missing here? I've been monkeying around with this for hours now and I just haven't found the right answer.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甜味拾荒者 2024-12-18 06:32:33

解决方案最终是双重的:

  1. 我在 phone_number 字段上已有规则,强制该值成为美国电话号码。该规则还将 allowEmpty 设置为 true,将 required 设置为 false。我想捕获一个空值,以便可以显示特别精确的消息。
  2. 我必须更新现有规则以翻转 allowEmptyrequired 值,并添加一个新规则,并将其 last 值设置为 正确。

在我的控制器操作中添加的最终更改如下所示:

$this->Proposal->Requestor->validate = Set::merge(
  $this->Proposal->Requestor->validate,
  array(
    'phone_number' => array(
      'notempty' => array(
        'rule'       => 'notEmpty',
        'message'    => 'Please enter a phone number so can can contact you with any questions about the work.',
        'allowEmpty' => false,
        'required'   => true,
        'last'       => true,
      ),
      'usphone' => array(
        'allowEmpty' => false,
        'required'   => true,
      ),
    )
  )
);

我不记得是否已验证鉴于 last 值,对现有 usphone 规则的更改是绝对必要的新规则,但这种组合运行良好。

The solution ended up being twofold:

  1. I had existing rule on the phone_number field that forced the value to be a US phone number. That rule also set allowEmpty to true and required to false. I wanted to catch an empty value so I could display a particularly precise message.
  2. I had to update the existing rule to flip the allowEmpty and required values and also add a new rule with its last value set to true.

The final change, added in my controller action looks like this:

$this->Proposal->Requestor->validate = Set::merge(
  $this->Proposal->Requestor->validate,
  array(
    'phone_number' => array(
      'notempty' => array(
        'rule'       => 'notEmpty',
        'message'    => 'Please enter a phone number so can can contact you with any questions about the work.',
        'allowEmpty' => false,
        'required'   => true,
        'last'       => true,
      ),
      'usphone' => array(
        'allowEmpty' => false,
        'required'   => true,
      ),
    )
  )
);

I can't remember whether I verified that the change to the existing usphone rule was strictly necessary given the last value of the new rule, but this combination is working fine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文