Skip to content

Commit

Permalink
Merge branch 'docs/19' into develop
Browse files Browse the repository at this point in the history
Forward port #19
  • Loading branch information
michalbundyra committed Mar 16, 2020
2 parents c00bd68 + 90886f0 commit b4c7c4a
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 59 deletions.
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

0 comments on commit b4c7c4a

Please sign in to comment.