【问题标题】:CakePHP Form helper date field, but only use a select on the month. Allow day and year to be text fieldCakePHP 表单助手日期字段,但仅使用月份选择。允许日期和年份作为文本字段
【发布时间】:2013-01-30 07:49:28
【问题描述】:

我正在尝试获取 cakephp 日期字段以将月份输出为选择,并将年份作为文本字段而不是两者都作为选择字段。

这是我认为输出日期类型字段的传统 Cake 方式(我添加了 dateFormat 属性以当前仅显示 2 个选择框(跳过当天)):

<?php echo $this->Form->input('Payment.credit_card_expiration', array('type' => 'date', 'label' => 'Expiration date', 'dateFormat' => 'MY')) ?>

我暂时做的是我认为是一种 hack,即将它分成 2 个字段。我不喜欢它,因为它没有显示该字段的validationErrors(主要问题)并且它看起来像丑陋的代码(为什么要编写丑陋的代码,尤其是如果蛋糕有办法做到这一点)。

<?php
echo $this->Form->input('Payment.credit_card_expiration.month', array('type' => 'select', 'label' => 'Expiration date', 'options' => array('Month','Jan','Feb','...')));
echo $this->Form->input('Payment.credit_card_expiration.year', array('type' => 'text', 'label' => '&nbsp;', 'placeholder' => 'Year'));

if(isset($this->validationErrors['Payment']['credit_card_expiration'])) {
    echo '<div class="error-message">' . $this->validationErrors['Payment']['credit_card_expiration'][0] . '</div>';
    echo '<div class="row"><div class="span5">&nbsp;</div></div>';
}
?>

作为说明,我考虑过可能必须扩展 FormHelper。我以前没有这样做过,但我确信我可以做到。我只是想知道是否有内置方式或优雅的 hack。

这是我在 StackOverflow 上的第一篇文章,所以我们开始吧,我会看看会发生什么。感谢您的任何想法或见解,非常感谢。

谢谢!

【问题讨论】:

    标签: cakephp cakephp-2.0


    【解决方案1】:

    CakePHP FormHelper 说你可以使用 $this-&gt;Form-&gt;month() 来做这个

    【讨论】:

    • 快速回复。谢谢你。我会看看我能不能让它工作。我很感激:)
    猜你喜欢
    • 2013-06-07
    • 1970-01-01
    • 2011-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多