Completed
Push — master ( e62fa2...4391c3 )
by Kristof
133:20 queued 117:59
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 Symfony\Component\Form\FormBuilderInterface;
11
use Symfony\Component\Validator\Constraints\NotBlank;
12
13
/**
14
 * The checkbox page part can be used to create forms with checkbox input fields
15
 *
16
 * @ORM\Entity
17
 * @ORM\Table(name="kuma_checkbox_page_parts")
18
 */
19 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...
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 'KunstmaanFormBundle: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 = array();
110
        if ($this->getRequired()) {
111
            $options = array();
112
            if (!empty($this->errorMessageRequired)) {
113
                $options['message'] = $this->errorMessageRequired;
114
            }
115
            $constraints[] = new NotBlank($options);
116
        }
117
        $formBuilder->add('formwidget_' . $this->getUniqueId(),
118
            BooleanFormSubmissionType::class,
119
            array(
120
                'label' => $this->getLabel(),
121
                'constraints' => $constraints,
122
                'required' => $this->getRequired(),
123
            )
124
        );
125
        $formBuilder->setData($data);
126
127
        $fields->append($bfsf);
128
    }
129
130
    /**
131
     * Returns the default backend form type for this page part
132
     *
133
     * @return string
134
     */
135
    public function getDefaultAdminType()
136
    {
137
        return CheckboxPagePartAdminType::class;
138
    }
139
}
140