您好,我想看看(在“ from”被加法之后)如何验证'choices' => $question->buildAnswerWithValidKey()
之一为真。
这是问题数组。
Array
(
[Sonne] => 1
[Mond] =>
[Und Sterne] =>
)
我想在表格被提出后对此进行验证。
这是我的'choices'
功能。根据具有正确密钥的问题(在这种情况下为1或0,是/否),问题将得到答案
public function buildAnswerWithValidKey()
{
$answers = [];
$valid = [];
$answersWithValidKey = [];
/** @var Answer $answer */
foreach ($this->getAnswers() as $answer) {
$answers[] = $answer->getAnswer();
$valid[] = $answer->getValid();
}
//Sets answers as item and valid as key as required by 'choices'
$answersWithValidKey[] = array_combine($answers, $valid);
return $answersWithValidKey;
}
这是我的控制器。 我在努力验证单选按钮的位置。
/** @var Question $question */
$question = $this->questionRepository->findById(12)[0];
$options = ['question' => $question];
$form = $this->createForm(ExamType::class, null, $options);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
}
return [
'form' => $form->createView(),
];
这是我其他可能有用的课程。
Answer.php-映射ManyToOne的位置。
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass="App\Repository\AnswerRepository")
*/
class Answer
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string")
*/
private $answer;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Question", inversedBy="answers")
* @ORM\JoinColumn(name="question_id", referencedColumnName="id")
*/
private $question;
/**
* @ORM\Column(type="boolean")
*/
private $valid;
public function getId(): ?int
{
return $this->id;
}
public function getAnswer(): ?string
{
return $this->answer;
}
public function setAnswer(string $answer): self
{
$this->answer = $answer;
return $this;
}
public function getQuestion(): ?Question
{
return $this->question;
}
public function setQuestion(?Question $question): self
{
$this->question = $question;
return $this;
}
public function getValid(): ?bool
{
return $this->valid;
}
public function setValid(bool $valid): self
{
$this->valid = $valid;
return $this;
}
}
还有我的Question.php-我在做buildAnswerWithValidKey()
函数的地方。
<?php
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass="App\Repository\QuestionRepository")
*/
class Question
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $question;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Answer", mappedBy="question")
*/
private $answers;
public function __construct()
{
$this->answers = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getQuestion(): ?string
{
return $this->question;
}
public function setQuestion(string $question): self
{
$this->question = $question;
return $this;
}
/**
* @return Collection|Answer[]
*/
public function getAnswers(): Collection
{
return $this->answers;
}
/**
* Sets the Answers
*
* @param mixed $answers
* @return void
*/
public function setAnswers($answers)
{
$this->answers = $answers;
}
public function addAnswer(Answer $answer): self
{
if (!$this->answers->contains($answer)) {
$this->answers[] = $answer;
$answer->setQuestion($this);
}
return $this;
}
public function removeAnswer(Answer $answer): self
{
if ($this->answers->contains($answer)) {
$this->answers->removeElement($answer);
// set the owning side to null (unless already changed)
if ($answer->getQuestion() === $this) {
$answer->setQuestion(null);
}
}
return $this;
}
public function buildAnswerWithValidKey()
{
$answers = [];
$valid = [];
$answersWithValidKey = [];
/** @var Answer $answer */
foreach ($this->getAnswers() as $answer) {
$answers[] = $answer->getAnswer();
$valid[] = $answer->getValid();
}
//Sets answers as item and valid as key as required by 'choices'
$answersWithValidKey[] = array_combine($answers, $valid);
return $answersWithValidKey;
}
}
编辑:
这是我的考试类型
<?php
namespace App\Form;
use App\Entity\Question;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class ExamType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
/**
* @var Question $question
*/
$question = $options['question'];
$builder
->add(
//Used to check in the validation
'question',
HiddenType::class,
[
'data' => $question->getId()
]
)
->add(
'Answers',
ChoiceType::class,
[
'choices' => $question->buildAnswerWithValidKey(),
'expanded' => true,
'label' => $question->getQuestion()
]
)
->add(
'Evaluate',
SubmitType::class
);
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => Question::class,
]);
$resolver->setRequired('question');
$resolver->setAllowedTypes('question', Question::class);
}
}
基本上,如果用户选择了正确的答案,我只是想返回某种消息。
答案 0 :(得分:1)
通常,要从表单中检索数据,您必须在$form->getData()
处理完请求并检查其是否有效后调用它。
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$answerData = $form->getData();
if (!empty($answerData['Answers'] )) {
// do something useful
}
}
但是,表单将根据其buildForm()方法返回数据内容,并且数据对象将尝试将其放入其中,该数据对象由data_class
中的FormType
选项定义。如果要在数组中返回结果,则data_class
应该是null
。 (因此也必须设置此值,Alternative是一个对象,以某种方式对返回值进行编码)