Completed
Push — master ( b0ba81...041dbc )
by Shcherbak
09:45
created

FullForm::init()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 47
Code Lines 30

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 47
rs 9.0303
cc 1
eloc 30
nc 1
nop 0
1
<?php
2
3
  include __DIR__ . '/../vendor/autoload.php';
4
5
  use Fiv\Form\Filter;
6
  use Fiv\Form\Validator;
7
8
  /**
9
   *
10
   */
11
  class Unique extends \Fiv\Form\Validator\Base {
0 ignored issues
show
Deprecated Code introduced by
The class Fiv\Form\Validator\Base has been deprecated.

This class, trait or interface has been deprecated.

Loading history...
12
13
    /**
14
     * @var string
15
     */
16
    protected $error = 'Value must be unique';
17
18
19
    /**
20
     * @param string $error
21
     * @return $this
22
     */
23
    public function setError($error) {
24
      $this->error = $error;
25
      return $this;
26
    }
27
28
29
    /**
30
     * @param string $value
31
     * @return bool
32
     */
33
    public function isValid($value) {
34
      if (in_array($value, ['test', 'daf'])) {
35
        $this->addError($this->error);
36
      }
37
      return !$this->hasErrors();
38
    }
39
40
  }
41
42
  /**
43
   * Example of form with all elements, filters and validators
44
   */
45
  class FullForm extends \Fiv\Form\Form {
46
47
    public function init() {
48
      $this->setMethod('post');
49
50
51
      # login
52
      $login = $this->input('login', 'Логін');
53
54
      $login->addValidator((new \Fiv\Form\Validator\Required())->setError('заповніть поле "логін"'));
55
      $login->addValidator((new \Fiv\Form\Validator\Len())->min(3, 'Мінімальна довжина %s символи'));
56
      $login->addValidator((new \Fiv\Form\Validator\Len())->max(10, 'Максимальна довжина %s символів'));
57
      $login->addValidator((new Unique())->setError('логін повинен бути унікальним'));
58
59
      $login->addFilter(new \Fiv\Form\Filter\Trim());
60
61
      # checkbox list
62
      $checkbox = $this->checkboxList('languages', 'Мови на яких ви спілкуєтесь:');
63
      $checkbox->addValidator(new \Fiv\Form\Validator\Required);
64
65
      $checkbox->setOptions([
66
        'en' => 'En',
67
        'ru' => 'Ru',
68
        'uk' => 'Uk',
69
      ]);
70
      $checkbox->setValue(['uk', 'en']);
71
72
      $this->checkbox('subscribe', 'Підписка на новини');
73
74
      $this->radioList('sex', 'Стать')->setOptions([
75
        'm' => 'Ч',
76
        'f' => 'Ж',
77
      ]);
78
79
      $this->select('type', 'Спосіб життя:')
80
        ->setOptions([
81
          0 => 'Сумний',
82
          1 => 'Веселий',
83
        ])
84
        ->setValue(1);
85
      $this->textarea('info')->setText('Коротко про себе');
86
87
      $login = $this->input('tel')->setText('Телефон:');
88
      $login->addValidator((new \Fiv\Form\Validator\Len())->exact(3, 'Телефон з 3х цифр'));
89
      $login->addFilter(new \Fiv\Form\Filter\Trim());
90
      $login->addFilter(new \Fiv\Form\Filter\RegexReplace('! !', ''));
91
92
      $this->submit('send', 'зареєструватись');
93
    }
94
95
  }
96
97
  $form = new FullForm();
98
  $form->init();
99
100
  if ($form->isSubmitted()) {
101
    if ($form->isValid()) {
102
      $data = $form->getData();
103
      echo "\n***" . __LINE__ . "***\n<pre>" . print_r($data, true) . "</pre>\n";
104
    } else {
105
      foreach ($form->getElements() as $element) {
106
        foreach ($element->getValidatorsErrors() as $error) {
107
          echo '<span style="color:red">' . $error . '</span><br>';
108
        }
109
      }
110
    }
111
112
  }
113
114
  echo $form;
115
116
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
117