在表单中使用 DateIntervalType class

Using DateIntervalType class in form

如何在 Symfony 项目中实现新的 DateIntervalType 字段?

$builder->add('remindEvery', DateIntervalType::class, array(
    'widget'      => 'integer', // render a text field for each part
    // 'input'    => 'string',  // if you want the field to return a ISO 8601 string back to you

    // customize which text boxes are shown
    'with_years'  => false,
    'with_months' => false,
    'with_days'   => true,
    'with_hours'  => true,
));        

Symfony 3.2 中引入了 DateIntervalType 字段类型。 该字段允许用户 select 一个时间间隔。例如,如果您想让用户选择他们接收状态电子邮件的频率,他们可以使用此字段来选择间隔,例如每“10 分钟”或“3 天”。

在实体 WorkingTime 中创建字段:

class WorkingTime
{
   /**
   * @var int
   *
   * @ORM\Column(name="id", type="integer")
   * @ORM\Id
   * @ORM\GeneratedValue(strategy="AUTO")
   */
   private $id;

  /**
  * @var string
  *
  * @ORM\Column(
  *     name="duration_task", type="string", length=25
  * )
  */
 private $durationTask;

// ...more fields

 /**
 * ##################################################
 * Getter & Setter
 * ##################################################
 */


/**
 * Get id
 *
 * @return int
 */
public function getId()
{
    return $this->id;
}

/**
 * Get durationTask
 *
 * @return string
 */
public function getDurationTask()
{
    return $this->durationTask;
}

/**
 * Set durationTask
 *
 * @param string $durationTask
 *
 * @return WorkingTime
 */
public function setDurationTask( $durationTask )
{
    $this->durationTask = $durationTask;

    return $this;
}

// ...

在控制器中:

$newWorkingTime = new WorkingTime();

// you can set default duration: 0 hour 1 minute 
$newWorkingTime
        ->setDurationZz( 'PT0H1M' );

$form = $this->createForm( WorkingTimeType::class, $newWorkingTime );
$form->handleRequest( $request )->getData( $newWorkingTime );

// save to database
if ( $form->isSubmitted() ) {
        $em2 = $this->getDoctrine()->getManager();
        $em2->persist( $newWorkingTime );
        $em2->flush();
}

return $this->render(
        'AppBundle:WorkingTime:new.html.twig', [
              'form'     =>     $form->createView(),
        ]                                         ]
    );

class 工作时间类型:

namespace AppBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\DateIntervalType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints\Length;


/**
 * Class WorkingTimeType
 *
 * @package AppBundle\Form
 */
class WorkingTimeType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array                $options
     */
    public function buildForm( FormBuilderInterface $builder, array $options )
    {
        $builder
            ->add( 'durationTask', DateIntervalType::class, [
                                   'input'        => 'string',
                                   'widget'       => 'choice',
                                   // choice fields to display
                                   'with_years'   => false,
                                   'with_months'  => false,
                                   'with_days'    => false,
                                   'with_minutes' => true,
                                   'with_hours'   => true,
                                   'with_seconds' => false,
                               ]
            )
            ->add( 'save', SubmitType::class );
    }


    /**
     * @param OptionsResolver $resolver
     */
    public function configureOptions( OptionsResolver $resolver )
    {
        $resolver->setDefaults( [ 'data_class' => 'AppBundle\Entity\WorkingTime' ] );

    }


    /**
     * @return string
     */
    public function getBlockPrefix()
    {
        return 'success_form';
    }
}

在new.html.twig中:

{{ form(form) }}