如何以覆盖形式隐藏出生日期字段?

发布于 2024-09-12 02:24:19 字数 571 浏览 3 评论 0原文

在我的挂钩形式更改中,这是出生日期数组:

[Birthdate] => Array(
  [#type] => fieldset
  [#title] => Birthdate
  [#weight] => 1
  [profile_birthdate] => Array(
    [#type] => date
    [#title] => Birthdate
    [#default_value] =>
    [#description] => The content of this field is kept private and will not be shown publicly.
    [#required] => 1
  )
)

我已经尝试过:

unset($form['Birthdate']['profile_birthdate']);
     unset($form['Birthdate']);

但这不起作用,因为我仍然收到“请输入有效的出生日期”消息。我希望隐藏该字段并且不显示任何消息。

In my hook form alter, this is the date of birth array:

[Birthdate] => Array(
  [#type] => fieldset
  [#title] => Birthdate
  [#weight] => 1
  [profile_birthdate] => Array(
    [#type] => date
    [#title] => Birthdate
    [#default_value] =>
    [#description] => The content of this field is kept private and will not be shown publicly.
    [#required] => 1
  )
)

I have tried:

unset($form['Birthdate']['profile_birthdate']);
     unset($form['Birthdate']);

and that does not work as I still get the "Please enter a valid date of birth" message. I want the field hidden and no message.

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

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

发布评论

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

评论(1

风苍溪 2024-09-19 02:24:19

错误消息的措辞(“...有效日期...”)暗示这是从表单验证函数发出的。根据写入方式,即使您已成功从表单中删除该字段,它可能仍会尝试验证该字段。

如果是这种情况,您需要使用不需要生日字段的自定义版本覆盖验证函数(检查 $form['#validate'] 中的内容代码>hook_form_alter()实现)。或者,您可以将其转换为 '#type' =>,而不是删除该字段。 '隐藏''#type' => 'value',并提供一个通过验证的默认值,但最终您将分配虚假的出生日期,这可能不是您想要的。

The phrasing of the error message ('... a valid date...') hints on this being issued from a form validation function. Depending on how that is written, it might still try to validate the field, even if you removed it successfully from the form.

If that is the case, you'll need to either override the validation function with a custom version that does not expect the birthdate field (check the content of $form['#validate'] in your hook_form_alter() implementation). Alternatively, instead of removing the field, you could turn it into '#type' => 'hidden' or '#type' => 'value', and provide a default value that passes validation, but then you'll end up assigning bogus birth dates, which might not be what you want.

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