简体   繁体   English

Symfony ChoiceType 表单将选择映射到相关实体字段

[英]Symfony ChoiceType form map choices to a related entity field

Not sure if it is possible, but I want to map the two choices of my ChoiceType form two one different Announcement field each.不确定是否可能,但我想将我的 ChoiceType 的两个选项分别映射到两个不同的Announcement字段。

Here is the ChoiceType in a EditAnnouncementType I made这是我制作的 EditAnnouncementType 中的EditAnnouncementType

->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
            ])

Here are the two fields I want the form to automatically update for the existing Announcement entity.这是我希望表单为现有的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;
    }
}

I have this working with two separate CheckBoxType forms instead, however I need to require the user to select at least one of the options, which isn't possible (to my knowledge) as two separate forms.我使用两个单独的 CheckBoxType 表单来代替,但是我需要要求用户至少选择一个选项,这是不可能的(据我所知)作为两个单独的表单。

Not sure if I properly understand what you are asking so let me rephrase it.不确定我是否正确理解您的要求,所以让我重新表述一下。
You would want your audience field to set employees to false and students to true in one case, and employees to true and students to false in the other case?您是否希望您的audience字段在一种情况下将employees设置为false ,将studentstrue在另一种情况下将employeestrue ,将studentsfalse

If I am correct then here is a possible way to go:如果我是对的,那么这里有一个可能的方法:

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;
    }
}

Your EditAnnouncementType class should already handle this correctly.您的EditAnnouncementType类应该已经正确处理了这个问题。
But can be further improved by the usage of class constants但是可以通过使用类常量来进一步改进

->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,
    ]
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM