【发布时间】:2021-06-21 01:00:57
【问题描述】:
从 Symfony 2.8 更新到 3.0 并验证运行后,发现由于验证错误,作者无法保存。
可能是由于规范更改颠倒了选择类型键和值。
我尝试将 choice_label 放在下面的参考中,但没有成功。
我也试过array_flip$staffs,但是显示变了,无法保存。
还有其他方法吗?
Symfony ChoiceType $choices - labels and values swapped
错误
This value is not valid.
Unable to reverse value for property path "author":
The choice "9" does not exist or is not unique.
StaffChoicelist 之前
use Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceList;
use Symfony\Component\Form\Extension\Core\ChoiceList\LazyChoiceList;
class StaffChoiceList extends LazyChoiceList
{
public function __construct($staffService, $loginStaff)
{
$this->staffService = $staffService;
$this->loginStaff = $loginStaff;
}
public function setCurrentStaff($currentStaff)
{
$this->currentStaff = $currentStaff;
}
public function loadChoiceList($value = null)
{
// Get the same shop staff as the login staff
$staffs = $this->staffService->getStaffByShop($this->loginStaff->getShop());
If the current staff is not included in the acquired staff (due to transfer etc.), add it to the end
if ($this->currentStaff && !array_search($this->currentStaff, $staffs)) {
$staffs[] = $this->currentStaff;
}
return new ChoiceList($staffs, $staffs);
}
}
StaffChoiceloader 之后
use Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface;
use Symfony\Component\Form\Choicelist\ArrayChoiceList;
class StaffChoiceLoader implements ChoiceLoaderInterface
{
public function loadChoiceList($value = null)
{
$staffs = $this->staffService->getStaffByShop($this->loginStaff->getShop());
if ($this->currentStaff && !array_search($this->currentStaff, $staffs)) {
$staffs[] = $this->currentStaff;
}
return new arrayChoiceList($staffs, null);
}
public function loadChoicesForValues(array $values, $value = null)
{
// Optimized when no data is preset
if (empty($choices))
{
return array();
}
$values = array();
foreach ($choices as $choice)
{
$values[] = (string) $this->loginStaff->getId();
}
return $values;
}
public function loadValuesForChoices(array $choices, $value = null)
{
// Optimized when nothing is sent
if (empty($values))
{
return array();
}
// Get the entity from the ID and return the required data
return $this->staffService->getStaffByShop($this->loginStaff->getShop());
}
}
类型
$authorChoiceList = new StaffChoiceLoader($this->staffService, $options['login_staff']);
$builder->add("author", EntityType::class, array(
"required" => true,
"class" => "AppBundle:Staff",
"choice_loader" => $authorChoiceList,
"choice_label" =>function ($value) {
return $value;
},
));
$builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) use ($authorChoiceList) {
$article = $event->getData();
$authorChoiceList->setCurrentStaff($article->getAuthor());
});
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
"validation_groups" => function (FormInterface $form) {
$article = $form->getData();
return $this->getValidationGroups($article->getArticleStatus());
},
));
}
员工.php
/**
* __toString
*
* @return string
*/
public function __toString()
{
return $this->staffName;
}
/**
* Set staffName
*
* @param string $staffName
* @return Staff
*/
public function setStaffName($staffName)
{
$this->staffName = $staffName;
return $this;
}
/**
* Get staffName
*
* @return string
*/
public function getStaffName()
{
return $this->staffName;
}
文章.php
/**
* @ORM\ManyToOne(targetEntity="Staff")
* @ORM\JoinColumn(name="author_id", referencedColumnName="id", nullable=true)
*/
protected $author;
/**
* Set author
*
* @param \AppBundle\Model\Entity\Staff $author
* @return Article
*/
public function setAuthor(\AppBundle\Model\Entity\Staff $author = null)
{
$this->author = $author;
return $this;
}
/**
* Get author
*
* @return \AppBundle\Model\Entity\Staff
*/
public function getAuthor()
{
return $this->author;
}
尝试过的代码 类型
public function __construct($staffService, array $options = [])
{
$this->staffService = $staffService;
$this->loginStaff = $options['login_staff'];
$this->currentStaff = $options['login_staff'];
}
错误
Notice: Undefined index: login_staff
【问题讨论】: