I am currently working on a field validator/invariant for fields in a custom content type created. Currently, the Invariant Error is not displaying correctly in Volto. It shows up as the following:
Here is the code for the invariant i am currently using in Plone classic backend:
class CheckFields(Invalid):
__doc__ = _(u'This field is required')
class IFinancialAidDocs(model.Schema):
# Fields go here!!
@invariant
def validate_attachment(data):
if data.attachment_type is None and data.attachment_file is not None:
raise CheckFields(_(u'Required input for Attachments Type 1 is missing'))
if data.attachment_type is not None and data.attachment_file is None:
raise CheckFields(_(u'Required input for Attachments File 1 is missing'))
@invariant
def validate_attachment1(data):
if data.attachment_type1 is None and data.attachment_file1 is not None:
raise CheckFields(_(u'Required input for Attachments Type 2 is missing'))
if data.attachment_type1 is not None and data.attachment_file1 is None:
raise CheckFields(_(u'Required input for Attachments File 2 is missing'))
@invariant
def validate_attachment2(data):
if data.attachment_type2 is None and data.attachment_file2 is not None:
raise CheckFields(_(u'Required input for Attachments Type 3 is missing'))
if data.attachment_type2 is not None and data.attachment_file2 is None:
raise CheckFields(_(u'Required input for Attachments File 3 is missing'))
@invariant
def validate_attachment3(data):
if data.attachment_type3 is None and data.attachment_file3 is not None:
raise CheckFields(_(u'Required input for Attachments Type 4 is missing'))
if data.attachment_type3 is not None and data.attachment_file3 is None:
raise CheckFields(_(u'Required input for Attachments File 4 is missing'))
@invariant
def validate_attachment4(data):
if data.attachment_type4 is None and data.attachment_file4 is not None:
raise CheckFields(_(u'Required input for Attachments Type 5 is missing'))
if data.attachment_type4 is not None and data.attachment_file4 is None:
raise CheckFields(_(u'Required input for Attachments File 5 is missing'))
How can I get the invariant to display in Volto as well as Plone Classic?
Granted, we could improve the human readability on this kind of messages. Right now, it shows what the server responds: returns a 400 since it had the validation errors, and it shows it to you in a toast.
there is no control panel setting similar in classic to see if Volto is even aware other than that it appears as active under add-ons
On the React side of things, do we need to hook into clamscan? or do we need some sort of endpoint in collective.clamav? Any comments, suggestions, are greatly appreciated.
I think one of the issues is that plone.restapi (AFAIK) doesn't use schemas to define its endpoint operations, which would allow the registration of validators over the input values. CC @sneridagh@tisto
Right, the clamav validator should be implemented on the schema, not on the z3c.form. This would mean to modify the File/Image content types FTI, but IMO it is the clean way.
I had hypothesized that something was amiss with the way this add-on (and some others) especially forms were not validating properly.
Being of limited skill (programmatically) I will see if I can modify locally to fix. Any thoughts in the Volto camp on what path form validation will take? will there be an associated library like z3c.from to take advantage of?