Validating the content of fileloader immediately

What I’m trying to do:

I have 2 components that users must fill up before proceeding with completing the form. Va;idate 1 is a texbox while Validate 2 is a fileloader. I have no problem with the textbox but haven’t been able to insure that the fileloader is not empty.

What I’ve tried and what’s not working:
I have tried the codes below. While it works with textbox, it doesn’t function with the fileloader. Is it possible to reference the fileloader as empty and show the error immediately so that the user is obligated to upload an image file?

My code sample showing the functions that I created is given below.

def button_addrequired_click(self, **event_args):
    self.label_errormessage.visible = False
    error = self.validate_data()
    if error:
	self.label_errormessage.text = error
	self.label_errormessage.visible = True
	return 

# Validate 1
def validate_data (self):
  if not self.text_box_userid.text:
    return ('UserID is required.') # this error message is supposed to be printed  in a separate textbox

# Validate 2
  c = FileLoader()
  if c.file != self.item['yourpic']:
    return ('Your latest photo is required.') # this error message is supposed to be printed in a separate textbox
   
  return None # Stay in the form until the components are validated/filled

Thank you for your help. Happy New Year to All in this forum.

You’re creating a brand new file loader in that code. You should be referencing the file loader that is on your form, something like self.file_loader_1 or whatever you named it.

4 Likes

Thank you. I will check it out.

Hi @jshaffstall,

May I revisit this issue after letting it ‘hibernate’ for some time?
Following your suggestion to reference the file loader in the form, this is how I modified the validate function:

def validate_data (self):
    c = self.file_loader_validid # referencing the fileloader in the form
    user_id = self.text_box_userid.text
    if not user_id and not c :
      return ('Valid govt-issued ID and UserID are required.')
      return None
    else:
      return ("Requirements filled. You may now continue.")
      return

When I ran above codes, I get an Attribute Error as below:

AttributeError: The ‘file’ property for a FileLoader is read-only. Did you initialise all data binding sources before initialising this component? at update_binding.py::

I actually do not want to read/display the image. I only want to make sure that a photo (with a specific name given by self.item[‘photo_name’] is loaded in the container. How to refer if an image container is empty? I checked the Q&A here but could not find a way to get off this issue.

Thank you.

That error specifically refers to data bindings. Those are embedded in the properties for your form components, not in your code. Check the properties and see what data bindings you have set up in your file loader properties, specifically for the file property. Remove that data binding.

You can check to see if there is a non-None value using:

if self.item[‘photo_name’]:

Whether that actually does what you want depends on how other things are setup in the form. I haven’t seen anything in your code that would set self.item[‘photo_name’].

Thank you. I did remove the bindings earlier and the Attribute Error disappeared. However, I am still in a quandary how to check if a specific image has been uploaded. Will examine the self.item[‘photo_name’] part if I am assigning it correctly…to be updated shortly.

This is how I ended up solving my problem. Refer to the codes below. I remove the binding and the ‘self’ reference to the image object as it does nothing uniess the image is taken from the server’s data table.

 def text_box_userid_pressed_enter(self, **event_args):
    pass

  def button_addcompulsories_click(self, **event_args):
    self.label_errormessage.visible = False
    error = self.validate_data()
    
    if error:
			self.label_errormessage.text = error
			self.label_errormessage.visible = True
			return 
 
  def validate_data (self):
    user_id = self.text_box_userid.text
    c = self.file_loader_validid
    
    if user_id and c.file != None:
      return ("Requirements filled. You may now continue.")
      return
    else:
      return ('Valid govt-issued ID and UserID are required.')
      return None


## load images; ## ask upload valid officiel govt ID
  def file_loader_validid_change(self, file, **event_args):
    pass

I only referred to the Fileloader in c = self.file_loader_validid and just checked if it contains any file with c.file != None.

Thank you for the help.

Note that in

only the first return can be executed. Because it’s a return, the function never reaches the second one.

Thank you very much. I had the error fixed.
So, the final correct version of the validate function should bve:

    user_id = self.text_box_userid.text
    c = self.file_loader_validid
    
    if user_id and c.file != None:
      return ("Requirements filled. You may now continue.")
      return
    else:
      return ('Valid govt-issued ID and UserID are required.')
      return None