pub enum Validator<T, S> {
    None,
    Custom(Rc<dyn Fn(ValidationContext<T>) -> S>),
}

Variants§

§

None

§

Custom(Rc<dyn Fn(ValidationContext<T>) -> S>)

Implementations§

Examples found in repository?
src/form/input.rs (line 135)
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
    fn update(&mut self, ctx: &Context<Self>, msg: Self::Message) -> bool {
        match msg {
            TextInputMsg::Init => {
                ctx.props().onvalidate.emit(ValidationContext {
                    value: self.value(ctx),
                    initial: true,
                });
            }
            TextInputMsg::Changed(data) => {
                self.value = Some(data.clone());
                ctx.props().onchange.emit(data.clone());
                ctx.props().onvalidate.emit(data.into());
            }
            TextInputMsg::Input(data) => {
                ctx.props().oninput.emit(data);
                if let Some(value) = self.extract_value() {
                    self.value = Some(value.clone());
                    ctx.props().onchange.emit(value.clone());
                    ctx.props().onvalidate.emit(value.into());
                }
                // only re-render if we have a validator
                return ctx.props().validator.is_custom();
            }
        }
        true
    }

Convert into the context and run

Examples found in repository?
src/form/group.rs (line 228)
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
    fn update(&mut self, ctx: &Context<Self>, msg: Self::Message) -> bool {
        match msg {
            Self::Message::Validate(value) => {
                let state = ctx.props().validator.run(value);
                if self.state != state {
                    self.state = state;
                    ctx.props()
                        .onvalidated
                        .emit(self.state.clone().unwrap_or_default());
                    if let Some((validation_ctx, _)) = ctx
                        .link()
                        .context::<ValidationFormContext>(Callback::noop())
                    {
                        validation_ctx
                            .push_state(GroupValidationResult(self.id.clone(), self.state.clone()));
                    }
                }
            }
        }
        true
    }
