Skip to content

Commit

Permalink
Display error for required fields without value in current language (#…
Browse files Browse the repository at this point in the history
…2314)

* Display error for required fields without value in current language

* Remove unnecessary import

* Changelog
  • Loading branch information
xispa authored May 27, 2023
1 parent 17cdd38 commit bd647c1
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Changelog
2.5.0 (unreleased)
------------------

- #2314 Display error for required fields without value in current language
- #2313 Log error when calculation fails
- #2310 Added `get_relative_delta` and `get_tzinfo` in datetime API
- #2311 Properly process and validate field values from sample header on submit
Expand Down
31 changes: 29 additions & 2 deletions src/bika/lims/browser/analysisrequest/add2.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
from plone.memoize.volatile import DontCache
from plone.memoize.volatile import cache
from plone.protect.interfaces import IDisableCSRFProtection
from Products.Archetypes.interfaces import IField
from Products.CMFPlone.utils import safe_unicode
from Products.Five.browser import BrowserView
from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile
Expand All @@ -52,6 +53,7 @@
from zope.component import getAdapters
from zope.component import queryAdapter
from zope.i18n.locales import locales
from zope.i18nmessageid import Message
from zope.interface import alsoProvides
from zope.interface import implements
from zope.publisher.interfaces import IPublishTraverse
Expand Down Expand Up @@ -1557,6 +1559,30 @@ def ajax_recalculate_prices(self):

return prices

def get_field(self, field_name):
"""Returns the field from the temporary sample with the given name
"""
if IField.providedBy(field_name):
return field_name

for field in self.get_ar_fields():
if field.getName() == field_name:
return field
return None

def get_field_label(self, field):
"""Returns the translated label of the given field
"""
field = self.get_field(field)
if not field:
return ""

instance = self.get_ar()
label = field.widget.Label(instance)
if isinstance(label, Message):
return self.context.translate(label)
return label

def check_confirmation(self):
"""Returns a dict when user confirmation is required for the creation of
samples. Returns None otherwise
Expand Down Expand Up @@ -1701,8 +1727,9 @@ def ajax_submit(self):
# If there are required fields missing, flag an error
for field in missing:
fieldname = "{}-{}".format(field, num)
msg = _("Field '{}' is required").format(safe_unicode(field))
fielderrors[fieldname] = msg
label = self.get_field_label(field)
msg = self.context.translate(_("Field '{}' is required"))
fielderrors[fieldname] = msg.format(label)

# Process and validate field values
valid_record = dict()
Expand Down

0 comments on commit bd647c1

Please sign in to comment.