本文介绍了如何限制可以以symfony形式输入的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了代码来创建输入表单,我可以编辑某个数据库列中的值,但我想对此值进行限制,例如我有一行我命名为credit",而另一列我将其命名为less"比信用"所以当我编辑我的行小于信用"所以行小于信用"将只接受输入如果值小于信用值如果我在信用行值上有 5 个硬币我可以输入如果价值 5 个硬币或少于 5 个硬币,则使用少于信用"输入表单,那么我该怎么做我的完整代码

I created code to create input form that i can edit a value in a certain database column put i want to but a limit to this value for example i have a row i named "credit" and other column I named it "less than credit" so when i edit my row "less than credit" so the row "less than credit" will only accept input on it if the value less than the credit value if i have 5 coins on the credit row value i can input on the "less than credit" input form if the value 5 coins or less than 5 coins so how I do that my full code

<?php

namespace site\blogBundle\Controller;

use AppBundle\Entity\User;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\DateType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use site\blogBundle\Form\TaskType;
class DefaultController extends Controller
{
    public function indexAction(Request $request)
    {
      //$task = new User();
      $user = $this->container->get('security.context')->getToken()->getUser();

      $investor = $this->getDoctrine()->getRepository('AppBundle:User')->findOneBy(array('id' => $user->getId()));
      $less_than_credit = $investor->getlessthancredit();
      $form = $this->createForm(new TaskType(), $investor);

      $form->handleRequest($request);

      if ($form->isSubmitted() && $form->isValid()) {
           if(!empty($form->get('less_than_credit')->getData())){
               $investor->setlessthancredit($form->get('less_than_credit')->getData());
           }
           else{
               $investor->setlessthancredit($less_than_credit);
           }

           $em = $this->getDoctrine()->getManager();
           $em->persist($investor);
           $em->flush();

           $session = $this->getRequest()->getSession();


           /**
            * @Route("/siteblog_homepage/")
            */
          return $this->redirectToRoute('siteblog_homepage');
         }

      return $this->render('siteblogBundle:Default:index.html.twig', array(
          'form' => $form->createView(),
      ));       

    }
}

推荐答案

您可以使用 表达语言:

/**
 * @Assert\Expression(
 *     "this.credit >= value",
 *     message="lessthancredit should be less or equal to credit"
 * )
 */
private $lessthancredit;

这篇关于如何限制可以以symfony形式输入的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 01:29