Skip to content

Commit

Permalink
Add filesizeValidator (#26)
Browse files Browse the repository at this point in the history
* Add filesizeValidator

* Bump version
  • Loading branch information
MrEssex authored Apr 14, 2023
1 parent f0f542a commit 46a3f33
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 10 deletions.
28 changes: 25 additions & 3 deletions demo/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Packaged\SafeHtml\SafeHtml;
use Packaged\Validate\Validators\ConfirmationValidator;
use Packaged\Validate\Validators\EqualValidator;
use Packaged\Validate\Validators\FileSizeValidator;
use Packaged\Validate\Validators\RegexValidator;
use Packaged\Validate\Validators\RequiredValidator;
use Packaged\Validate\Validators\StringValidator;
Expand Down Expand Up @@ -94,7 +95,10 @@ protected function _initDataHandlers()
->addValidator(new RequiredValidator());
$this->youCantTouchThis = ReadOnlyDataHandler::i()->setValue('Dare You');
$this->age = IntegerDataHandler::i()->setLabel('How old are you?');
$this->profilePicture = FileDataHandler::i()->setLabel("Profile Picture")->setGuidance('120px X 120px png or jpg');
$this->profilePicture = FileDataHandler::i()
->addValidator(new FileSizeValidator(1))
->setLabel("Profile Picture")
->setGuidance('Max Size 1mb');
$this->comments = MultiLineTextDataHandler::i()
->setLabel("Any Comments?")
->setGuidance("Required")
Expand All @@ -113,8 +117,7 @@ protected function _configureDataHandlers()
$form = new DemoForm();
if(!empty($data))
{
$form->hydrate($data);
$form->validate();
$errors = $form->hydrate($data);
}

?>
Expand Down Expand Up @@ -151,6 +154,24 @@ protected function _configureDataHandlers()
<h2>Example Form</h2>
<?php echo $form->render() ?>

<?php if(!empty($errors)): ?>
<div>
<h2>ERROR Data</h2>
<table>
<?php foreach($errors as $k => $v): ?>
<tr>
<th><?= $k; ?></th>
<td>
<?php foreach($v as $o): ?>
<?= $o->getMessage() ?>
<?php endforeach; ?>
</td>
</tr>
<?php endforeach; ?>
</table>
</div>
<?php endif; ?>

<?php if(!empty($data)): ?>
<div style="display: flex">
<div><h2>POST Data</h2>
Expand All @@ -164,6 +185,7 @@ protected function _configureDataHandlers()
</table>
</div>
<div><h2>Form Data</h2>
<?= $form->selection->getValue() ?>
<table>
<?php foreach($form->getFormData() as $k => $v): ?>
<tr>
Expand Down
2 changes: 1 addition & 1 deletion js/dist/form.min.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion js/dist/form.min.js

Large diffs are not rendered by default.

16 changes: 12 additions & 4 deletions js/src/validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,16 @@ const _errorMap = new WeakMap();

function _getEleValue(ele)
{
if((!(ele.type === 'checkbox' || ele.type === 'radio')) || ele instanceof HTMLSelectElement || ele.checked)
if((!(ele.type === 'checkbox' || ele.type === 'radio' || ele.type === 'file')) || ele instanceof HTMLSelectElement || ele.checked)
{
return ele.value;
}

if(ele.type === 'file' && ele.files[0])
{
return ele.files[0];
}

return null;
}

Expand Down Expand Up @@ -48,7 +54,7 @@ function _getHandlerValue(form, handlerName)
return;
}
if(ele.getAttribute('name')
.substr(-1) === ']')
.substr(-1) === ']')
{
if(!fieldValue || fieldValue.constructor.name !== 'Array')
{
Expand Down Expand Up @@ -221,7 +227,8 @@ export function validateHandler(form, handlerName, errorOnPotentiallyValid = fal
addErrors(form, handlerName, result, errorOnPotentiallyValid);
}

form.dispatchEvent(new CustomEvent('form-handler-validation',
form.dispatchEvent(new CustomEvent(
'form-handler-validation',
{detail: {handlerName, result}, bubbles: true, cancellable: false}
));

Expand Down Expand Up @@ -250,7 +257,8 @@ export function validateForm(form)
fullResult.append(handlerName, result);
});

form.dispatchEvent(new CustomEvent('form-validation',
form.dispatchEvent(new CustomEvent(
'form-validation',
{detail: {result: fullResult}, bubbles: true, cancellable: false}
));
return fullResult;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@packaged/form",
"version": "4.2.3",
"version": "4.3.0",
"main": "/index.js",
"repository": "[email protected]:packaged/form",
"author": "Tom Kay <[email protected]>",
Expand Down

0 comments on commit 46a3f33

Please sign in to comment.