Form components and inheritance. Possible?

One of the big omissions IMO in Anvil is the lack of a comprehensive set of data entry fields. By field I mean a combination of a prompt, a entryfield (like the standard textbox, a dropdown, etc), a helpline. This combined with field level plus form level validation. So maybe masks or rules as well. See https://quasar.dev/vue-components/input for just one example.
I think data entry fields would be a big productivity boost.

I am aware of the Form validation library (https://anvil.works/library/form-validation) but that library is useful but still very limited in scope.

So I set about to create some simple Fields myself. This was not easy. Every component duplicated a lot of code. The usual approach would be to create an abstract EntryField and derive other fields from this one.
However I could not get inheritance to work for Form components (and their Templates). Is it even possible?

(PS for anyone interested, I ended up creating some standard components in combination with the form validation library mentioned above. Not very elegant but it works; I think :wink:

1 Like

I suspect that mixin-style inheritance (via Python’s multiple inheritance) might help.

Thanks. Tried that, but failed. Did not know what to do with two templates (ancestor and derived form component).

maybe you can share your attempts at multiple inheritance. Inheritance in anvil should work just like it does in python.

e.g.

class A:
  def foo(self):
    return 'bar'

class Form1(Form1Template, A):
   ...

# instances of Form1 now have a foo method.

2 Likes

Yes that should work, I think.
I was messing with one Form component derived from another. That was a bit too confusing for me.
Your approach would at least get the common functionality in one non-visual class.

1 Like

Multiple inheritance also lets you factor out multiple facets, e.g., two (or more) different aspects that might vary independently from each other.

You can see an example of mixins being used within forms in my recent demo app post.

I know. It’s just that I don’t know how the FormTemplate inheritance (if any) would work.

Say I have a composite form consisting of a prompt label, a help text label, an error label (which is what all fields would have in common). Could a derived FormTemplate inherit these comps and add, say, a text box. I wouldn’t know how ATM.

1 Like

There are hundreds of examples of Anvil Forms using other Forms by composition. While I’ve seen C++ Builder Forms using other C++ Builder Forms by inheritance (and even built a few dozen), I don’t recall seeing it in Anvil.

Part of that may be the way the auto-generated base classes (FormTemplates) behave. I doubt that they were built with that style of inheritance in mind. C++ Builder forms do not have such intermediary base classes, which could easily get tripped up by another Form higher in the class hierarchy.

If Anvil officially supports the kind of inheritance you have in mind, I’d go for it. Otherwise, I’d reuse Forms by composition, not inheritance.

1 Like

3 posts were split to a new topic: Client side Python and skulpt

I tried this approach, ie use multiple inheritance to separate out non-visual functionality, and it seems to work. Thx.

2 Likes