Completed
Pull Request — 5.0 (#2163)
by Kevin
13:16
created

FormBundle/Entity/PageParts/CheckboxPagePart.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Kunstmaan\FormBundle\Entity\PageParts;
4
5
use ArrayObject;
6
use Doctrine\ORM\Mapping as ORM;
7
use Kunstmaan\FormBundle\Entity\FormSubmissionFieldTypes\BooleanFormSubmissionField;
8
use Kunstmaan\FormBundle\Form\BooleanFormSubmissionType;
9
use Kunstmaan\FormBundle\Form\CheckboxPagePartAdminType;
10
use Kunstmaan\FormBundle\Form\SingleLineTextPagePartAdminType;
11
use Symfony\Component\Form\FormBuilderInterface;
12
use Symfony\Component\Validator\Constraints\NotBlank;
13
14
/**
15
 * The checkbox page part can be used to create forms with checkbox input fields
16
 *
17
 * @ORM\Entity
18
 * @ORM\Table(name="kuma_checkbox_page_parts")
19
 */
20 View Code Duplication
class CheckboxPagePart extends AbstractFormPagePart
0 ignored issues
show
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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
     * Sets the required valud of this page part
38
     *
39
     * @param bool $required
40
     *
41
     * @return CheckboxPagePart
42
     */
43
    public function setRequired($required)
44
    {
45
        $this->required = $required;
46
47
        return $this;
48
    }
49
50
    /**
51
     * Check if the page part is required
52
     *
53
     * @return bool
54
     */
55
    public function getRequired()
56
    {
57
        return $this->required;
58
    }
59
60
    /**
61
     * Sets the message shown when the page part is required and no value was entered
62
     *
63
     * @param string $errorMessageRequired
64
     *
65
     * @return CheckboxPagePart
66
     */
67
    public function setErrorMessageRequired($errorMessageRequired)
68
    {
69
        $this->errorMessageRequired = $errorMessageRequired;
70
71
        return $this;
72
    }
73
74
    /**
75
     * Get the error message that will be shown when the page part is required and no value was entered
76
     *
77
     * @return string
78
     */
79
    public function getErrorMessageRequired()
80
    {
81
        return $this->errorMessageRequired;
82
    }
83
84
    /**
85
     * Returns the frontend view
86
     *
87
     * @return string
88
     */
89
    public function getDefaultView()
90
    {
91
        return "KunstmaanFormBundle:CheckboxPagePart:view.html.twig";
92
    }
93
94
    /**
95
     * Modify the form with the fields of the current page part
96
     *
97
     * @param FormBuilderInterface $formBuilder The form builder
98
     * @param ArrayObject          $fields      The fields
99
     * @param int                  $sequence    The sequence of the form field
100
     */
101
    public function adaptForm(FormBuilderInterface $formBuilder, ArrayObject $fields, $sequence)
102
    {
103
        $bfsf = new BooleanFormSubmissionField();
104
        $bfsf->setFieldName('field_' . $this->getUniqueId());
105
        $bfsf->setLabel($this->getLabel());
106
        $bfsf->setSequence($sequence);
107
108
        $data = $formBuilder->getData();
109
        $data['formwidget_' . $this->getUniqueId()] = $bfsf;
110
        $constraints = array();
111
        if ($this->getRequired()) {
112
            $options = array();
113
            if (!empty($this->errorMessageRequired)) {
114
                $options['message'] = $this->errorMessageRequired;
115
            }
116
            $constraints[] = new NotBlank($options);
117
        }
118
        $formBuilder->add('formwidget_' . $this->getUniqueId(),
119
            BooleanFormSubmissionType::class,
120
            array(
121
                'label'       => $this->getLabel(),
122
                '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