Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
/composer.lock
/phpunit.xml
/build/lint.cache
/build/pdepend/
/build/logs/
/build/coverage/
/build/api/
/bower_components/
/build/
/vendor/
5 changes: 3 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ before_install:

install:
- travis_retry composer install --no-interaction --prefer-source
- composer info -i

script:
- vendor/bin/phpunit --disallow-test-output --report-useless-tests --configuration build/phpunit.xml.dist
- vendor/bin/phpcs --standard=build/phpcs.xml
- vendor/bin/phpcs
- vendor/bin/phpunit --disallow-test-output --report-useless-tests --coverage-clover build/logs/clover.xml

after_success:
- chmod +x ./docs/deploy.sh
Expand Down
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ All notable changes to this project will be documented in this file, in reverse

- [#27](https://github.com/xtreamwayz/html-form-validator/pull/26) adds the FormFactoryInterface and
ValidationResultInterface.

- [#29](https://github.com/xtreamwayz/html-form-validator/pull/29) adds container-interop compatibility. This enables
custom validators and filters.
- [#29](https://github.com/xtreamwayz/html-form-validator/pull/29) adds the InputFilterFactory which can be used to
instantiate a Zend\InputFilter\Factory from a container-interop compatible container.

### Deprecated

- [#27](https://github.com/xtreamwayz/html-form-validator/pull/26) deprecates:
Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ It's pretty crazy what you have to do to get a form build. Create a lot of php c
etc. So why not build a html form and use the standard element attributes to extract the validation rules and filters.
Together with some powerful html compliant data attributes you can create forms, validation and filters in one place.

Currently There are a few pr's open before this works with zend-service manager:
- [zend-validator #51](https://github.com/zendframework/zend-validator/pull/51)
[source](https://github.com/weierophinney/zend-validator/tree/feature/50)
- [zend-inputfilter #86](https://github.com/zendframework/zend-inputfilter/pull/86)
[source](https://github.com/gianarb/zend-inputfilter/tree/feature/zf3-improvement)
- [zend-inputfilter #95](https://github.com/zendframework/zend-inputfilter/pull/95)
[source](https://github.com/kynx/zend-inputfilter/tree/sm-v2-v3-compat)

## Installation

```bash
Expand Down
6 changes: 3 additions & 3 deletions build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,22 @@
</target>

<target name="build:phpcs" unless="phpcs.done" description="Find PSR2 coding standard violations">
<phpcodesniffer standard="${project.basedir}/build/phpcs.xml">
<phpcodesniffer standard="${project.basedir}/phpcs.xml">
<fileset refid="php.files" />
<formatter type="checkstyle" outfile="${project.basedir}/build/logs/checkstyle.xml" />
</phpcodesniffer>
<property name="phpcs.done" value="true" />
</target>

<target name="build:phpmd" unless="phpmd.done" depends="prepare" description="Perform project mess detection">
<phpmd file="${project.basedir}/src" rulesets="${project.basedir}/build/phpmd.xml">
<phpmd file="${project.basedir}/src" rulesets="${project.basedir}/phpmd.xml">
<formatter type="xml" outfile="${project.basedir}/build/logs/pmd.xml" />
</phpmd>
<property name="phpmd.done" value="true" />
</target>

<target name="build:phpunit" unless="phpunit.done" depends="prepare" description="Run unit tests with PHPUnit">
<exec command="phpunit --configuration ${project.basedir}/build/phpunit.xml.dist"
<exec command="phpunit --coverage-html build/coverage --coverage-clover build/logs/clover.xml --coverage-crap4j build/logs/crap4j.xml --log-junit build/logs/junit.xml"
passthru="true" checkreturn="true" />
<property name="phpunit.done" value="true" />
</target>
Expand Down
20 changes: 0 additions & 20 deletions build/phpunit.xml.dist

This file was deleted.

23 changes: 18 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,27 @@
"homepage": "https://xtreamwayz.github.io/"
}
],
"repositories": [
{
"type": "vcs",
"url": "https://github.com/weierophinney/zend-validator",
"reference": "sm-v2-v3-compat"
},
{
"type": "vcs",
"url": "https://github.com/kynx/zend-inputfilter",
"reference": "feature/50"
}
],
"require": {
"php": "^7.0",
"zendframework/zend-validator": "^2.5",
"zendframework/zend-inputfilter": "^2.6",
"zendframework/zend-validator": "^2.6",
"zendframework/zend-filter": "^2.6",
"zendframework/zend-inputfilter": "^2.5",
"zendframework/zend-servicemanager": "^3.0",
"zendframework/zend-i18n": "^2.6",
"zendframework/zend-uri": "^2.5"
"zendframework/zend-uri": "^2.5",
"zendframework/zend-stdlib": "^2.7|^3.0"
},
"require-dev": {
"phpunit/phpunit": "^5.0",
Expand All @@ -32,8 +45,8 @@
"@cs",
"@test"
],
"cs": "php vendor/squizlabs/php_codesniffer/scripts/phpcs --standard=./build/phpunit.xml.dist",
"test": "php vendor/phpunit/phpunit/phpunit --configuration ./build/phpunit.xml.dist"
"cs": "php vendor/squizlabs/php_codesniffer/scripts/phpcs",
"test": "php vendor/phpunit/phpunit/phpunit"
},
"autoload": {
"psr-4": {
Expand Down
File renamed without changes.
File renamed without changes.
13 changes: 13 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<phpunit bootstrap="./test/bootstrap.php" colors="true" backupGlobals="false">
<testsuites>
<testsuite name="Xtreamwayz\\HTMLFormValidatorTests">
<directory>test</directory>
</testsuite>
</testsuites>

<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">src</directory>
</whitelist>
</filter>
</phpunit>
11 changes: 1 addition & 10 deletions src/FormElement/AbstractFormElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use DOMDocument;
use DOMElement;
use Xtreamwayz\HTMLFormValidator\ValidatorManager;
use Zend\InputFilter\InputInterface;

abstract class AbstractFormElement
Expand Down Expand Up @@ -46,12 +45,6 @@ public function __invoke(DOMElement $element, InputInterface $input, DOMDocument
// Enforce required and allow empty properties
if ($this->element->hasAttribute('required') || $this->element->getAttribute('aria-required') == 'true') {
$this->input->setRequired(true);
// Attach NotEmpty validator manually so it won't use the plugin manager, which fails for servicemanager 3
$this->attachValidatorByName('notempty');
} else {
$this->input->setRequired(false);
// Enforce properties so it doesn't try to load NotEmpty, which fails for servicemanager 3
$this->input->setAllowEmpty(true);
}
}

Expand Down Expand Up @@ -116,9 +109,7 @@ protected function attachValidators()
*/
protected function attachValidatorByName($name, array $options = [])
{
// Needs to be refactored after zend-validator got an update
$class = ValidatorManager::getValidator($name);
$this->input->getValidatorChain()->attach(new $class($options));
$this->input->getValidatorChain()->attachByName($name, $options);
}

/**
Expand Down
4 changes: 3 additions & 1 deletion src/FormElement/Checkbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Xtreamwayz\HTMLFormValidator\FormElement;

use Zend\Validator\Identical;

class Checkbox extends AbstractFormElement
{
/**
Expand All @@ -16,7 +18,7 @@ protected function attachDefaultFilters()
*/
protected function attachDefaultValidators()
{
$this->attachValidatorByName('identical', [
$this->attachValidatorByName(Identical::class, [
'token' => $this->element->getAttribute('value'),
]);
}
Expand Down
3 changes: 2 additions & 1 deletion src/FormElement/Color.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Xtreamwayz\HTMLFormValidator\FormElement;

use Zend\Filter\StringToLower;
use Zend\Validator\Regex;

class Color extends AbstractFormElement
{
Expand All @@ -19,7 +20,7 @@ protected function attachDefaultFilters()
*/
protected function attachDefaultValidators()
{
$this->attachValidatorByName('regex', [
$this->attachValidatorByName(Regex::class, [
'pattern' => '/^\#[a-fA-Z0-9]{6}$/',
]);
}
Expand Down
4 changes: 3 additions & 1 deletion src/FormElement/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Xtreamwayz\HTMLFormValidator\FormElement;

use Zend\Validator\Date as DateValidator;

class Date extends AbstractFormElement
{
/**
Expand All @@ -16,7 +18,7 @@ protected function attachDefaultFilters()
*/
protected function attachDefaultValidators()
{
$this->attachValidatorByName('date', [
$this->attachValidatorByName(DateValidator::class, [
'format' => 'Y-m-d',
]);
}
Expand Down
4 changes: 3 additions & 1 deletion src/FormElement/DateTimeLocal.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Xtreamwayz\HTMLFormValidator\FormElement;

use Zend\Validator\Date as DateValidator;

class DateTimeLocal extends AbstractFormElement
{
/**
Expand All @@ -16,7 +18,7 @@ protected function attachDefaultFilters()
*/
protected function attachDefaultValidators()
{
$this->attachValidatorByName('date', [
$this->attachValidatorByName(DateValidator::class, [
'format' => 'Y-m-d\TH:i',
]);
}
Expand Down
13 changes: 8 additions & 5 deletions src/FormElement/Email.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

use Zend\Filter\StringTrim;
use Zend\Filter\StripNewlines;
use Zend\Validator\EmailAddress;
use Zend\Validator\Regex;
use Zend\Validator\StringLength;

class Email extends AbstractFormElement
{
Expand All @@ -21,22 +24,22 @@ protected function attachDefaultFilters()
*/
protected function attachDefaultValidators()
{
$this->attachValidatorByName('emailaddress', [
$this->attachValidatorByName(EmailAddress::class, [
'useMxCheck' => filter_var(
$this->element->getAttribute('data-validator-use-mx-check'),
FILTER_VALIDATE_BOOLEAN
),
]);

if ($this->element->hasAttribute('minlength') || $this->element->hasAttribute('maxlength')) {
$this->attachValidatorByName('stringlength', [
'min' => $this->element->getAttribute('minlength') ?: 0,
'max' => $this->element->getAttribute('maxlength') ?: null,
$this->attachValidatorByName(StringLength::class, [
'min' => $this->element->getAttribute('minlength') ?: 0,
'max' => $this->element->getAttribute('maxlength') ?: null,
]);
}

if ($this->element->hasAttribute('pattern')) {
$this->attachValidatorByName('regex', [
$this->attachValidatorByName(Regex::class, [
'pattern' => sprintf('/%s/', $this->element->getAttribute('pattern')),
]);
}
Expand Down
4 changes: 3 additions & 1 deletion src/FormElement/Month.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Xtreamwayz\HTMLFormValidator\FormElement;

use Zend\Validator\Date as DateValidator;

class Month extends AbstractFormElement
{
/**
Expand All @@ -16,7 +18,7 @@ protected function attachDefaultFilters()
*/
protected function attachDefaultValidators()
{
$this->attachValidatorByName('date', [
$this->attachValidatorByName(DateValidator::class, [
'format' => 'Y-m',
]);
}
Expand Down
17 changes: 11 additions & 6 deletions src/FormElement/Number.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
namespace Xtreamwayz\HTMLFormValidator\FormElement;

use InvalidArgumentException;
use Zend\I18n\Validator\IsFloat;
use Zend\I18n\Validator\IsInt;
use Zend\Validator\GreaterThan;
use Zend\Validator\LessThan;
use Zend\Validator\Step;

class Number extends AbstractFormElement
{
Expand All @@ -21,31 +26,31 @@ protected function attachDefaultValidators()
$step = ($this->element->hasAttribute('step')) ? $this->element->getAttribute('step') : 1;

if (is_numeric($step) && (int) $step == $step) {
$this->attachValidatorByName('isint');
$this->attachValidatorByName(IsInt::class);
} elseif (is_numeric($step) && (float) $step == $step) {
$this->attachValidatorByName('isfloat', [
'locale' => 'en'
$this->attachValidatorByName(IsFloat::class, [
'locale' => 'en',
]);
} elseif ($step != 'any') {
throw new InvalidArgumentException('Number step must be an int, float or the text "any"');
}

if ($this->element->hasAttribute('min')) {
$this->attachValidatorByName('greaterthan', [
$this->attachValidatorByName(GreaterThan::class, [
'min' => $this->element->getAttribute('min'),
'inclusive' => true,
]);
}

if ($this->element->hasAttribute('max')) {
$this->attachValidatorByName('lessthan', [
$this->attachValidatorByName(LessThan::class, [
'max' => $this->element->getAttribute('max'),
'inclusive' => true,
]);
}

if (is_numeric($step)) {
$this->attachValidatorByName('step', [
$this->attachValidatorByName(Step::class, [
'step' => $step,
'baseValue' => ($this->element->hasAttribute('min')) ? $this->element->getAttribute('min') : 0,
]);
Expand Down
10 changes: 6 additions & 4 deletions src/FormElement/Password.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Xtreamwayz\HTMLFormValidator\FormElement;

use Zend\Filter\StripNewlines;
use Zend\Validator\Regex;
use Zend\Validator\StringLength;

class Password extends AbstractFormElement
{
Expand All @@ -20,14 +22,14 @@ protected function attachDefaultFilters()
protected function attachDefaultValidators()
{
if ($this->element->hasAttribute('minlength') || $this->element->hasAttribute('maxlength')) {
$this->attachValidatorByName('stringlength', [
'min' => $this->element->getAttribute('minlength') ?: 0,
'max' => $this->element->getAttribute('maxlength') ?: null,
$this->attachValidatorByName(StringLength::class, [
'min' => $this->element->getAttribute('minlength') ?: 0,
'max' => $this->element->getAttribute('maxlength') ?: null,
]);
}

if ($this->element->hasAttribute('pattern')) {
$this->attachValidatorByName('regex', [
$this->attachValidatorByName(Regex::class, [
'pattern' => sprintf('/%s/', $this->element->getAttribute('pattern')),
]);
}
Expand Down
Loading