### File

`src/lib/validators/is-selectable-value.directive.ts`

### Extends

`ExtractOptionsDataSourceMixin`

### Implements

`Validator`

### Metadata

Providers | `{ provide: NG_VALIDATORS, multi: true, useExisting: forwardRef(() => IsSelectableValueDirective), }`
Selector | `[rxapIsSelectableValue]`
Standalone | `true`

### Index

###### Properties

- Protected Optional dataSource
- Protected Readonly dataSourceLoader
- Protected Readonly injector
- Protected Optional metadata
- Protected Optional settings
- Protected Optional useDataSourceValue
- Protected Optional viewer

###### Methods

- Public validate
- Protected extractOptionsDatasource

###### Inputs

- viewer

### Constructor

`constructor(dataSourceLoader: DataSourceLoader, injector: Injector)`
Defined in src/lib/validators/is-selectable-value.directive.ts:43

Parameters :

Name | Type | Optional
dataSourceLoader | `DataSourceLoader` | No
injector | `Injector` | No

### Inputs

viewer
Type : `BaseDataSourceViewer`
Default value : `{ id: '[rxapIsSelectableValue]' }`
Defined in src/lib/validators/is-selectable-value.directive.ts:43

### Methods

Public validate
`validate(control: AbstractControl)`
Defined in src/lib/validators/is-selectable-value.directive.ts:53

Parameters :

Name | Type | Optional
control | `AbstractControl` | No

Returns : `ValidationErrors | null`

Protected extractOptionsDatasource
`extractOptionsDatasource(control?: AbstractControl)`
Inherited from `ExtractOptionsDataSourceMixin`
Defined in `ExtractOptionsDataSourceMixin:76`

Parameters :

Name | Type | Optional
control | `AbstractControl` | Yes

Returns : `BaseDataSource<ControlOptions | Record>`

### Properties

Protected Optional dataSource
Type : `BaseDataSource<ControlOptions | Record>`
Inherited from `ExtractOptionsDataSourceMixin`
Defined in `ExtractOptionsDataSourceMixin:70`

Protected Readonly dataSourceLoader
Type : `DataSourceLoader`
Inherited from `ExtractOptionsDataSourceMixin`
Defined in `ExtractOptionsDataSourceMixin:64`

Protected Readonly injector
Type : `Injector`
Inherited from `ExtractOptionsDataSourceMixin`
Defined in `ExtractOptionsDataSourceMixin:72`

Protected Optional metadata
Type : `BaseDataSourceMetadata`
Inherited from `ExtractOptionsDataSourceMixin`
Defined in `ExtractOptionsDataSourceMixin:66`

Protected Optional settings
Type : `UseOptionsDataSourceSettings<any>`
Inherited from `ExtractOptionsDataSourceMixin`
Defined in `ExtractOptionsDataSourceMixin:62`

Protected Optional useDataSourceValue
Type : `UseDataSourceValue<ControlOptions | Record>`
Inherited from `ExtractOptionsDataSourceMixin`
Defined in `ExtractOptionsDataSourceMixin:74`

Protected Optional viewer
Type : `BaseDataSourceViewer`
Inherited from `ExtractOptionsDataSourceMixin`
Defined in `ExtractOptionsDataSourceMixin:68`

    import {
      AbstractControl,
      NG_VALIDATORS,
      ValidationErrors,
      Validator,
    } from '@angular/forms';
    import {
      Directive,
      forwardRef,
      Inject,
      INJECTOR,
      Injector,
      Input,
      isDevMode,
    } from '@angular/core';
    import {
      BaseDataSourceViewer,
      DataSourceLoader,
    } from '@rxap/data-source';
    import { Mixin } from '@rxap/mixin';
    import { ControlOptions } from '@rxap/utilities';
    import { ExtractOptionsDataSourceMixin } from '../mixins/extract-options-data-source.mixin';

    // eslint-disable-next-line @typescript-eslint/no-empty-interface
    export interface IsSelectableValueDirective extends ExtractOptionsDataSourceMixin {
    }

    @Mixin(ExtractOptionsDataSourceMixin)
    @Directive({
      selector: '[rxapIsSelectableValue]',
      providers: [
        {
          provide: NG_VALIDATORS,
          multi: true,
          useExisting: forwardRef(() => IsSelectableValueDirective),
        },
      ],
      standalone: true,
    })
    export class IsSelectableValueDirective implements Validator {

      @Input()
      public viewer: BaseDataSourceViewer = { id: '[rxapIsSelectableValue]' };

      constructor(
        @Inject(DataSourceLoader)
        protected readonly dataSourceLoader: DataSourceLoader,
        @Inject(INJECTOR)
        protected readonly injector: Injector,
      ) {
      }

      public validate(control: AbstractControl): ValidationErrors | null {
        if (control.value) {
          this.extractOptionsDatasource(control);
          const data = this.useDataSourceValue?.lastValue;
          if (data) {
            let options: ControlOptions;
            if (Array.isArray(data)) {
              options = data;
            } else {
              options = Object.entries(data)
                              .map(([ value, display ]) => ({
                                value,
                                display,
                              }));
            }
            if (!options.some(option => option.value === control.value)) {
              return {
                isSelectableValue: {
                  expected: 'Value should be from the list of provided options',
                },
              };
            }
          } else if (isDevMode()) {
            console.warn('The last value from the OptionsDataSource is empty', this);
          }
        }
        return null;
      }

    }

# results matching ""

# No results matching ""