11import { ChangeDetectionStrategy , ChangeDetectorRef , Component , DestroyRef , inject , OnInit } from '@angular/core' ;
2- import { FormControl , FormGroup , ReactiveFormsModule , Validators } from '@angular/forms' ;
2+ import { FormControl , FormGroup , ReactiveFormsModule , ValidatorFn , Validators } from '@angular/forms' ;
33import { ToastrService } from 'ngx-toastr' ;
44import { take } from 'rxjs/operators' ;
55import { ServerService } from 'src/app/_services/server.service' ;
@@ -62,7 +62,7 @@ export class ManageSettingsComponent implements OnInit {
6262 this . settingsForm . addControl ( 'taskScan' , new FormControl ( this . serverSettings . taskScan , [ Validators . required ] ) ) ;
6363 this . settingsForm . addControl ( 'taskBackup' , new FormControl ( this . serverSettings . taskBackup , [ Validators . required ] ) ) ;
6464 this . settingsForm . addControl ( 'taskCleanup' , new FormControl ( this . serverSettings . taskCleanup , [ Validators . required ] ) ) ;
65- this . settingsForm . addControl ( 'ipAddresses' , new FormControl ( this . serverSettings . ipAddresses , [ Validators . required , Validators . pattern ( ValidIpAddress ) ] ) ) ;
65+ this . settingsForm . addControl ( 'ipAddresses' , new FormControl ( this . serverSettings . ipAddresses , [ this . emptyOrPattern ( ValidIpAddress ) ] ) ) ;
6666 this . settingsForm . addControl ( 'port' , new FormControl ( this . serverSettings . port , [ Validators . required ] ) ) ;
6767 this . settingsForm . addControl ( 'loggingLevel' , new FormControl ( this . serverSettings . loggingLevel , [ Validators . required ] ) ) ;
6868 this . settingsForm . addControl ( 'allowStatCollection' , new FormControl ( this . serverSettings . allowStatCollection , [ Validators . required ] ) ) ;
@@ -77,6 +77,7 @@ export class ManageSettingsComponent implements OnInit {
7777 this . settingsForm . addControl ( 'onDeckProgressDays' , new FormControl ( this . serverSettings . onDeckProgressDays , [ Validators . required ] ) ) ;
7878 this . settingsForm . addControl ( 'onDeckUpdateDays' , new FormControl ( this . serverSettings . onDeckUpdateDays , [ Validators . required ] ) ) ;
7979
80+
8081 // Automatically save settings as we edit them
8182 this . settingsForm . valueChanges . pipe (
8283 distinctUntilChanged ( ) ,
@@ -186,4 +187,19 @@ export class ManageSettingsComponent implements OnInit {
186187 console . error ( 'error: ' , err ) ;
187188 } ) ;
188189 }
190+
191+ emptyOrPattern ( pattern : RegExp ) : ValidatorFn {
192+ return ( control ) => {
193+ if ( ! control . value || control . value . length === 0 ) {
194+ return null ;
195+ }
196+
197+ if ( pattern . test ( control . value ) ) {
198+ return null ;
199+ }
200+
201+ return { 'emptyOrPattern' : { 'requiredPattern' : pattern . toString ( ) , 'actualValue' : control . value } } ;
202+ }
203+ }
204+
189205}
0 commit comments