我按照这篇文章,为信用卡到期日设置了一个月份小部件:
How to display a month-year dropdown in Symfony2
现在我尝试将字段设置为表单提交期间选择的值。我该怎么做?
我的表格类型:
$builder->add('card_expiration_date', 'date', array(
'mapped' => false,
'widget' => 'choice',
'empty_value' => array('year' => 'Year', 'month' => 'Month', 'day' => 'Day'),
'format' => 'dd-MM-yyyy',
'input' => 'string',
'data' => date('Y-m-d'),
'years' => range(date('Y'), date('Y') + 10)
));
我一直在尝试做的一些事情是:
试用1 :
{{ form_widget(form.card_expiration_date.month, {'value': ccMonth}) }}
{{ form_widget(form.card_expiration_date.year, {'value': ccYear}) }}
这会正确设置值,但问题是它仍然会渲染form.card_expiration_date
试用2:
{% if ccMonth == '' and ccYear == '' %}
{{ form_widget(form.card_expiration_date, {'date_pattern': '<span style="display: none;">{{ day }}</span> {{ month }} <span class="delim">/</span> {{ year }}'}) }}
{% else %}
{{ form_widget(form.card_expiration_date, {'date_pattern': '<span style="display: none;">{{ day }}</span> {{ month }} <span class="delim">/</span> {{ year }}', 'value': cmonth~'/'~cyear }) }}
{% endif %}
我很确定有更好的方法来设置我的试用版2的值。这看起来不对。任何帮助将不胜感激。
修改
试用3:
{% if cmonth == '' and cyear == '' %}
{{ form_widget(form.card_expiration_date, {'date_pattern': '<span style="display: none;">{{ day }}</span> {{ month }} <span class="delim">/</span> {{ year }}'}) }}
{% else %}
{{ form_widget(form.card_expiration_date, {'date_pattern': '<span style="display: none;">{{ day }}</span> {{ month }} <span class="delim">/</span> {{ year }}'}) }}
{{ form_widget(form.card_expiration_date.month, {'value': cmonth}) }}
{{ form_widget(form.card_expiration_date.year, {'value': cyear}) }}
{% endif %}
EDIT2
{% set ccMonth = cardInfo.card.expdate|slice(2, 2) %}
{% set cmonth = (cardInfo.card)? ((ccMonth starts with '0')? ccMonth|slice(1, 1) : ccMonth) : '' %}
{% set cyear = (cardInfo.card)? '20'~cardInfo.card.expdate|slice(0, 2) : '' %}
<div class="col-lg-8">
{% if cmonth == '' and cyear == '' %}
{{ form_widget(form.card_expiration_date, {'date_pattern': '<span style="display: none;">{{ day }}</span> {{ month }} <span class="delim">/</span> {{ year }}'}) }}
{% else %}
{{ form_widget(form.card_expiration_date, {'date_pattern': '<span style="display: none;">{{ day }}</span> {{ month }} <span class="delim">/</span> {{ year }}'}) }}
{{ form_widget(form.card_expiration_date.month, {'value': cmonth}) }}
{{ form_widget(form.card_expiration_date.year, {'value': cyear}) }}
{% endif %}
{{ form_errors(form.card_expiration_date) }}
</div>
EDIT3
这就是我的所作所为。不确定这是否正确,但似乎有效。
控制器:
$form = $this->createForm(new BillingType($em, $ccInfo), $billing, array(
'action' => $this->generateUrl('save_billing', array('id' => $id)),
'method' => 'POST'
));
BillingType:
function __construct(EntityManager $em, $ccInfo)
{
$this->em = $em;
$this->ccInfo = $ccInfo;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$date = date('Y-m-d');
if( ! is_null($this->ccInfo['card']))
{
if (substr($this->ccInfo['card']['expdate'], -2, 1) == '0')
{
$ccMonth = substr($this->ccInfo['card']['expdate'], -1, 1);
}
else
{
$ccMonth = substr($this->ccInfo['card']['expdate'], -2, 2);
}
$ccYear = '20'.substr($this->ccInfo['card']['expdate'], 0, 2);
$date = date('Y-m-d', strtotime($ccYear.'-'.$ccMonth.'-01'));
}
$builder->add('card_expiration_date', 'date', array(
'mapped' => false,
'widget' => 'choice',
'empty_value' => array('year' => 'Year', 'month' => 'Month', 'day' => 'Day'),
'format' => 'dd-MM-yyyy',
'input' => 'string',
'data' => $date,
'years' => range(date('Y'), date('Y') + 10)
));
}
答案 0 :(得分:0)
都不是。您应该设置基础对象(或数组)的数据。如果存在值,form_widget
视图助手将已呈现该值。如果您处于控制器环境中,那么已有一种方法可以执行此操作
$form->handleRequest($this->getRequest());
这会将请求数据绑定到表单的数据对象。如果您想分享更多详细信息,我可以帮助您解决具体实施问题。