如何以覆盖形式隐藏出生日期字段?
在我的挂钩形式更改中,这是出生日期数组:
[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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
错误消息的措辞(“...有效日期...”)暗示这是从表单验证函数发出的。根据写入方式,即使您已成功从表单中删除该字段,它可能仍会尝试验证该字段。
如果是这种情况,您需要使用不需要生日字段的自定义版本覆盖验证函数(检查
$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 yourhook_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.