ngx-schema-form
ngx-schema-form copied to clipboard
Can indicate required for widget?
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;
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 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
I totally agree that should be officially supported! 👍