在Symfony中,选择开始日期和结束日期为两周

在Symfony中,选择开始日期和结束日期为两周,symfony,doctrine-orm,Symfony,Doctrine Orm,有人能帮我吗。我在创建查询或如何向createAction添加更改以实现以下目标时遇到问题。单击“创建”时,它将检查工资周期是否有效,因为在“工资周”表中,用户输入两周周期时,它将填充一周 工资周期:工资周期ID、开始日期、结束日期和状态 工资周:id、开始日期、结束日期、天数、正常工作时间 例如,用户在工资周期中输入起始日期:2017年7月16日结束日期:2017年7月29日,然后在工资周表中输入第1周期起始日期:2017年7月16日结束日期:2017年7月22日第2周期起始日期:2017年7

有人能帮我吗。我在创建查询或如何向createAction添加更改以实现以下目标时遇到问题。单击“创建”时,它将检查工资周期是否有效,因为在“工资周”表中,用户输入两周周期时,它将填充一周

工资周期:工资周期ID、开始日期、结束日期和状态 工资周:id、开始日期、结束日期、天数、正常工作时间

例如,用户在工资周期中输入起始日期:2017年7月16日结束日期:2017年7月29日,然后在工资周表中输入第1周期起始日期:2017年7月16日结束日期:2017年7月22日第2周期起始日期:2017年7月23日结束日期:2017年7月29日


因此,该期间将被视为有效的else错误,并且在创建期间时,一旦该期间有效,将检查它是否存在于工资单期间表else error中。但我不确定如何添加确保用户进入2周周期的部分。>7天我想你可以用以下方法解决这个问题

//create new trait
<?php

namespace yourBundlePath\Form\Type\Utility;

use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 *
 * @package Daim\CoreBundle\Form\Type\Utility
 */
trait ContainerTrait
{
    /**
     * @var ContainerInterface
     */
    private $containerObject;

    /**
     * @param ContainerInterface $container
     * @return ContainerInterface
     */
    public function setContainer(ContainerInterface $container)
    {
        return $this->containerObject = $container;
    }

    /**
     * @return ContainerInterface
     */
    public function getContainer()
    {        
        return $this->containerObject;
    }
}

//form
use yourBundlePath\Form\Type\Utility\ContainerTrait;
class yourFormClass
{
   //call after the class declaration
    use ContainerTrait;

 $builder->addEventListener(
        FormEvents::SUBMIT,
        function (FormEvent $event) {
           $form = $event->getForm();
           $em = $this->getContainer()->get('doctrine');
           $startDate = $form->get('startDate')->getData();
           $endDate = $form->get('endDate')->getData();
           $entity = $em->getRepository('comtwclagripayrollBundle:Payrollperiod')->findByPayrollPeriod(array('startDate'=>$startDate,'endDate'=> $endDate));
           if ($entity){
            $form->get('startDate')->addError(
                    new FormError(
                     'ERROR!  Payroll Period exist'
                    )
                );
           }
         }
    );
//创建新特征

人们可以用各种方法来解决这个问题。如果这是一个常见问题(和/或您更喜欢全局解决方案),请使用。如果您不介意“本地”解决方案,请查看

这两种情况都在文档页面中进行了说明。另一个参考是

剩下的就是如何计算日期之间的差异,我建议使用PHP,如下所示:

$days = $startDate->diff($endDate)->days;
if ($days <= 7 && $days > 14) {
    // build my constraint error message because the period is invalid.
}

谢谢,但我似乎在getContainer中遇到了一个错误。另外,我如何进行自验证以确保用户进入两周的周期。嘿,siju,你能告诉我可以做些什么来缓解getContainer错误吗?第一次使用add event listener,所以我不太确定,它正在我的项目中工作。我不知道你的项目缺少什么。我会尽快检查我的项目,并会通知你。请检查您的两个日期是否有14天,以确保用户输入两周的时间段OK谢谢。我在这一点上迷路了,因为不是每个周期都有整整两周。我如何确保在哪里检查它只是在get container上给了我一个错误,不确定是否丢失了某些内容。这将创建“自定义”表单验证。因此,表单不再有效,具体位置取决于您选择的方法。尝试“阅读方法”在提供的页面上有大量的代码示例,它们实际上教会了您它的用途和去向。。当您遇到实际问题时返回,显示您尝试了什么,什么不起作用,等等。。(指南…)我不熟悉使用symfony,这就是为什么我问,不确定是否要进行自定义验证。我尝试了这个@Assert\Range*(*min=“today”,max=“+2weeks”*),但它一直说日期无效。我链接的页面对此进行了解释。范围验证器仅适用于单个字段(可以在多个字段上使用,以便在2周内进行测试,例如从现在起或其他情况,但不能与另一个字段进行比较)。当你想比较一个字段和另一个字段时,你需要一些其他的东西。文档并没有告诉我多少关于确保用户使用回调或类约束输入14天的信息。或者我现在不明白。是否有回调或类约束来检查周期是否存在?否。“我不确定如何添加确保用户进入2周周期的部分。”7天
//create new trait
<?php

namespace yourBundlePath\Form\Type\Utility;

use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 *
 * @package Daim\CoreBundle\Form\Type\Utility
 */
trait ContainerTrait
{
    /**
     * @var ContainerInterface
     */
    private $containerObject;

