Code Duplication    Length = 121-124 lines in 2 locations

src/Kunstmaan/FormBundle/Entity/PageParts/FileUploadPagePart.php 1 location

@@ 20-143 (lines=124) @@
17
 * @ORM\HasLifecycleCallbacks
18
 * @ORM\Table(name="kuma_file_upload_page_parts")
19
 */
20
class FileUploadPagePart extends AbstractFormPagePart
21
{
22
    /**
23
     * If set to true, you are obligated to fill in this page part
24
     *
25
     * @ORM\Column(type="boolean", nullable=true)
26
     */
27
    protected $required = false;
28
29
    /**
30
     * Error message shows when the page part is required and nothing is filled in
31
     *
32
     * @ORM\Column(type="string", name="error_message_required", nullable=true)
33
     */
34
    protected $errorMessageRequired;
35
36
    /**
37
     * Modify the form with the fields of the current page part
38
     *
39
     * @param FormBuilderInterface $formBuilder The form builder
40
     * @param ArrayObject          $fields      The fields
41
     * @param int                  $sequence    The sequence of the form field
42
     */
43
    public function adaptForm(FormBuilderInterface $formBuilder, ArrayObject $fields, $sequence)
44
    {
45
        $ffsf = new FileFormSubmissionField();
46
        $ffsf->setFieldName('field_'.$this->getUniqueId());
47
        $ffsf->setLabel($this->getLabel());
48
        $ffsf->setSequence($sequence);
49
50
        $data = $formBuilder->getData();
51
        $data['formwidget_'.$this->getUniqueId()] = $ffsf;
52
53
        $constraints = [];
54
        if ($this->getRequired()) {
55
            $options = [];
56
            if (!empty($this->errorMessageRequired)) {
57
                $options['message'] = $this->errorMessageRequired;
58
            }
59
            $constraints[] = new NotBlank($options);
60
        }
61
62
        $formBuilder->add(
63
            'formwidget_'.$this->getUniqueId(),
64
            FileFormSubmissionType::class,
65
            [
66
                'label' => $this->getLabel(),
67
                'value_constraints' => $constraints,
68
                'required' => $this->getRequired(),
69
            ]
70
        );
71
        $formBuilder->setData($data);
72
73
        $fields->append($ffsf);
74
    }
75
76
    /**
77
     * Sets the required valud of this page part
78
     *
79
     * @param bool $required
80
     *
81
     * @return FileUploadPagePart
82
     */
83
    public function setRequired($required)
84
    {
85
        $this->required = $required;
86
87
        return $this;
88
    }
89
90
    /**
91
     * Check if the page part is required
92
     *
93
     * @return bool
94
     */
95
    public function getRequired()
96
    {
97
        return $this->required;
98
    }
99
100
    /**
101
     * Sets the message shown when the page part is required and no value was entered
102
     *
103
     * @param string $errorMessageRequired
104
     *
105
     * @return FileUploadPagePart
106
     */
107
    public function setErrorMessageRequired($errorMessageRequired)
108
    {
109
        $this->errorMessageRequired = $errorMessageRequired;
110
111
        return $this;
112
    }
113
114
    /**
115
     * Get the error message that will be shown when the page part is required and no value was entered
116
     *
117
     * @return string
118
     */
119
    public function getErrorMessageRequired()
120
    {
121
        return $this->errorMessageRequired;
122
    }
123
124
    /**
125
     * Returns the view used in the frontend
126
     *
127
     * @return mixed
128
     */
129
    public function getDefaultView()
130
    {
131
        return '@KunstmaanForm/FileUploadPagePart/view.html.twig';
132
    }
133
134
    /**
135
     * Returns the default backend form type for this page part
136
     *
137
     * @return string
138
     */
139
    public function getDefaultAdminType()
140
    {
141
        return FileUploadPagePartAdminType::class;
142
    }
143
}
144

src/Kunstmaan/FormBundle/Entity/PageParts/CheckboxPagePart.php 1 location

@@ 19-139 (lines=121) @@
16
 * @ORM\Entity
17
 * @ORM\Table(name="kuma_checkbox_page_parts")
18
 */
19
class CheckboxPagePart extends AbstractFormPagePart
20
{
21
    /**
22
     * If set to true, you are obligated to fill in this page part
23
     *
24
     * @ORM\Column(type="boolean", nullable=true)
25
     */
26
    protected $required = false;
27
28
    /**
29
     * Error message shows when the page part is required and nothing is filled in
30
     *
31
     * @ORM\Column(type="string", name="error_message_required", nullable=true)
32
     */
33
    protected $errorMessageRequired;
34
35
    /**
36
     * Sets the required valud of this page part
37
     *
38
     * @param bool $required
39
     *
40
     * @return CheckboxPagePart
41
     */
42
    public function setRequired($required)
43
    {
44
        $this->required = $required;
45
46
        return $this;
47
    }
48
49
    /**
50
     * Check if the page part is required
51
     *
52
     * @return bool
53
     */
54
    public function getRequired()
55
    {
56
        return $this->required;
57
    }
58
59
    /**
60
     * Sets the message shown when the page part is required and no value was entered
61
     *
62
     * @param string $errorMessageRequired
63
     *
64
     * @return CheckboxPagePart
65
     */
66
    public function setErrorMessageRequired($errorMessageRequired)
67
    {
68
        $this->errorMessageRequired = $errorMessageRequired;
69
70
        return $this;
71
    }
72
73
    /**
74
     * Get the error message that will be shown when the page part is required and no value was entered
75
     *
76
     * @return string
77
     */
78
    public function getErrorMessageRequired()
79
    {
80
        return $this->errorMessageRequired;
81
    }
82
83
    /**
84
     * Returns the frontend view
85
     *
86
     * @return string
87
     */
88
    public function getDefaultView()
89
    {
90
        return '@KunstmaanForm/CheckboxPagePart/view.html.twig';
91
    }
92
93
    /**
94
     * Modify the form with the fields of the current page part
95
     *
96
     * @param FormBuilderInterface $formBuilder The form builder
97
     * @param ArrayObject          $fields      The fields
98
     * @param int                  $sequence    The sequence of the form field
99
     */
100
    public function adaptForm(FormBuilderInterface $formBuilder, ArrayObject $fields, $sequence)
101
    {
102
        $bfsf = new BooleanFormSubmissionField();
103
        $bfsf->setFieldName('field_'.$this->getUniqueId());
104
        $bfsf->setLabel($this->getLabel());
105
        $bfsf->setSequence($sequence);
106
107
        $data = $formBuilder->getData();
108
        $data['formwidget_'.$this->getUniqueId()] = $bfsf;
109
        $constraints = [];
110
        if ($this->getRequired()) {
111
            $options = [];
112
            if (!empty($this->errorMessageRequired)) {
113
                $options['message'] = $this->errorMessageRequired;
114
            }
115
            $constraints[] = new NotBlank($options);
116
        }
117
        $formBuilder->add(
118
            'formwidget_'.$this->getUniqueId(),
119
            BooleanFormSubmissionType::class,
120
            [
121
                'label' => $this->getLabel(),
122
                'value_constraints' => $constraints,
123
                'required' => $this->getRequired(),
124
            ]
125
        );
126
        $formBuilder->setData($data);
127
128
        $fields->append($bfsf);
129
    }
130
131
    /**
132
     * Returns the default backend form type for this page part
133
     *
134
     * @return string
135
     */
136
    public function getDefaultAdminType()
137
    {
138
        return CheckboxPagePartAdminType::class;
139
    }
140
}
141