gpt4 book ai didi

mysql - Symfony2 验证约束

转载 作者:行者123 更新时间:2023-11-28 23:33:11 25 4
gpt4 key购买 nike

我正在处理一个登录和注册表单,在主页上我需要为用户提供两个单选按钮选项以选择两个事件(存储在变量 eT1 和 eT2 中)。但是我需要确保任何特定事件的最大限制都没有满,即。如果用户选择了一个最大限制已满的事件,则需要显示一条错误消息。* 我需要 Controller 内部的验证逻辑。* 对于登录和注册,我使用 FOSUserBundle

这是 Controller 代码

<?php

namespace AppBundle\Controller;

use AppBundle\Entity\events;
//use AppBundle\Entity\eventtype;
use AppBundle\Entity\users;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;

class DefaultController extends Controller {

/**
* @Route("/home", name="homepage")
*/
public function indexAction(Request $request) {
$events = new events();

$form = $this->createFormBuilder($events)
->add('eT1', ChoiceType::class, array(
'choices' => array(
'Poker' => 1,
'Chess' => 2,
'Cricket' => 3,
'Marbles' => 4,
'Football' => 5,
),
'choices_as_values' => true,
'expanded' => true,
'multiple' => false,
'label' => 'Choose After Breakfast Event',
))
->add('eT2', ChoiceType::class, array(
'choices' => array(
'Poker' => 1,
'Chess' => 2,
'Cricket' => 3,
'Marbles' => 4,
'Football' => 5,
),
'choices_as_values' => true,
'expanded' => true,
'multiple' => false,
'label' => 'Choose After Snacks Event',
))
->add('save', SubmitType::class, array('label' => 'Submit'))
->getForm();

if ($request->isMethod('POST')) {
$form->submit($request);


if ($form->isValid()) {
// perform some action, eg. persisting the data to database...
$user = $this->container->get('security.context')->getToken()->getUser();
$events->setuser($user);
// var_dump($id);
// exit;
//$events->setuserID($id);


$em = $this->getDoctrine()->getManager();

// tells Doctrine you want to (eventually) save the Product (no queries yet)
$em->persist($events);

// actually executes the queries (i.e. the INSERT query)
$em->flush();
return $this->redirectToRoute('homepage');
}
}

return $this->render('default/index.html.twig', array(
'form' => $form->createView(),
));
}
}

下面是用户实体

<?php

namespace AppBundle\Entity;

use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\Common\Collections\ArrayCollection;

/**
* users
*
* @ORM\Table(name="users")
* @ORM\Entity(repositoryClass="AppBundle\Repository\usersRepository")
*/
class users extends BaseUser
{
/**
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;

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

/**
* @ORM\OneToMany(targetEntity="events", mappedBy="users")
*/
protected $multiEvents;


public function __construct()
{
parent::__construct();
// your own logic
$this->multiEvents = new ArrayCollection();
//$this->id = $id;
//$this->name = $name;
}

}

下面是事件实体

<?php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
* events
*
* @ORM\Table(name="events")
* @ORM\Entity(repositoryClass="AppBundle\Repository\eventsRepository")
*/
class events {

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

/**
* @var int
*
* @ORM\Column(name="user_id", type="integer")
*/
protected $user_id;

/**
* @var int
*
* @ORM\Column(name="ET1", type="integer")
*/
protected $eT1;

/**
* @var int
*
* @ORM\Column(name="ET2", type="integer")
*/
protected $eT2;

/**
* @ORM\ManyToOne(targetEntity="users", inversedBy="multievents")
* @ORM\JoinColumn(name="user_id", referencedColumnName="id")
*/
private $singleUser;

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

/**
* Get user_id
*
* @return integer
*/
public function getuser_id() {
return $this->user_id;
}

/**
* Set user
*
* @param integer $user
* @return events
*/
public function setUser($user) {
$this->singleUser = $user;
}


/**
* Set eT1
*
* @param integer $eT1
* @return events
*/
public function setET1($eT1) {
$this->eT1 = $eT1;

return $this;
}

/**
* Get eT1
*
* @return integer
*/
public function getET1() {
return $this->eT1;
}

/**
* Set eT2
*
* @param integer $eT2
* @return events
*/
public function setET2($eT2) {
$this->eT2 = $eT2;

return $this;
}

/**
* Get eT2
*
* @return integer
*/
public function getET2() {
return $this->eT2;
}
}

下面是事件类型实体

<?php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
* eventtype
*
* @ORM\Table(name="eventtype")
* @ORM\Entity(repositoryClass="AppBundle\Repository\eventtypeRepository")
*/
class eventtype
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;

/**
* @var string
*
* @ORM\Column(name="EventName", type="string", length=255)
*/
protected $eventName;


/**
* @var int
*
* @ORM\Column(name="MaxLimit", type="integer")
*/
protected $maxLimit;


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

/**
* Set eventName
*
* @param string $eventName
* @return eventtype
*/
public function setEventName($eventName)
{
$this->eventName = $eventName;

return $this;
}

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


/**
* Set maxLimit
*
* @param integer $maxLimit
* @return eventtype
*/
public function setMaxLimit($maxLimit)
{
$this->maxLimit = $maxLimit;

return $this;
}

/**
* Get maxLimit
*
* @return integer
*/
public function getMaxLimit()
{
return $this->maxLimit;
}
}

事件类型表已经填充了仅代表 5 种事件类型的五行。扑克 (id=1) 的最大限制是 12,国际象棋 (id=2) 的最大限制是 4,其余三个即。 cricket(id=3), marbles(id=4) & football(id=5) 是 10。

最佳答案

我不知道你为什么“需要 Controller 内部的验证逻辑”......避免胖 Controller 是一个很好的做法。

我的解决方案不能完全满足您的需求,但我还是建议:我认为您可以创建一个 custom validation constraint (有了依赖项,你就可以注入(inject)实体管理器,你可以检查事件是否已满)

然后在每个字段的注释中简单地添加您的约束:$eT1$eT2

关于mysql - Symfony2 验证约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36721225/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com