    /**
     * @param ContainerInterface $container
     * @return ContainerInterface
     */
    public function setContainer(ContainerInterface $container)
    {
        return $this->containerObject = $container;
    }

    /**
     * @return ContainerInterface
     */
    public function getContainer()
    {        
        return $this->containerObject;
    }
}

//form
use yourBundlePath\Form\Type\Utility\ContainerTrait;
class yourFormClass
{
   //call after the class declaration
    use ContainerTrait;

 $builder->addEventListener(
        FormEvents::SUBMIT,
        function (FormEvent $event) {
           $form = $event->getForm();
           $em = $this->getContainer()->get('doctrine');
           $startDate = $form->get('startDate')->getData();
           $endDate = $form->get('endDate')->getData();
           $entity = $em->getRepository('comtwclagripayrollBundle:Payrollperiod')->findByPayrollPeriod(array('startDate'=>$startDate,'endDate'=> $endDate));
           if ($entity){
            $form->get('startDate')->addError(
                    new FormError(
                     'ERROR!  Payroll Period exist'
                    )
                );
           }
         }
    );
$days = $startDate->diff($endDate)->days;
if ($days <= 7 && $days > 14) {
    // build my constraint error message because the period is invalid.
}
class Payrollperiod {
    ...

    /**
     * @Assert\Callback
     */
    public function validatePayrollPeriod(ExecutionContextInterface $context) {
        $days = $this->startdate->diff($this->enddate)->days;
        if ($days <= 7 && $days > 14) {
            $context->buildViolation('There have to be at least 7 and a maximum of 13 days for your payroll period.')
                ->atPath('enddate') // this is where the message is bound to, can be either start or end date depending on where you prefer.
                ->addViolation();
        }
    }
}
...

if ($form->isValid()) {
    $em = $this->getDoctrine()->getManager();
    $entityExists = $em->getRepository('comtwclagripayrollBundle:Payrollperiod')
        ->findByPayrollPeriod($entity->getStartdate(), $entity->getEnddate());

    // If the entity does not exist we can add it and redirect to the new page.
    if (!$entityExists) {

        // Add our form entity to the database.
        $em->persist($entity);
        $em->flush();
        
        // redirectToRoute is a controller helper function and is a tiny bit shorter.
        return $this->redirectToRoute('payrollperiod_show', array(
            'payrollperiodid' => $entity->getPayrollperiodid()
        ));
    }

    // Form is valid but we didn't return anything so far.
    // So there is an entity with the same period start or end.
    // Add a flash and show the form again.
    $this->addFlash('error', 'A payroll period is already present
                              with the same start or end date.');
}

return ...