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

Adds new documentation for IsFloat validator #19

Merged
merged 2 commits into from
Mar 16, 2020
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
59 changes: 0 additions & 59 deletions docs/book/validators.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,62 +121,3 @@ expression options. These include *Korean*, *Japanese*, and *Chinese*.

As a result, when using the `Alpha` validator with these languages, the input
will be validated using the English alphabet.

## IsFloat

`Laminas\I18n\Validator\IsFloat` allows you to validate if a given value contains a
floating-point value. This validator takes into account localized input.

### Supported options

The following options are supported for `Laminas\I18n\Validator\IsFloat`:

- `locale`: Sets the locale to use when validating localized float values.

### Basic float validation

By default, if no locale is provided, `IsFloat` will use the system locale.

```php
$validator = new Laminas\I18n\Validator\IsFloat();

$validator->isValid(1234.5); // returns true
$validator->isValid('10a01'); // returns false
$validator->isValid('1,234.5'); // returns true
```

(The above example assumes that the environment locale is set to `en`.)

### Localized float validation

Float values are often written differently based on the country or region. For
example, using English, you might write `1.5`, whereas in german you would write
`1,5`, and in other languages you might use grouping.

`Laminas\I18n\Validator\IsFloat` is able to validate such notations. However, it is
limited to the locale you set. See the following code:

```php
$validator = new Laminas\I18n\Validator\IsFloat(['locale' => 'de']);

$validator->isValid(1234.5); // returns true
$validator->isValid("1 234,5"); // returns false
$validator->isValid("1.234"); // returns true
```

By using a locale, your input is validated based on the locale provided. Using a
notation not specific to the locale results in a `false` evaulation.

The default validation locale can also be set after instantiation using
`setLocale()`, and retrieved using `getLocale()`.

### Migration from 2.0-2.3 to 2.4+

Version 2.4 adds support for PHP 7. In PHP 7, `float` is a reserved keyword,
which required renaming the `Float` validator. If you were using the `Float`
validator directly previously, you will now receive an `E_USER_DEPRECATED`
notice on instantiation. Please update your code to refer to the `IsFloat` class
instead.

Users pulling their `Float` validator instance from the validator plugin manager
receive an `IsFloat` instance instead starting in 2.4.0.
67 changes: 67 additions & 0 deletions docs/book/validators/is-float.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# IsFloat

`Laminas\I18n\Validator\IsFloat` allows you to validate if a given value
**contains a floating-point value**. This validator takes into account localized
input.

Float values are often written differently based on the country or region. For
example, using English, you might write `1.5`, whereas in german you would write
`1,5`, and in other languages you might use grouping.

`Laminas\I18n\Validator\IsFloat` is able to validate such notations. However, it
is limited to the locale you set.

## Basic Usage

```php
$validator = new Laminas\I18n\Validator\IsFloat();

$validator->isValid(1234.5); // true
$validator->isValid('10a01'); // false
$validator->isValid('1,234.5'); // true
```

By default, if no locale is provided, `IsFloat` will use the system locale
provided by PHP's `Locale` class and the `getDefault()` method.

(The above example assumes that the environment locale is set to `en`.)

Using a notation not specific to the locale results in a `false` evaluation.

## Using Locale

```php fct_label="Constructor Usage"
$validator = new Laminas\I18n\Validator\IsFloat(['locale' => 'en_US']);

$validator->isValid(1234.5); // true
```

```php fct_label="Setter Usage"
$validator = new Laminas\I18n\Validator\IsFloat();
$validator->setLocale('en_US');

$validator->isValid(1234.5); // true
```

```php fct_label="Locale Class Usage"
Locale::setDefault('en_US');

$validator = new Laminas\I18n\Validator\IsFloat();

$validator->isValid(1234.5); // true
```

### Get Current Value

To get the current value of this option, use the `getLocale()` method.

```php
$validator = new Laminas\I18n\Validator\IsFloat(['locale' => 'en_US']);

echo $validator->getLocale(); // 'en_US'
```

### Default Value

By default, if no locale is provided, `IsFloat` will use the system locale
provided by PHP's `Locale::getDefault()`.
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ nav:
- Validators:
- 'Standard Validators': validators.md
- DateTime: validators/date-time.md
- IsFloat: validators/is-float.md
- IsInt: validators/is-int.md
- PhoneNumber: validators/phone-number.md
- PostCode: validators/post-code.md
Expand Down