Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow aliases and translations #2

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
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
46 changes: 46 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -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
49 changes: 49 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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',
];
}
```
19 changes: 11 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand All @@ -22,5 +20,10 @@
"psr-4": {
"Rareloop\\Lumberjack\\Validation\\Test\\": "tests"
}
},
"config": {
"allow-plugins": {
"composer/installers": true
}
}
}
3 changes: 1 addition & 2 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
printerClass="Codedungeon\PHPUnitPrettyResultPrinter\Printer">
stopOnFailure="false">
<testsuites>
<testsuite name="Rareloop Lumberjack Validation">
<directory>tests</directory>
Expand Down
22 changes: 18 additions & 4 deletions src/AbstractForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ abstract class AbstractForm implements FormInterface
protected $data = [];
protected $rules = [];
protected $messages = [];
protected $aliases = [];
protected $translations = [];
protected $validator;
protected $validation;

Expand All @@ -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()
Expand Down
64 changes: 64 additions & 0 deletions tests/Unit/FormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down Expand Up @@ -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'
];
}