[][src]Struct form_validation::ValidatorFn

pub struct ValidatorFn<Value, Key> { /* fields omitted */ }

Function to perform validation on a form field.

Example

use form_validation::{Validation, ValidationError, ValidatorFn};

let v: ValidatorFn<i32, String> = ValidatorFn::new(|value, key: &String| {
    if value < &0 {
        let value_clone = *value;
        Err(ValidationError::new(key.clone()).with_message(move |key| {
            format!(
                "The value of {} ({}) cannot be less than 0",
                key, value_clone
            )
        }).into()) // convert into ValidationErrors
    } else {
        Ok(())
    }
});

let key = "field1".to_string();
assert!(v.validate_value(&20, &key).is_ok());
assert!(v.validate_value(&-1, &key).is_err());
assert_eq!(
    "The value of field1 (-1) cannot be less than 0",
    v.validate_value(&-1, &key).unwrap_err().to_string()
);

Implementations

impl<Value, Key> ValidatorFn<Value, Key>[src]

pub fn new<C>(closure: C) -> Self where
    C: Fn(&Value, &Key) -> Result<(), ValidationErrors<Key>> + 'static, 
[src]

Create a new ValidatorFn.

Trait Implementations

impl<Value, Key> Clone for ValidatorFn<Value, Key>[src]

impl<Value, Key> Debug for ValidatorFn<Value, Key>[src]

impl<C, Value, Key> From<C> for ValidatorFn<Value, Key> where
    C: Fn(&Value, &Key) -> Result<(), ValidationErrors<Key>> + 'static, 
[src]

impl<Value, Key> From<ValidatorFn<Value, Key>> for AsyncValidatorFn<Value, Key> where
    Key: Clone + PartialEq + 'static,
    Value: Clone + PartialEq + 'static, 
[src]

impl<Value, Key> PartialEq<ValidatorFn<Value, Key>> for ValidatorFn<Value, Key>[src]

impl<Value, Key> Validation<Value, Key> for ValidatorFn<Value, Key> where
    Key: Clone + PartialEq
[src]

Auto Trait Implementations

impl<Value, Key> !RefUnwindSafe for ValidatorFn<Value, Key>

impl<Value, Key> !Send for ValidatorFn<Value, Key>

impl<Value, Key> !Sync for ValidatorFn<Value, Key>

impl<Value, Key> Unpin for ValidatorFn<Value, Key>

impl<Value, Key> !UnwindSafe for ValidatorFn<Value, Key>

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<V, T> VZip<V> for T where
    V: MultiLane<T>,