Browse Source

update form validation

main
ghost 1 year ago
parent
commit
f3b32a713f
  1. 26
      src/app/controller/submit.php
  2. 96
      src/app/model/validator.php
  3. 38
      src/app/view/theme/default/submit.phtml
  4. 20
      src/config/validator.json

26
src/app/controller/submit.php

@ -97,14 +97,12 @@ class AppControllerSubmit
[ [
'value' => null, 'value' => null,
'required' => $this->_validator->getPageKeywordsRequired(), 'required' => $this->_validator->getPageKeywordsRequired(),
'minlength' => $this->_validator->getPageKeywordsLengthMin(),
'maxlength' => $this->_validator->getPageKeywordsLengthMax(),
'placeholder' => sprintf( 'placeholder' => sprintf(
_('Page keywords (%s-%s total / %s-%s chars per item)'), _('Page keywords (%s-%s total / %s-%s chars per item)'),
number_format($this->_validator->getPageKeywordsQuantityMin()), number_format($this->_validator->getPageKeywordsQuantityMin()),
number_format($this->_validator->getPageKeywordsQuantityMax()), number_format($this->_validator->getPageKeywordsQuantityMax()),
number_format($this->_validator->getPageKeywordsLengthMin()), number_format($this->_validator->getPageKeywordLengthMin()),
number_format($this->_validator->getPageKeywordsLengthMax()) number_format($this->_validator->getPageKeywordLengthMax())
), ),
] ]
], ],
@ -115,6 +113,7 @@ class AppControllerSubmit
[ [
'value' => null, 'value' => null,
'required' => $this->_validator->getPageTorrentRequired(), 'required' => $this->_validator->getPageTorrentRequired(),
'accept' => implode(',', $this->_validator->getPageTorrentMimeTypes()),
'placeholder' => sprintf( 'placeholder' => sprintf(
_('Torrent file (use Ctrl to select multiple files)') _('Torrent file (use Ctrl to select multiple files)')
), ),
@ -134,9 +133,9 @@ class AppControllerSubmit
{ {
$error = []; $error = [];
if ($this->_validator->pageTitle($_POST['title'], $error)) if (!$this->_validator->pageTitle($_POST['title'], $error))
{ {
$form->title->error = $error; $form->title->error[] = $error;
} }
// @TODO check for page duplicates // @TODO check for page duplicates
@ -148,9 +147,9 @@ class AppControllerSubmit
{ {
$error = []; $error = [];
if ($this->_validator->pageDescription($_POST['description'], $error)) if (!$this->_validator->pageDescription($_POST['description'], $error))
{ {
$form->description->error = $error; $form->description->error[] = $error;
} }
$form->description->attribute->value = htmlentities($_POST['description']); $form->description->attribute->value = htmlentities($_POST['description']);
@ -160,14 +159,21 @@ class AppControllerSubmit
{ {
$error = []; $error = [];
if ($this->_validator->pageKeywords($_POST['keywords'], $error)) if (!$this->_validator->pageKeywords($_POST['keywords'], $error))
{ {
$form->keywords->error = $error; $form->keywords->error[] = $error;
} }
$form->keywords->attribute->value = htmlentities($_POST['keywords']); $form->keywords->attribute->value = htmlentities($_POST['keywords']);
} }
if (isset($_FILES['torrent']))
{
$error = [];
// @TODO
}
// Request valid // Request valid
if (empty($error)) if (empty($error))
{ {

96
src/app/model/validator.php

@ -167,16 +167,6 @@ class AppModelValidator
return $this->_config->page->keywords->required; return $this->_config->page->keywords->required;
} }
public function getPageKeywordsLengthMin() : int
{
return $this->_config->page->keywords->length->min;
}
public function getPageKeywordsLengthMax() : int
{
return $this->_config->page->keywords->length->max;
}
public function getPageKeywordsQuantityMin() : int public function getPageKeywordsQuantityMin() : int
{ {
return $this->_config->page->keywords->quantity->min; return $this->_config->page->keywords->quantity->min;
@ -187,14 +177,9 @@ class AppModelValidator
return $this->_config->page->keywords->quantity->max; return $this->_config->page->keywords->quantity->max;
} }
public function getPageKeywordsRegex() : string
{
return $this->_config->page->keywords->regex;
}
public function pageKeywords(mixed $value, array &$error = []) : bool public function pageKeywords(mixed $value, array &$error = []) : bool
{ {
if (!is_array($value)) if (!is_string($value))
{ {
array_push( array_push(
$error, $error,
@ -214,67 +199,60 @@ class AppModelValidator
return false; return false;
} }
if ($this->getPageKeywordsRequired())
{
$total = 0; $total = 0;
foreach ($value as $keywords) foreach (explode(PHP_EOL, str_replace(['#', ',', ' '], PHP_EOL, $value)) as $keyword)
{ {
if (!is_string($kt)) $error = [];
if (!$this->pageKeyword($keyword, $error))
{ {
array_push( array_push(
$error, $error,
_('Invalid magnet keyword value data type') _('Invalid page keyword'),
); );
return false; return false;
} }
if (!preg_match(MAGNET_KT_REGEX, $kt)) $total++;
{
array_push(
$error,
sprintf(
_('Magnet keyword format does not match condition "%s"'),
MAGNET_KT_REGEX
)
);
return false;
} }
if (mb_strlen($kt) < MAGNET_KT_MIN_LENGTH || if ($total < $this->getPageKeywordsQuantityMin() ||
mb_strlen($kt) > MAGNET_KT_MAX_LENGTH) $total > $this->getPageKeywordsQuantityMax())
{ {
array_push( array_push(
$error, $error,
sprintf( sprintf(
_('Magnet keyword out of %s-%s chars range'), _('Page keywords quantity out of %s-%s range'),
MAGNET_KT_MIN_LENGTH, $this->getPageKeywordsQuantityMin(),
MAGNET_KT_MAX_LENGTH $this->getPageKeywordsQuantityMax()
) )
); );
return false; return false;
} }
}
$total++; return true;
} }
if ($total < MAGNET_KT_MIN_QUANTITY || /// Page keyword
$total > MAGNET_KT_MAX_QUANTITY) public function getPageKeywordLengthMin() : int
{ {
array_push( return $this->_config->page->keyword->length->min;
$error, }
sprintf(
_('Magnet keywords quantity out of %s-%s range'),
MAGNET_KT_MIN_QUANTITY,
MAGNET_KT_MAX_QUANTITY
)
);
return false; public function getPageKeywordLengthMax() : int
{
return $this->_config->page->keyword->length->max;
} }
return true; public function getPageKeywordRegex() : string
{
return $this->_config->page->keyword->regex;
} }
public function pageKeyword(mixed $value, array &$error = []) : bool public function pageKeyword(mixed $value, array &$error = []) : bool
@ -289,29 +267,28 @@ class AppModelValidator
return false; return false;
} }
if (!preg_match($this->getPageKeywordsRegex(), $value)) if (!preg_match($this->getPageKeywordRegex(), $value))
{ {
array_push( array_push(
$error, $error,
sprintf( sprintf(
_('Page keyword "%s" format does not match condition "%s"'), _('Page keyword format does not match condition "%s"'),
$value, $this->getPageKeywordRegex()
$this->getPageKeywordsRegex()
) )
); );
return false; return false;
} }
if (mb_strlen($value) < $this->getPageDescriptionLengthMin() || if (mb_strlen($value) < $this->getPageKeywordLengthMin() ||
mb_strlen($value) > $this->getPageDescriptionLengthMax()) mb_strlen($value) > $this->getPageKeywordLengthMax())
{ {
array_push( array_push(
$error, $error,
sprintf( sprintf(
_('Page description out of %s-%s chars range'), _('Page keyword out of %s-%s chars range'),
$this->getPageDescriptionLengthMin(), $this->getPageKeywordLengthMin(),
$this->getPageDescriptionLengthMax() $this->getPageKeywordLengthMax()
) )
); );
@ -327,6 +304,11 @@ class AppModelValidator
return $this->_config->page->torrent->required; return $this->_config->page->torrent->required;
} }
public function getPageTorrentMimeTypes() : array
{
return $this->_config->page->torrent->mime;
}
// Common // Common
public function host(mixed $value, array &$error = []) : bool public function host(mixed $value, array &$error = []) : bool
{ {

38
src/app/view/theme/default/submit.phtml

@ -12,7 +12,7 @@
<div class="margin-b-24 padding-b-16 border-bottom-default"> <div class="margin-b-24 padding-b-16 border-bottom-default">
<h2><?php echo _('Submit') ?></h2> <h2><?php echo _('Submit') ?></h2>
</div> </div>
<form class="margin-t-8" name="submit" method="post" action="submit"> <form class="margin-t-8" name="submit" method="post" enctype="multipart/form-data" action="submit">
<div class="margin-b-16"> <div class="margin-b-16">
<label for="title"> <label for="title">
<?php echo _('Title') ?> <?php echo _('Title') ?>
@ -23,12 +23,14 @@
<path d="M8 16A8 8 0 1 0 8 0a8 8 0 0 0 0 16zm.93-9.412-1 4.705c-.07.34.029.533.304.533.194 0 .487-.07.686-.246l-.088.416c-.287.346-.92.598-1.465.598-.703 0-1.002-.422-.808-1.319l.738-3.468c.064-.293.006-.399-.287-.47l-.451-.081.082-.381 2.29-.287zM8 5.5a1 1 0 1 1 0-2 1 1 0 0 1 0 2z"/> <path d="M8 16A8 8 0 1 0 8 0a8 8 0 0 0 0 16zm.93-9.412-1 4.705c-.07.34.029.533.304.533.194 0 .487-.07.686-.246l-.088.416c-.287.346-.92.598-1.465.598-.703 0-1.002-.422-.808-1.319l.738-3.468c.064-.293.006-.399-.287-.47l-.451-.081.082-.381 2.29-.287zM8 5.5a1 1 0 1 1 0-2 1 1 0 0 1 0 2z"/>
</svg> </svg>
</sub> </sub>
<?php foreach ($form->title->error as $error) { ?> <?php foreach ($form->title->error as $errors) { ?>
<div class="text-color-red margin-b-8"> <?php foreach ($errors as $error) { ?>
<div class="text-color-red margin-y-8">
<?php echo $error ?> <?php echo $error ?>
</div> </div>
<?php } ?> <?php } ?>
<input class="width-100 margin-t-8 <?php echo ($form->title->error ? 'background-color-red' : false) ?>" <?php } ?>
<input class="width-100 margin-t-8"
type="text" type="text"
name="title" name="title"
id="title" id="title"
@ -48,12 +50,14 @@
<path d="M8 16A8 8 0 1 0 8 0a8 8 0 0 0 0 16zm.93-9.412-1 4.705c-.07.34.029.533.304.533.194 0 .487-.07.686-.246l-.088.416c-.287.346-.92.598-1.465.598-.703 0-1.002-.422-.808-1.319l.738-3.468c.064-.293.006-.399-.287-.47l-.451-.081.082-.381 2.29-.287zM8 5.5a1 1 0 1 1 0-2 1 1 0 0 1 0 2z"/> <path d="M8 16A8 8 0 1 0 8 0a8 8 0 0 0 0 16zm.93-9.412-1 4.705c-.07.34.029.533.304.533.194 0 .487-.07.686-.246l-.088.416c-.287.346-.92.598-1.465.598-.703 0-1.002-.422-.808-1.319l.738-3.468c.064-.293.006-.399-.287-.47l-.451-.081.082-.381 2.29-.287zM8 5.5a1 1 0 1 1 0-2 1 1 0 0 1 0 2z"/>
</svg> </svg>
</sub> </sub>
<?php foreach ($form->description->error as $error) { ?> <?php foreach ($form->description->error as $errors) { ?>
<div class="text-color-red margin-b-8"> <?php foreach ($errors as $error) { ?>
<div class="text-color-red margin-y-8">
<?php echo $error ?> <?php echo $error ?>
</div> </div>
<?php } ?> <?php } ?>
<textarea class="width-100 margin-t-8 <?php echo ($form->description->error ? 'background-color-red' : false) ?>" <?php } ?>
<textarea class="width-100 margin-t-8"
name="description" name="description"
id="description" id="description"
<?php echo $form->description->attribute->required ? 'required="required"' : false ?> <?php echo $form->description->attribute->required ? 'required="required"' : false ?>
@ -71,12 +75,14 @@
<path d="M8 16A8 8 0 1 0 8 0a8 8 0 0 0 0 16zm.93-9.412-1 4.705c-.07.34.029.533.304.533.194 0 .487-.07.686-.246l-.088.416c-.287.346-.92.598-1.465.598-.703 0-1.002-.422-.808-1.319l.738-3.468c.064-.293.006-.399-.287-.47l-.451-.081.082-.381 2.29-.287zM8 5.5a1 1 0 1 1 0-2 1 1 0 0 1 0 2z"/> <path d="M8 16A8 8 0 1 0 8 0a8 8 0 0 0 0 16zm.93-9.412-1 4.705c-.07.34.029.533.304.533.194 0 .487-.07.686-.246l-.088.416c-.287.346-.92.598-1.465.598-.703 0-1.002-.422-.808-1.319l.738-3.468c.064-.293.006-.399-.287-.47l-.451-.081.082-.381 2.29-.287zM8 5.5a1 1 0 1 1 0-2 1 1 0 0 1 0 2z"/>
</svg> </svg>
</sub> </sub>
<?php foreach ($form->keywords->error as $error) { ?> <?php foreach ($form->keywords->error as $errors) { ?>
<div class="text-color-red margin-b-8"> <?php foreach ($errors as $error) { ?>
<div class="text-color-red margin-y-8">
<?php echo $error ?> <?php echo $error ?>
</div> </div>
<?php } ?> <?php } ?>
<textarea class="width-100 margin-t-8 <?php echo ($form->keywords->error ? 'background-color-red' : false) ?>" <?php } ?>
<textarea class="width-100 margin-t-8"
name="keywords" name="keywords"
<?php echo $form->keywords->attribute->required ? 'required="required"' : false ?> <?php echo $form->keywords->attribute->required ? 'required="required"' : false ?>
placeholder="<?php echo $form->keywords->attribute->placeholder ?>" placeholder="<?php echo $form->keywords->attribute->placeholder ?>"
@ -93,18 +99,20 @@
<path d="M8 16A8 8 0 1 0 8 0a8 8 0 0 0 0 16zm.93-9.412-1 4.705c-.07.34.029.533.304.533.194 0 .487-.07.686-.246l-.088.416c-.287.346-.92.598-1.465.598-.703 0-1.002-.422-.808-1.319l.738-3.468c.064-.293.006-.399-.287-.47l-.451-.081.082-.381 2.29-.287zM8 5.5a1 1 0 1 1 0-2 1 1 0 0 1 0 2z"/> <path d="M8 16A8 8 0 1 0 8 0a8 8 0 0 0 0 16zm.93-9.412-1 4.705c-.07.34.029.533.304.533.194 0 .487-.07.686-.246l-.088.416c-.287.346-.92.598-1.465.598-.703 0-1.002-.422-.808-1.319l.738-3.468c.064-.293.006-.399-.287-.47l-.451-.081.082-.381 2.29-.287zM8 5.5a1 1 0 1 1 0-2 1 1 0 0 1 0 2z"/>
</svg> </svg>
</sub> </sub>
<?php foreach ($form->torrent->error as $error) { ?> <?php foreach ($form->torrent->error as $errors) { ?>
<div class="text-color-red margin-b-8"> <?php foreach ($errors as $error) { ?>
<div class="text-color-red margin-y-8">
<?php echo $error ?> <?php echo $error ?>
</div> </div>
<?php } ?> <?php } ?>
<input class="width-100 margin-t-8 <?php echo ($form->torrent->error ? 'background-color-red' : false) ?>" <?php } ?>
<input class="width-100 margin-t-8"
type="file" type="file"
multiple="multiple" multiple="multiple"
name="torrent" name="torrent"
id="torrent" id="torrent"
<?php echo $form->torrent->attribute->required ? 'required="required"' : false ?> accept="<?php echo $form->torrent->attribute->accept ?>"
value="<?php echo $form->torrent->attribute->value ?>" /> <?php echo $form->torrent->attribute->required ? 'required="required"' : false ?> />
</div> </div>
<!-- <!--
<div class="margin-y-8 padding-t-4"> <div class="margin-y-8 padding-t-4">

20
src/config/validator.json

@ -21,14 +21,8 @@
}, },
"regex": "/.*/ui" "regex": "/.*/ui"
}, },
"keywords": "keyword":
{
"required": false,
"quantity":
{ {
"min": 0,
"max": 20
},
"length": "length":
{ {
"min": 0, "min": 0,
@ -36,9 +30,21 @@
}, },
"regex": "/[\\w]+/ui" "regex": "/[\\w]+/ui"
}, },
"keywords":
{
"required": false,
"quantity":
{
"min": 0,
"max": 20
}
},
"torrent": "torrent":
{ {
"required": true, "required": true,
"mime": [
"application/x-bittorrent"
],
"quantity": "quantity":
{ {
"min": 0, "min": 0,

Loading…
Cancel
Save