From 9fbe37371757c1673df6b531a5c43119cbd82a2e Mon Sep 17 00:00:00 2001 From: Adam Tomat Date: Thu, 4 May 2023 14:44:06 +0100 Subject: [PATCH 01/11] Upgrade rakit/validation to v1.4.0 and bump PHPUnit too --- composer.json | 7 +++---- phpunit.xml | 3 +-- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/composer.json b/composer.json index 31a4b42..61e555e 100644 --- a/composer.json +++ b/composer.json @@ -1,17 +1,16 @@ { "name": "rareloop/lumberjack-validation", "require": { - "rakit/validation": "^0.13.1", + "rakit/validation": "^1.4.0", "rareloop/lumberjack-core": "^5.0.0||^6.0.0" }, "require-dev": { - "phpunit/phpunit": "^6.0", + "phpunit/phpunit": "^9.0", "satooshi/php-coveralls": "^1.0", "mockery/mockery": "^1.0.0", "brain/monkey": "^2.0.2", "satooshi/php-coveralls": "^1.0", - "squizlabs/php_codesniffer": "^3.2", - "codedungeon/phpunit-result-printer": "^0.4.4" + "squizlabs/php_codesniffer": "^3.2" }, "autoload": { "psr-4": { diff --git a/phpunit.xml b/phpunit.xml index 2ebc93b..0c9805e 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -8,8 +8,7 @@ convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" - stopOnFailure="false" - printerClass="Codedungeon\PHPUnitPrettyResultPrinter\Printer"> + stopOnFailure="false"> tests From 0c7f71e713adb0aa846ef1ddc41da589bce7733e Mon Sep 17 00:00:00 2001 From: Adam Tomat Date: Thu, 4 May 2023 14:44:33 +0100 Subject: [PATCH 02/11] =?UTF-8?q?Allow=20forms=20to=20define=20=E2=80=9C$a?= =?UTF-8?q?liases=E2=80=9D=20and=20=E2=80=9C$translations=E2=80=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 49 +++++++++++++++++++++++++++++++ src/AbstractForm.php | 22 +++++++++++--- tests/Unit/FormTest.php | 64 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 131 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 23290b0..90d55ed 100644 --- a/README.md +++ b/README.md @@ -51,3 +51,52 @@ class IndexController } } ``` + +## Adding custom messages + +You can add custom validation messages per rule by adding a `protected $messages` variable to your form. + +[See the documentation](https://github.com/rakit/validation#custom-validation-message) for a list of the variables that are available within your message. + +```php +class ContactForm extends AbstractForm +{ + protected $rules = [ + 'name' => 'required', + 'email' => 'required|email' + ]; + + protected $messages = [ + 'required' => ':attribute missing', + ]; +} +``` + +## Adding custom attribute aliases + +If you need to change how a field's name is presented in the error message, you can add an alias for it. + +For example, if we had the field `district_id`, by default any validation errors for this field would look something like this: + +> The District id field is required + +Instead, you can add an alias by adding a `protected $alias` variable to your form. For example we can change the output to be: + +> The District field is required + +[See the documentation](https://github.com/rakit/validation#attribute-alias) for more information. + +```php +class ContactForm extends AbstractForm +{ + protected $rules = [ + 'province_id' => 'required', + 'district_id' => 'required', + ]; + + protected $aliases = [ + 'province_id' => 'Province', + 'district_id' => 'District', + ]; +} +``` diff --git a/src/AbstractForm.php b/src/AbstractForm.php index 429fe4f..592f2c6 100644 --- a/src/AbstractForm.php +++ b/src/AbstractForm.php @@ -10,6 +10,8 @@ abstract class AbstractForm implements FormInterface protected $data = []; protected $rules = []; protected $messages = []; + protected $aliases = []; + protected $translations = []; protected $validator; protected $validation; @@ -18,16 +20,28 @@ public function __construct(Validator $validator) $this->validator = $validator; } - public function validate(array $data) : bool + public function validate(array $data): bool { $this->data = $data; - $this->validation = $this->validator->validate($this->data, $this->rules, $this->messages); + + if (!empty($this->translations)) { + $this->validator->setTranslations($this->translations); + } + + $this->validation = $this->validator->make($this->data, $this->rules, $this->messages); + + if (!empty($this->aliases)) { + $this->validation->setAliases($this->aliases); + } + + + $this->validation->validate(); if ($this->validation->fails()) { return false; - } else { - return true; } + + return true; } public function errors() diff --git a/tests/Unit/FormTest.php b/tests/Unit/FormTest.php index d343db7..3ec8167 100644 --- a/tests/Unit/FormTest.php +++ b/tests/Unit/FormTest.php @@ -64,6 +64,37 @@ public function can_add_a_custom_message_for_errors() ], $form->errors()); } + /** @test */ + public function can_add_aliases_to_fields_with_custom_message() + { + $form = new FormWithAliases(new Validator); + + $form->validate([]); + + $this->assertSame([ + 'name' => [ + 'FOO missing', + ], + 'email' => [ + 'BAR missing', + ], + ], $form->errors()); + } + + /** @test */ + public function can_add_translations() + { + $form = new FormWithTranslations(new Validator); + + $form->validate(['nomor' => 10]); + + $this->assertSame([ + 'nomor' => [ + "Nomor hanya memperbolehkan '1', '2', atau '3'", + ], + ], $form->errors()); + } + /** @test */ public function can_serialise_form_to_an_array() { @@ -103,3 +134,36 @@ class FormWithCustomMessage extends AbstractForm 'required' => ':attribute missing', ]; } + +class FormWithAliases extends AbstractForm +{ + protected $rules = [ + 'name' => 'required', + 'email' => 'required', + ]; + + protected $messages = [ + 'required' => ':attribute missing', + ]; + + protected $aliases = [ + 'name' => 'FOO', + 'email' => 'BAR', + ]; +} + +class FormWithTranslations extends AbstractForm +{ + protected $rules = [ + 'nomor' => 'in:1,2,3', + ]; + + protected $messages = [ + 'in' => ':attribute hanya memperbolehkan :allowed_values' + ]; + + protected $translations = [ + 'and' => 'dan', + 'or' => 'atau' + ]; +} From 5cea987c88cc699e585f4454b52a4ca3b40d52a0 Mon Sep 17 00:00:00 2001 From: Adam Tomat Date: Thu, 4 May 2023 21:11:19 +0100 Subject: [PATCH 03/11] Upgrade packages --- composer.json | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/composer.json b/composer.json index 61e555e..ef3aa86 100644 --- a/composer.json +++ b/composer.json @@ -5,12 +5,12 @@ "rareloop/lumberjack-core": "^5.0.0||^6.0.0" }, "require-dev": { - "phpunit/phpunit": "^9.0", - "satooshi/php-coveralls": "^1.0", - "mockery/mockery": "^1.0.0", + "phpunit/phpunit": "^9.5.21", + "php-coveralls/php-coveralls": "^2.0", + "mockery/mockery": "^1.5.0", "brain/monkey": "^2.0.2", - "satooshi/php-coveralls": "^1.0", - "squizlabs/php_codesniffer": "^3.2" + "squizlabs/php_codesniffer": "^3.6.0", + "brain/monkey": "~2.4.2" }, "autoload": { "psr-4": { From 29edd7e82ff67ffb67d14fceb37702eb01b7e6ca Mon Sep 17 00:00:00 2001 From: Adam Tomat Date: Thu, 4 May 2023 21:11:29 +0100 Subject: [PATCH 04/11] Add Github CI workflow --- .github/workflows/ci.yml | 46 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 .github/workflows/ci.yml diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..9bda1d3 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,46 @@ +name: CI + +on: [push, pull_request] + +jobs: + build-test: + runs-on: ubuntu-latest + strategy: + matrix: + php_version: [7.4, 8.0, 8.1] + composer_flags: ['', '--prefer-lowest'] + + steps: + - uses: actions/checkout@v2 + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php_version }} + extensions: xdebug + + - name: Install dependencies + uses: php-actions/composer@v5 + with: + php_version: ${{ matrix.php_version }} + args: ${{ matrix.composer_flags }} + command: update + + - name: Run tests + run: ./vendor/bin/phpunit --coverage-clover ./tests/logs/clover.xml + env: + XDEBUG_MODE: coverage + + - name: Run Codesniffer + run: vendor/bin/phpcs --standard=PSR2 ./src + + # - name: Submit coverage to Coveralls + # # We use php-coveralls library for this, as the official Coveralls GitHub Action lacks support for clover reports: + # # https://github.com/coverallsapp/github-action/issues/15 + # env: + # COVERALLS_REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }} + # COVERALLS_PARALLEL: true + # COVERALLS_FLAG_NAME: ${{ github.job }}-PHP-${{ matrix.php_version }} ${{ matrix.composer_flags }} + # run: | + # composer global require php-coveralls/php-coveralls + # ~/.composer/vendor/bin/php-coveralls -v From 80c000c2d5c83ec042ae0583bbd497f10f721766 Mon Sep 17 00:00:00 2001 From: Adam Tomat Date: Thu, 4 May 2023 21:13:25 +0100 Subject: [PATCH 05/11] Trust composer/installers --- composer.json | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index ef3aa86..4e86bd6 100644 --- a/composer.json +++ b/composer.json @@ -21,5 +21,10 @@ "psr-4": { "Rareloop\\Lumberjack\\Validation\\Test\\": "tests" } + }, + "config": { + "allow-plugins": { + "composer/installers": true + } } -} \ No newline at end of file +} From 2ddf8c553c45035359a53c07e71dddfca34f0efa Mon Sep 17 00:00:00 2001 From: Adam Tomat Date: Thu, 4 May 2023 21:24:13 +0100 Subject: [PATCH 06/11] Fix composer.json --- composer.json | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/composer.json b/composer.json index 4e86bd6..cfe71dd 100644 --- a/composer.json +++ b/composer.json @@ -8,9 +8,8 @@ "phpunit/phpunit": "^9.5.21", "php-coveralls/php-coveralls": "^2.0", "mockery/mockery": "^1.5.0", - "brain/monkey": "^2.0.2", - "squizlabs/php_codesniffer": "^3.6.0", - "brain/monkey": "~2.4.2" + "brain/monkey": "~2.4.2", + "squizlabs/php_codesniffer": "^3.6.0" }, "autoload": { "psr-4": { @@ -27,4 +26,4 @@ "composer/installers": true } } -} +} \ No newline at end of file From 6f8f13126a56d6669a72571f72ceaf2f63590db4 Mon Sep 17 00:00:00 2001 From: Adam Tomat Date: Thu, 4 May 2023 21:32:26 +0100 Subject: [PATCH 07/11] Experimenting with disabling platform check --- composer.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index cfe71dd..57a675f 100644 --- a/composer.json +++ b/composer.json @@ -24,6 +24,7 @@ "config": { "allow-plugins": { "composer/installers": true - } + }, + "platform-check": false } } \ No newline at end of file From 31b4541549e79e601c864fd5a1a3f6c6ba812eb0 Mon Sep 17 00:00:00 2001 From: Adam Tomat Date: Thu, 4 May 2023 21:44:10 +0100 Subject: [PATCH 08/11] Testing --- .github/workflows/ci.yml | 4 ++-- composer.json | 3 +-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 9bda1d3..53c2199 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -11,7 +11,7 @@ jobs: composer_flags: ['', '--prefer-lowest'] steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 - name: Setup PHP uses: shivammathur/setup-php@v2 @@ -20,7 +20,7 @@ jobs: extensions: xdebug - name: Install dependencies - uses: php-actions/composer@v5 + uses: php-actions/composer@v6 with: php_version: ${{ matrix.php_version }} args: ${{ matrix.composer_flags }} diff --git a/composer.json b/composer.json index 57a675f..cfe71dd 100644 --- a/composer.json +++ b/composer.json @@ -24,7 +24,6 @@ "config": { "allow-plugins": { "composer/installers": true - }, - "platform-check": false + } } } \ No newline at end of file From 3cdaa5b1650347591613eabb6f3225df8fa8c061 Mon Sep 17 00:00:00 2001 From: Adam Tomat Date: Thu, 4 May 2023 21:52:17 +0100 Subject: [PATCH 09/11] Testing --- .github/workflows/ci.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 53c2199..1903073 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -22,6 +22,7 @@ jobs: - name: Install dependencies uses: php-actions/composer@v6 with: + dev: no php_version: ${{ matrix.php_version }} args: ${{ matrix.composer_flags }} command: update From c5ac470865933afc750e4c1d90cc063d9655fc60 Mon Sep 17 00:00:00 2001 From: Adam Tomat Date: Thu, 4 May 2023 21:54:21 +0100 Subject: [PATCH 10/11] Testing --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 1903073..3538eee 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -24,7 +24,7 @@ jobs: with: dev: no php_version: ${{ matrix.php_version }} - args: ${{ matrix.composer_flags }} + args: ${{ matrix.composer_flags }} --ignore-platform-reqs command: update - name: Run tests From 2154af8d181221a892c449749c73b1352419a6cb Mon Sep 17 00:00:00 2001 From: Adam Tomat Date: Thu, 4 May 2023 21:55:52 +0100 Subject: [PATCH 11/11] Testing --- .github/workflows/ci.yml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 3538eee..53c2199 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -22,9 +22,8 @@ jobs: - name: Install dependencies uses: php-actions/composer@v6 with: - dev: no php_version: ${{ matrix.php_version }} - args: ${{ matrix.composer_flags }} --ignore-platform-reqs + args: ${{ matrix.composer_flags }} command: update - name: Run tests