【问题标题】:Symfony ChoiceType form map choices to a related entity fieldSymfony ChoiceType 表单将选项映射到相关实体字段
【发布时间】:2019-03-20 22:38:17
【问题描述】:

不确定是否可行,但我想将我的 ChoiceType 的两个选项映射为两个不同的 Announcement 字段。

这是我创建的EditAnnouncementType 中的 ChoiceType

->add('audience', ChoiceType::class,
        [
            'choices' =>
            [
                'Students: anybody with a student level field populated' => 'students',
                'Employees: anybody with an employee ID number' => 'employees'
            ],
                'expanded' => true,
                'required' => true,
                'multiple' => true
            ])

这是我希望表单为现有Announcement 实体自动更新的两个字段。

class Announcement
{
    /**
     * @param bool $employees
     *
     * @return $this
     */
    public function setEmployees(bool $employees)
    {
        $this->employees = $employees;
        return $this;
    }

    /**
     * @param bool $students
     *
     * @return $this
     */
    public function setStudents(bool $students)
    {
        $this->students = $students;
        return $this;
    }
}

我使用两个单独的 CheckBoxType 表单来代替,但是我需要要求用户选择 至少一个 选项,这不可能(据我所知)作为两个单独的表格。

【问题讨论】:

    标签: php symfony symfony-forms


    【解决方案1】:

    不确定我是否正确理解您的要求,所以让我重新表述一下。
    在一种情况下,您是否希望 audience 字段将 employees 设置为 falsestudents 设置为 true,在另一种情况下将 employees 设置为 truestudents 设置为 false

    如果我是正确的,那么这是一种可能的方法:

    class Announcement
    {
        public const AUDIENCE_EMPLOYEES = 'employees';
        public const AUDIENCE_STUDENTS = 'students';
    
        public function getAudience(): array
        {
            $audience = [];
    
            if($this->employees()) { 
                $audience[] = self::AUDIENCE_EMPLOYEES;
            }
            if($this->employees()) { 
                $audience[] = self::AUDIENCE_STUDENTS;
            }
    
            return $audience;
        }
    
        public function setAudience(array $audience): Announcement
        {
            $this->employees = in_array(self::AUDIENCE_EMPLOYEES, $audience);
            $this->students = in_array(self::AUDIENCE_STUDENTS, $audience);
    
            return $this;
        }
    }
    

    您的 EditAnnouncementType 类应该已经正确处理了这个问题。
    但是可以通过使用类常量来进一步改进

    ->add('audience', ChoiceType::class,
        [
            'choices' =>
            [
                'Students: anybody with a student level field populated' => Announcement::AUDIENCE_STUDENTS,
                'Employees: anybody with an employee ID number' => Announcement::AUDIENCE_EMPLOYEES,
            ],
            'expanded' => true,
            'required' => true,
            'multiple' => true,
        ]
    )
    

    【讨论】:

    • 从某种意义上说,是的,这就是我想要做的。理想情况下,如果可能的话,我不需要受众领域。此外,用户需要能够同时选择这两个选项
    • 如果您希望表单字段表示某些内容,您的表单类型将映射到实体(或 DTO,但无论如何),那么,是的,您需要为此填写一个属性。我的错,我看的不够仔细'multiple' => true,
    • 编辑后的代码应该做的,为了有一个多选的可能性
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多