1
我跟着這個職位,設立信用卡到期日一個月一年部件:Symfont嫩枝月年設置爲選擇的值在形式
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}) }}
這正確設置值,但與此相關的問題是,它仍然呈現出形式。卡爾德諾維奇日期
試驗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的值。這只是看起來不正確的。任何幫助將不勝感激。
EDIT
試驗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)
));
}
不太清楚控制器上下文的含義。正如您在「表單」類型中看到的那樣,該字段未映射。請參閱我的帖子中的編輯2。似乎無法在此處發佈所有代碼。 – softie
未映射的字段意味着它不會持久保存到數據庫。但是,這並不意味着它不能通過請求訪問。@Peter Bailey的意思是將當前請求綁定到您的表單(提交後)您的表單字段應該自動填入提交之前所持有的數據。 – Artamiel
我想使用樹枝設置值。那麼我如何從控制器上下文中執行它?一個例子可能嗎? 有沒有一種方法可以在樹枝上明確設置card_expiration_date.day,card_expiration_date.month和card_expiration_date.year? – softie