11import {
2+ AfterViewInit ,
23 Component ,
34 Inject ,
5+ ViewChild ,
46} from '@angular/core' ;
7+ import {
8+ AbstractControl ,
9+ ValidationErrors ,
10+ } from '@angular/forms' ;
511import { JsonPatchOperationPathCombiner } from '@dspace/core/json-patch/builder/json-patch-operation-path-combiner' ;
612import { JsonPatchOperationsBuilder } from '@dspace/core/json-patch/builder/json-patch-operations-builder' ;
713import { SubmissionSectionError } from '@dspace/core/submission/models/submission-section-error.model' ;
@@ -40,6 +46,8 @@ import { SectionFormOperationsService } from '../form/section-form-operations.se
4046import { SectionModelComponent } from '../models/section.model' ;
4147import { SectionDataObject } from '../models/section-data.model' ;
4248import { SectionsService } from '../sections.service' ;
49+
50+
4351/**
4452 * This component represents the submission section to select the Creative Commons license.
4553 */
@@ -53,7 +61,7 @@ import { SectionsService } from '../sections.service';
5361 ] ,
5462 standalone : true ,
5563} )
56- export class SubmissionSectionCustomUrlComponent extends SectionModelComponent {
64+ export class SubmissionSectionCustomUrlComponent extends SectionModelComponent implements AfterViewInit {
5765
5866 /**
5967 * The form id
@@ -103,6 +111,13 @@ export class SubmissionSectionCustomUrlComponent extends SectionModelComponent {
103111 */
104112 redirectedUrls : string [ ] = [ ] ;
105113
114+ private readonly errorMessagePrefix = 'error.validation.custom-url.' ;
115+
116+ /**
117+ * The FormComponent reference
118+ */
119+ @ViewChild ( 'formRef' ) public formRef : FormComponent ;
120+
106121 constructor (
107122 protected sectionService : SectionsService ,
108123 protected operationsBuilder : JsonPatchOperationsBuilder ,
@@ -181,7 +196,6 @@ export class SubmissionSectionCustomUrlComponent extends SectionModelComponent {
181196 * the section data retrieved from the server
182197 */
183198 initForm ( sectionData : WorkspaceitemSectionCustomUrlObject ) : void {
184- const model =
185199 this . formModel = [
186200 new DynamicInputModel ( {
187201 id : 'url' ,
@@ -198,6 +212,42 @@ export class SubmissionSectionCustomUrlComponent extends SectionModelComponent {
198212 this . updateSectionData ( sectionData ) ;
199213 }
200214
215+ customUrlValidator = ( _ : AbstractControl ) : ValidationErrors | null => {
216+ if ( this . sectionData . errorsToShow ?. length ) {
217+ const urlErrors = this . sectionData . errorsToShow . map ( ( error ) =>
218+ error . message . replace ( this . errorMessagePrefix , '' ) ) ;
219+ const validationErrors : ValidationErrors = { } ;
220+
221+ urlErrors . forEach ( ( error ) => {
222+ validationErrors [ error ] = true ;
223+ } ) ;
224+ return validationErrors ;
225+ }
226+ return null ;
227+ } ;
228+
229+ /**
230+ * Update control status
231+ * @param addValidator
232+ */
233+ updateControlStatus ( addValidator = false ) : void {
234+ const control = this . formRef ?. formGroup ?. get ( 'url' ) ;
235+ if ( control ) {
236+ if ( addValidator ) {
237+ control . addValidators ( this . customUrlValidator ) ;
238+ // reset errors on user input
239+ this . subs . push ( control . valueChanges . subscribe ( ( ) => {
240+ control . setErrors ( null ) ;
241+ } ) ) ;
242+ }
243+ control . updateValueAndValidity ( { onlySelf : true , emitEvent : false } ) ;
244+ }
245+ }
246+
247+ ngAfterViewInit ( ) : void {
248+ this . updateControlStatus ( true ) ;
249+ }
250+
201251 /**
202252 * When an information is changed build the formOperations
203253 * If the submission scope is in EditItem also manage redirected-urls formOperations
@@ -250,6 +300,7 @@ export class SubmissionSectionCustomUrlComponent extends SectionModelComponent {
250300 if ( isNotEmpty ( errors ) || isNotEmpty ( this . sectionData . errorsToShow ) ) {
251301 this . sectionService . checkSectionErrors ( this . submissionId , this . sectionData . id , this . formId , errors , this . sectionData . errorsToShow ) ;
252302 this . sectionData . errorsToShow = errors ;
303+ this . updateControlStatus ( true ) ;
253304 }
254305 } ) ,
255306 ) ;
0 commit comments