Transformador de dados:
<? php
namespace ibanu MainBundle Form DataTransformer;
use Symfony Component Form DataTransformerInterface;
use Symfony Component Form Exception TransformationFailedException;
/ **
*
* @author ion
* /
class DurationToIntegerTransformer implementa DataTransformerInterface
{
public function transform($duration)
{
if (null === $duration) {
$duration = 0;
}
$hours = floor($duration / 60);
$minutes = $duration % 60;
return array('hours' => $hours, 'minutes' => $minutes);
}
public function reverseTransform($values)
{
$hours = isset($values['hours']) ? $values['hours'] : 0 ;
$minutes = isset($values['minutes']) ? $values['minutes'] : 0 ;
if (!is_int($hours) && !is_int($minutes)) {
throw new TransformationFailedException('Expected one integer.');
}
return $hours * 60 + $minutes;
}
}
Tipo de formulário:
<? php
namespace ibanu MainBundle Form Type;
use Symfony Component Form AbstractType;
use Symfony Component Form FormBuilderInterface;
use Symfony Component OptionsResolver OptionsResolverInterface;
use ibanu MainBundle Form DataTransformer DurationToIntegerTransformer;
classe DurationType estende AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$transformer = new DurationToIntegerTransformer();
$builder->add(
$builder->create('hours', 'integer', array('error_bubbling' => false, 'empty_data' => 0))
);
$builder->add(
$builder->create('minutes', 'integer', array('error_bubbling' => false, 'empty_data' => 0))
);
$builder->addModelTransformer($transformer);
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'hours' => range(0, 256),
'minutes' => range(0, 59),
'error_bubbling' => false,
'compound' => true,
'required' => false
));
}
public function getParent()
{
return 'text';
}
public function getName()
{
return 'duration';
}
}
Uso:
$builder
->add('estimated', 'duration')