Skip to content

Commit 466923a

Browse files
committed
Refactored files uploads, form rules
1 parent d6d7b48 commit 466923a

5 files changed

Lines changed: 101 additions & 73 deletions

File tree

examples/form.php

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,27 @@
1313
use JustCoded\FormHandler\DataObjects\MailMessage;
1414

1515
$validation = [
16-
'rules' => [
17-
'required' => [
18-
'fields' => ['name', 'email', 'subject', 'message'],
19-
'message' => '{field} is required'
16+
'fields' => [
17+
'name' => ['required'],
18+
'email' => ['required', 'email'],
19+
'subject' => ['required'],
20+
'message' => [
21+
'required',
22+
['lengthMin', 5]
2023
],
21-
'email' => [
22-
'fields' => ['email'],
23-
'message' => '{field} is not a valid email address'
24+
'cv_file' => [
25+
[
26+
'required',
27+
'message' => 'Please upload {field}',
28+
],
29+
[
30+
'file',
31+
['jpeg', 'jpg', 'png'], // types.
32+
2000000, // size limit 2 MB.
33+
'message' => '{field} should be up to 2MB and allows only file types jpeg, png.',
34+
],
2435
],
25-
'file' => [
26-
'fields' => ['cv_file', 'image_file'],
27-
'allowType' => ['jpeg', 'jpg', 'pdf', 'png'],
28-
'allowSize' => 10000000 // 10 MB
29-
]
30-
], // according to Valitron doc.
36+
], // according to Valitron doc for mapFieldsRules.
3137
'labels' => [
3238
'name' => 'Name',
3339
'email' => 'Email address'
@@ -41,11 +47,12 @@
4147
'password' => 'YOUR PASSWORD',
4248
'protocol' => 'tls',
4349
'port' => 587,
50+
'attachmentsSizeLimit' => 8000000, // around 8MB.
4451
];
4552

4653
$fileManager = new FileManager([
4754
'uploadPath' => __DIR__ . '/attachments',
48-
'uploadUrl' => $_SERVER['HTTP_ORIGIN'] . '/attachments',
55+
'uploadUrl' => 'http://MY-DOMAIN.COM/attachments',
4956
]);
5057

5158
$message = [

src/DataObjects/MailMessage.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ public function getBcc()
155155
/**
156156
* Getting email Subject
157157
*
158-
* @return mixed
158+
* @return string
159159
*/
160160
public function getSubject()
161161
{
@@ -169,7 +169,7 @@ public function getSubject()
169169
/**
170170
* Getting email Body Template
171171
*
172-
* @return mixed
172+
* @return string
173173
*/
174174
public function getBodyTemplate()
175175
{

src/FormHandler.php

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use JustCoded\FormHandler\Handlers\HandlerInterface;
66
use Valitron\Validator;
7-
use JustCoded\FormHandler\Validator\File as FileValidator;
7+
use JustCoded\FormHandler\Validator\FileValidator;
88

99
/**
1010
* Class FormHandler
@@ -58,6 +58,9 @@ class FormHandler
5858
public function __construct(array $validationRules, HandlerInterface $handler, string $response = 'json')
5959
{
6060
$this->rules = $validationRules;
61+
if (empty($this->rules['fields'])) {
62+
throw new \Exception("You should specify 'fields' in validation array.");
63+
}
6164

6265
$this->handler = $handler;
6366

@@ -74,19 +77,12 @@ public function __construct(array $validationRules, HandlerInterface $handler, s
7477
public function validate(array $data)
7578
{
7679
$this->formFields = $data;
77-
$v = new Validator($data);
78-
79-
$v = FileValidator::validate($v, $this->rules);
80-
// create rules from input array.
81-
foreach ($this->rules['rules'] as $key => $params) {
82-
$rule = $v->rule($key, $params['fields']);
83-
if (!empty($params['message'])) {
84-
$rule->message($params['message']);
85-
}
86-
}
80+
$v = $this->getValidator($data);
81+
82+
$v->mapFieldsRules($this->rules['fields']);
8783

8884
// apply labels.
89-
if (!empty($this->rules)) {
85+
if (!empty($this->rules['labels'])) {
9086
$v->labels($this->rules['labels']);
9187
}
9288

@@ -100,6 +96,23 @@ public function validate(array $data)
10096
return empty($this->errors);
10197
}
10298

99+
/**
100+
* Create validator object with registered custom validators.
101+
*
102+
* @param array $data Data to validate.
103+
*
104+
* @return Validator
105+
*/
106+
public function getValidator($data)
107+
{
108+
$v = new Validator($data);
109+
110+
// register additional validators.
111+
FileValidator::register($v);
112+
113+
return $v;
114+
}
115+
103116
/**
104117
* Sending email by handler
105118
*/

src/Validator/File.php

Lines changed: 0 additions & 45 deletions
This file was deleted.

src/Validator/FileValidator.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
namespace JustCoded\FormHandler\Validator;
4+
5+
use Valitron\Validator;
6+
use JustCoded\FormHandler\DataObjects\File;
7+
8+
/**
9+
* Class File
10+
*
11+
* @package JustCoded\FormHandler\Validator
12+
*/
13+
class FileValidator
14+
{
15+
/**
16+
* Custom Valitron file attachments validation
17+
*
18+
* @param Validator $v Validator object
19+
*
20+
* @return Validator
21+
*/
22+
public static function register(Validator &$v)
23+
{
24+
$v::addRule('file', [static::class, 'validate'], 'is too big or has wrong format.');
25+
return $v;
26+
}
27+
28+
/**
29+
* Custom Valitron file attachments validation
30+
*
31+
* @param string $field Field to validate.
32+
* @param File|null $value Field value.
33+
* @param array $params Rule params
34+
* @param array $fields All available fields.
35+
*
36+
* @return bool
37+
*/
38+
public static function validate($field, $value, array $params, array $fields)
39+
{
40+
$allowTypes = $params[0] ?? [];
41+
$allowSize = $params[1] ?? 2000000;
42+
43+
if (empty($value) || ! is_a($value, File::class)) {
44+
return false;
45+
} elseif (0 < $allowSize && $value->size >= $allowSize) {
46+
return false;
47+
} elseif (!empty($allowTypes) && ! in_array($value->getExtension(), $allowTypes)) {
48+
return false;
49+
}
50+
51+
return true;
52+
}
53+
}

0 commit comments

Comments
 (0)