ngx-schema-form icon indicating copy to clipboard operation
ngx-schema-form copied to clipboard

Can indicate required for widget?

Open vellengs opened this issue 7 years ago • 3 comments

how to show a required(*) indicate in widget?

export declare abstract class Widget<T extends FormProperty> {
    formProperty: T;
    control: FormControl;
    errorMessages: string[];
    id: string;
    name: string;
    schema: any;
}

Maybe better add a property say 'required' here;

vellengs avatar Oct 29 '17 14:10 vellengs

I asked myself the same question :) You can write something like this as a work around.

import { Component, OnInit } from "@angular/core";
import { Validators } from "@angular/forms";
import { ControlWidget } from "angular2-schema-form";
import { WidgetService } from "../../services";

@Component({
	selector: "sf-string",
	templateUrl: "./sf-string.component.html",
})

export class FSStringWidget extends ControlWidget implements OnInit {

	public isRequired: boolean = false;

	constructor(
		private widgetService: WidgetService
	) {
		super();
	}

	public getInputType(): string {
		if (!this.schema.widget.id || this.schema.widget.id === "string") {
			return "text";
		} else {
			return this.schema.widget.id;
		}
	}

	public ngOnInit(): void {
		this.isRequired =  this.widgetService.isRequired(this.formProperty);
	}
}

export class WidgetService {

	public isRequired(formProperty: any): boolean {
		const requiredFields = formProperty.parent.schema.required || [];
		const fieldPath = formProperty.path;
		const controlName = fieldPath.substr(fieldPath.lastIndexOf("/") + 1);

		return requiredFields.indexOf(controlName) > -1;
	}
}

glennverschooren avatar Nov 06 '17 15:11 glennverschooren

@glennverschooren Thank you for good implement. But maybe official supported at base widget class. then needn't implement

	public ngOnInit(): void {
		this.isRequired =  this.widgetService.isRequired(this.formProperty);
	}

every widget.

vellengs avatar Nov 06 '17 15:11 vellengs

@vellengs

I totally agree that should be officially supported! 👍

glennverschooren avatar Nov 06 '17 15:11 glennverschooren