I followed this post, to set up a month year widget for credit card expiry date:
How to display a month-year dropdown in Symfony2
Now I am trying to set the fields to the values selected during form submission. How do I do this?
My form Type:
$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)
));
Some of the things I've been trying to do are:
Trial 1:
{{ form_widget(form.card_expiration_date.month, {'value': ccMonth}) }}
{{ form_widget(form.card_expiration_date.year, {'value': ccYear}) }}
This sets the values correctly but the issue with this is that it still renders out the form.card_expiration_date
Trial 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 %}
I am pretty sure there's a better way to set the value on my Trial 2. That just doesn't look right. Any help will be appreciated.
EDIT
Trial 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
Here's what I did. Not sure if it's the right way, but seems to work.
Controller:
$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)
));
}
Neither. You should set the data of the underlying object (or array). The form_widget
view helper will already render the value if one is present. If you're in a controller context, there's already a method for doing this
$form->handleRequest($this->getRequest());
This will bind the request data to the form's data object. I can help you iron out your specific implementation if you want to share more details.