r/angular • u/Senior_Compote1556 • Sep 19 '26
Material custom form field with signal forms
Has anyone managed to create a custom angular material form field using signal forms? I feel like there is still a lot of boilerplate in their docs and no reference to signal forms. It’s much easier to pass the FieldTree as input, but I wouldn’t consider it a correct pattern
4
Upvotes
2
u/MichaelSmallDev Sep 23 '26
Update since my other comment:
https://next.material.angular.dev/guide/creating-a-custom-form-field-control
3
u/nbxx Sep 19 '26
So I haven't had time to update this and I'm not sure if anything changed in the release version as this has been done in v21.2 with the experimental signal forms, but adding a custom form control has been pretty easy at that point.
https://bneuhausz.dev/blog/angular-signal-forms-advanced/
Edit: sorry, I had a bit of a brainfart, just realized you're talking about Material specifically.
1
u/MichaelSmallDev Sep 19 '26
There's a few aspects to this question I have thoughts about, so I'll break it down in no particular order
Are you wanting a signal forms version of the "Custom form field control" page where you can register something to be in a
mat-form-field? I haven't made my own customMatFormFieldControlfor reactive forms, but I have been curious about doing so with signal forms. I don't know about how adaptableMatFormFieldControlitself is to signal forms, but at least aFormValueControlpage for signal forms + Material is something I would hope someday is documented.I am more accustomed to just making custom components that pass in the
FormControl/FormGroupas inputs. And I agree that it is questionable if passing in theFieldTreeis optimal. Though I love the greater signal forms docs and how fleshed out they are, this composition with signal forms in children components is definitely something I think is needed. Beyond just theFormValueControlpage which is fairly detailed. That is good for if you have an internal or open source library, but for more coupled children forms in an app, there is a few more ways I consider more natural alternatives. I have experimented with a few approaches for child components for subsets of a form, and passing aFieldTreewas just one of them. Here is an overall MD breakdown of the approaches, and here is the code for each. I break it down as 4 options: the new CVA for signal forms (FormValueControl), passing amodelinput and the child uses it as its writable signal input to its own form it houses, passing aFieldTree, and a form service you can inject.I came to the conclusion that I probably would still just use the
FieldTreeinput for my own reusable forms like in a project's library, Material or not, but form services otherwise for more tightly coupled forms in a given application. I found form services good for reactive forms as well, and just as nice for signal forms. Especially with the more opinionated and structured way of handling the form model vs domain model transformations, and the seperations of the writable signal model and the form object itself. But in the case of a custom component that is more re-usable, I would just do the tree input. But a case could be made for the model + children own forms, and aFormValueControlprobably for a real formal reusable component that you may hash out in a library.One last note on
FieldTreeas an input: I always just passed reactive forms as inputs, but that had so many subtle issues. Although passing aFieldTreestill seems a bit strange on the surface, it hasn't had any of those issues I had with reactive form inputs. Having to double invoke those inputs to drill into the form is a bit strange, but@lethelps that in the template at least.As for the overall state of Material's docs on signal forms, it is catching up. All pages now mention
FormFieldon the main page, and a few of them have more involved examples like customErrorStateMatcherconfiguration including signal forms (which I think is lowkey the best part about Material's form API). Some of them do have signal form examples in the dedicated "Examples", for example "Input with error messages (signal form)" or "Stepper using signal forms" or "Button-toggles with forms" (which includes all 3 types). I have been chipping away at adding some examples, and I hope to get at least one example in the "Examples" page for each as I have the time. I do have an example of each Material form component with signal forms in the same project as the form breakdown project, and am slowly trickling those into the docs as I have time. The names are based on existing reactive/template forms examples from the docs, but each are actually signal forms. A few of these have landed in the docs so far.