# Class: Validator

## Validator

#### new Validator($form, userOptions)

  Validate a form, a fieldset or whatever had inputs inside

  ##### Parameters:

  Name | Type | Description
  --- | --- | ---
  `$form` | HTMLElement |
  `userOptions` | Object | ###### Properties
  Name | Type | Argument | Default | Description
  --- | --- | --- | --- | ---
  `fields` | String | <optional> | input,textarea,select |
  `filter` | String |  | input[type=\"button\"], | input[type=\"submit\"], input[type=\"reset\"], input[type=\"image\"]]
  `customErrorLabelPrefix` | String | <optional> | data-error-label |
  `errorMessages` | Object | <optional> | {} |
  `validatorsOptions` | Object | <optional> |  |
  `onValidate` | Callback | <optional> | data => {} |
  `onInvalidate` | Callback | <optional> | data => {} |
  `liveValidation` | Object | <optional> |  | ###### Properties
  Name | Type | Argument | Description
  --- | --- | --- | ---
  `onValidate` | function | <optional> |
  `onInvalidate` | function | <optional> |
  `eventsName` | Object | <optional> | ###### Properties
  Name | Type | Argument | Description
  --- | --- | --- | ---
  `optin` | String | <optional> |
  `select` | String | <optional> |
  `inputText` | String | <optional> |
  `eventsHook` | Object | <optional> |

  See:
    - extra/modules/validator.md for details

  ##### Example

      let validator = new Validator( $form, options )

      on( $form, {
           "eventsName": "submit",
           "callback": e => {
              e.preventDefault();
              validator
                      .validate()
                      .then( data => {
                              console.log( 'Valid', data );
                              $form.submit();
                          }
                      )
                      .catch( data => {
                              console.log( 'Invalid', data );
                              data.errors.forEach( input => {
                                      console.log( input.getErrorMessages() );
                                  }
                              )
                          }
                      );
          }
      });

### Methods

#### getAllFields(onlyValidated)

  Get all handled fields (DOM elements)

  ##### Parameters:

  Name | Type | Description
  --- | --- | ---
  `onlyValidated` | Boolean | If true, remove all fields without validator

  ##### Returns:

  Type
    Array.<HTMLElement>

#### getAllInputs()

  Get all inputs (input objects, not DOM elements)

  ##### Returns:

  Type
    Array.<Input>

#### getCheckedFields()

  Get only the fields (DOM elements) with at least one validator

  ##### Returns:

  Type
    Array.<HTMLElement>

#### getCheckedInputs()

  Get all inputs (input objects, not DOM elements) with at least one validator

  ##### Returns:

  Type
    Array.<Input>

#### getFieldValidator($field)

  Return the validator object of an input

  ##### Parameters:

  Name | Type | Description
  --- | --- | ---
  `$field` | HTMLElement |

  ##### Returns:

  Type
    Input

#### update()

  Bind or rebind all inputs

#### validate()

  Start the validation of all the form

  ##### Returns:

  Type
    Promise

#### validateField($field)

  Validate one field

  ##### Parameters:

  Name | Type | Description
  --- | --- | ---
  `$field` | HTMLElement |

  ##### Returns:

  Type
    Promise

### Type Definitions

#### Input

  ##### Type:

  - Object

  ##### Properties:

  Name | Type | Description
  --- | --- | ---
  `$input` | HTMLElement |
  `$label` | HTMLElement |
  `getErrorMessages` | function |
  `getErrors` | function |
  `hasError` | Boolean |
  `isValid` | function |
  `isLiveValidation` | Boolean |

#### Search results