diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..53c2199 --- /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@v3 + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php_version }} + extensions: xdebug + + - name: Install dependencies + uses: php-actions/composer@v6 + 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 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/composer.json b/composer.json index 31a4b42..cfe71dd 100644 --- a/composer.json +++ b/composer.json @@ -1,17 +1,15 @@ { "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", - "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" + "phpunit/phpunit": "^9.5.21", + "php-coveralls/php-coveralls": "^2.0", + "mockery/mockery": "^1.5.0", + "brain/monkey": "~2.4.2", + "squizlabs/php_codesniffer": "^3.6.0" }, "autoload": { "psr-4": { @@ -22,5 +20,10 @@ "psr-4": { "Rareloop\\Lumberjack\\Validation\\Test\\": "tests" } + }, + "config": { + "allow-plugins": { + "composer/installers": true + } } } \ No newline at end of file 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 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' + ]; +}