Skip to content

Commit bf7c51e

Browse files
authored
housekeeping: Improve Custom Formatters Docs (#153)
1 parent 4b6ad22 commit bf7c51e

File tree

1 file changed

+21
-10
lines changed

1 file changed

+21
-10
lines changed

README.md

+21-10
Original file line numberDiff line numberDiff line change
@@ -221,15 +221,9 @@ this.ValidationRule(
221221
"Passwords must match.");
222222
```
223223

224-
## Capabilities
224+
## Custom Formatters
225225

226-
In essence, ReactiveUI.Validation is a relatively simple model of the `ValidationContext` containing a list of `IValidationComponent` instances. An `IValidationComponent` provides an observable of `IValidationState`. Whenever validation state changes (either a transition of validity) or `ValidationText` changes, then a new value is pushed out.
227-
228-
1. Rules can be composed of single or multiple properties along with more generic Observables.
229-
2. Validation text can encapsulate both valid and invalid states.
230-
3. Binding can occur to either a View or an action.
231-
4. Validation text can reference either the ViewModel or properties which comprise the validation rule e.g. include text entered as part of validation message.
232-
5. Validation text output can be adjusted using custom formatters, not only allowing for single & multiline output but also for platforms like Android it should be possible to achieve richer renderings i.e. Bold/italics.
226+
You can pass an instance of `IValidationTextFormatter<T>` to a call to `BindValidation` if you'd like to override the default `SingleLineFormatter` used in the validation library. The `SingleLineFormatter` accepts a separator char and uses whitespace by default, so the code snippet below shows how to use a non-default separator char:
233227

234228
```cs
235229
// This formatter is based on the default SingleLineFormatter but uses a custom separator char.
@@ -238,7 +232,7 @@ this.BindValidation(ViewModel, x => x.ErrorLabel.Text, formatter)
238232
.DisposeWith(disposables);
239233
```
240234

241-
The simplest possible `IValidationTextFormatter<TOut>` implementation may look like this one.
235+
The simplest possible custom `IValidationTextFormatter<TOut>` implementation may look like this one.
242236

243237
```cs
244238
private class ConstFormatter : IValidationTextFormatter<string>
@@ -250,12 +244,29 @@ private class ConstFormatter : IValidationTextFormatter<string>
250244
public string Format(ValidationText validationText) => _text;
251245
}
252246

253-
// This formatter is based on a custsom IValidationTextFormatter implementation.
247+
// This formatter is based on a custom IValidationTextFormatter implementation.
254248
var formatter = new ConstFormatter("The input is invalid.");
255249
this.BindValidation(ViewModel, x => x.ErrorLabel.Text, formatter)
256250
.DisposeWith(disposables);
257251
```
258252

253+
If you'd like to override the `IValidationTextFormatter<string>` used in ReactiveUI.Validation by default, register an instance of `IValidationTextFormatter<string>` into `Locator.CurrentMutable` before your app starts. This could be useful in cases when your app needs localization and you wish to pass message keys instead of messages to `ValidationRule` calls.
254+
255+
```cs
256+
// Register a singleton instance of IValidationTextFormatter<string> into Splat.Locator.
257+
Locator.CurrentMutable.RegisterConstant(new CustomFormatter(), typeof(IValidationTextFormatter<string>));
258+
```
259+
260+
## Capabilities
261+
262+
In essence, ReactiveUI.Validation is a relatively simple model of the `ValidationContext` containing a list of `IValidationComponent` instances. An `IValidationComponent` provides an observable of `IValidationState`. Whenever validation state changes (either a transition of validity) or `ValidationText` changes, then a new value is pushed out.
263+
264+
1. Rules can be composed of single or multiple properties along with more generic Observables.
265+
2. Validation text can encapsulate both valid and invalid states.
266+
3. Binding can occur to either a View or an action.
267+
4. Validation text can reference either the ViewModel or properties which comprise the validation rule e.g. include text entered as part of validation message.
268+
5. Validation text output can be adjusted using custom formatters, not only allowing for single & multiline output but also for platforms like Android it should be possible to achieve richer renderings i.e. Bold/italics.
269+
259270
## Feedback
260271

261272
Please use [GitHub issues](https://github.com/reactiveui/ReactiveUI.Validation/issues) for questions, comments, or bug reports.

0 commit comments

Comments
 (0)