More examples
Hide additional examples
src/pagination.rs (line 137)
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
    fn view(&self, ctx: &Context<Self>) -> Html {
        // The pagination menu : "1-20 of nnn"
        let mut menu_classes = Classes::from("pf-c-options-menu");
        if self.expanded {
            menu_classes.push("pf-m-expanded");
        }

        // The default rust div operator does floor(), we need ceil, so we cast to float before doing the operation
        let max_page = ctx
            .props()
            .total_entries
            .map(|m| (m as f64 / ctx.props().selected_choice as f64).ceil() as u32);
        let current_page =
            (ctx.props().offset as f64 / ctx.props().selected_choice as f64).ceil() as u32;

        let is_last_page = if let Some(max) = ctx.props().total_entries {
            ctx.props().offset + ctx.props().selected_choice > max
        } else {
            false
        };

        let total_entries = ctx
            .props()
            .total_entries
            .map(|m| format!("{}", m))
            .unwrap_or_else(|| String::from("unknown"));
        // +1 because humans don't count from 0 :)
        let showing = format!(
            "{} - {}",
            ctx.props().offset + 1,
            ctx.props().offset + ctx.props().selected_choice
        );

        let limit_choices = ctx.props().entries_per_page_choices.clone();
        let link = ctx.link().clone();

        // todo also add max page
        let page_number_field_validator = Validator::from(
            |ctx: ValidationContext<String>| match ctx.value.parse::<u32>() {
                Ok(value) => {
                    if value > 0 {
                        InputState::Default
                    } else {
                        InputState::Error
                    }
                }
                Err(_) => InputState::Error,
            },
        );

        let validator_clone = page_number_field_validator.clone();
        let onchange_callback = {
            ctx.link().callback(move |input: String| {
                match validator_clone.run(ValidationContext::from(input.clone())) {
                    Some(InputState::Default) => Msg::Page(input.parse::<u32>().unwrap()),
                    _ => Msg::Page(current_page + 1),
                }
            })
        };

        return html! {

          <div class="pf-c-pagination" ref={self.global_close.clone()} >

              // the selector of how many entries per page to display
              <div class="pf-c-pagination__total-items">
                  <b>{ showing.clone() }</b> {"\u{00a0}of\u{00a0}"}
                  <b>{ total_entries.clone() }</b>
              </div>
              <div class={ menu_classes }>
                  <div class="pf-c-options-menu__toggle pf-m-text pf-m-plain">
                      <span class="pf-c-options-menu__toggle-text">
                           <b>{ showing }</b>{"\u{00a0}of\u{00a0}"}
                          <b>{ total_entries }</b>
                      </span>
                  <Button
                      class="pf-c-options-menu__toggle-button"
                      id="pagination-options-menu-top-toggle"
                      //aria-haspopup="listbox"
                      aria_label="Items per page"
                      onclick={ctx.link().callback(|_|Msg::ToggleMenu)}
                      >
                          <span class="pf-c-options-menu__toggle-button-icon">
                              { Icon::CaretDown }
                          </span>
                  </Button>
          </div>
          {{ if self.expanded {
              html! {
              <ul class="pf-c-options-menu__menu" >
                  { for limit_choices.into_iter().map(|limit|  { html!{
                        <li>
                          <Button
                              class="pf-c-options-menu__menu-item"
                                      onclick={link.callback(move |_|Msg::SetLimit(limit))}>
                                  {limit} {" per page"}
                                  {{ if ctx.props().selected_choice == limit {
                                       html!{
                                      <div class="pf-c-options-menu__menu-item-icon">
                                         { Icon::Check }
                                      </div>
                                          }
                                  } else {
                                      html!{}
                                  } }}
                          </Button>
                        </li>
                  }})}
              </ul>
              }
          } else { html! {} }
          }}
        </div>


              // the navigation buttons

              <nav class="pf-c-pagination__nav" aria-label="Pagination">
                  <div class="pf-c-pagination__nav-control pf-m-first">
                    <Button
                      variant={Variant::Plain}
                      onclick={ctx.link().callback(|_|Msg::First)}
                      disabled={ ctx.props().offset == 0 }
                      aria_label="Go to first page"
                    >
                      { Icon::AngleDoubleLeft }
                    </Button>
                  </div>
                  <div class="pf-c-pagination__nav-control pf-m-prev">
                      <Button
                          aria_label="Go to previous page"
                          variant={Variant::Plain}
                          onclick={ctx.link().callback(|_|Msg::Previous)}
                          disabled= { ctx.props().offset == 0 }
                      >
                             { Icon::AngleLeft }
                      </Button>
                  </div>
                  <div class="pf-c-pagination__nav-page-select">
                    <TextInput
                      r#type="number"
                      validator={page_number_field_validator}
                      onchange={onchange_callback}
                      value = {(current_page+1).to_string()}
                    />
                  {{
                      if let Some(max_page) = max_page {
                          html!{<span aria-hidden="true">{ "of "} { max_page }</span>}
                      } else {
                          html!{}
                      }
                  }}
                  </div>
                  <div class="pf-c-pagination__nav-control pf-m-next">
                    <Button
                      aria_label="Go to next page"
                      variant={Variant::Plain}
                      onclick={ctx.link().callback(|_|Msg::Next)}
                      disabled={max_page.map_or_else(|| false, |m| current_page >= m)}
                    >
                      { Icon::AngleRight }
                    </Button>
                  </div>
                  <div class="pf-c-pagination__nav-control pf-m-last">
                    <Button
                      aria_label="Go to last page"
                      variant={Variant::Plain}
                      onclick={ctx.link().callback(|_|Msg::Last)}
                      disabled={is_last_page}
                    >
                      { Icon::AngleDoubleRight }
                    </Button>
                  </div>
              </nav>
          </div>
              };
    }

Only convert when necessary, and run.

Examples found in repository?
src/validator.rs (line 35)
31
32
33
34
35
36
    pub fn run<C>(&self, ctx: C) -> Option<S>
    where
        C: Into<ValidationContext<T>>,
    {
        self.run_if(|| ctx.into())
    }
More examples
Hide additional examples
src/form/input.rs (line 229)
226
227
228
229
230
231
    fn input_state(&self, ctx: &Context<Self>) -> InputState {
        ctx.props()
            .validator
            .run_if(|| ValidationContext::from(self.value(ctx)))
            .unwrap_or_else(|| ctx.props().state)
    }
src/form/area.rs (line 202)
199
200
201
202
203
204
    fn input_state(&self, ctx: &Context<Self>) -> InputState {
        ctx.props()
            .validator
            .run_if(|| ValidationContext::from(self.value.clone()))
            .unwrap_or_else(|| ctx.props().state)
    }

Run with the provided context.

Trait Implementations§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Returns the “default value” for a type. Read more
Converts to this type from the input type.

Validators are equal if they are still None. Everything else is a change.

This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Convert self to a value of a Properties struct.
Convert self to a value of a Properties struct.
The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.
Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more