AttributeError: 'Event' object has no attribute 'key' at app/m3/_Components/TextInput/TextBox.py:149

What I’m trying to do:
I am trying to create UI by creating a pop up form for my users to fill out. And once I click on save, the data to be saved in my data base.

What I’ve tried and what’s not working:
I am getting this error and I do NOT understand why.

AttributeError: 'Event' object has no attribute 'key'
at app/m3/_Components/TextInput/TextBox.py:149

Client Side Code:

from ._anvil_designer import HomeTemplate
from anvil import *
import anvil.server
import anvil.tables as tables
import anvil.tables.query as q
from anvil.tables import app_tables
from .Company_Info_Form import Company_Info_Form

class Home(HomeTemplate):
  def __init__(self, **properties):
    # Set Form properties and Data Bindings.
    self.init_components(**properties)

    # Any code you write here will run before the form opens.

  def button_1_click(self, **event_args):
    """This method is called when the component is clicked."""
    company_info_form = Company_Info_Form()

    def save_action():
      anvil.server.call('save_company_info', Company_Info_Form.get_data())
      return True
    
    alert(
      content=company_info_form,
      large=True,
      buttons=[("Save", save_action), ("Cancel", False)]
    )
    pass 

Server Side Code

@anvil.server.callable
def save_company_info(data):
    """
    Save company information using data passed as a dictionary.
    """
    # Extract data from the dictionary
    app_tables.move_company.add_row(
        Company_ID=data.get('company_id'),
        Company_Name=data.get('company_name'),
        Email=data.get('email'),
        Phone_Number=data.get('phone_number'),
        Website=data.get('website'),
        Address=data.get('address'),
        License=data.get('license'),
        Service_Area=data.get('service_area')
    )

Company_Info_Form

class Company_Info_Form(Company_Info_FormTemplate):
  def __init__(self, **properties):
    # Set Form properties and Data Bindings.
    self.init_components(**properties)

    # Any code you write here will run before the form opens.
    data = {
            'company_id': self.text_box_company_id.text,
            'company_name': self.text_box_company_name.text,
            'contact_name': self.text_box_contact_name.text,
            'email': self.text_box_email.text,
            'phone_number': self.text_box_phone_number.text,
            'website': self.text_box_website.text,
            'address': self.text_box_address.text,
            'license': self.text_box_license.text,
            'service_area': self.text_box_service_area.text
        }

Clone link:
share a copy of your app

It’s hard to say for certain, but this sequence is suspicious. You create an instance of Company_Info_Form, but then in the server call you use the class name instead of the variable name that has the instance. I’d have expected to see:

anvil.server.call('save_company_info', company_info_form.get_data())

You also don’t show the get_data method in Company_Info_Form, so it’s hard to say what’s going on in there.

Hmm… How would you put a form in pop up and save it from another form?

I am trying to create a user form to be filled out. I am trying to access the form as a pop up from the main page, and save it to my database?

Maybe a copy of my app might help

The normal procedure is to use alert with the form instance, and after the alert comes back you do something with the form instance. Something along the lines of:

  def button_1_click(self, **event_args):
    """This method is called when the component is clicked."""
    company_info_form = Company_Info_Form()

    result = alert(
      content=company_info_form,
      large=True,
      buttons=[("Save", True), ("Cancel", False)]
    )

    if result:
        anvil.server.call('save_company_info', company_info_form.get_data())
3 Likes

@Harry_Python, with regards to the error you’re seeing, do you have steps to reproduce the error?

So I went and fixed it according to you @jshaffstall suggestion. Now I am getting another error. This is pretty frustrating, but I know it’s something stupid I am doing wrong.

TypeError: save_company_info() got an unexpected keyword argument 'Website'
at /home/anvil/downlink/anvil/_threaded_server.py:169
called from Home, line 28
called from app/m3/_Components/Button/__init__.py:39

Company_Info_Form

# Any code you write here will run before the form opens.
  def get_data(self):
        """
        Collect data from the form and return it as a dictionary.
        """
        return {
            'Company_ID': self.text_box_company_id.text,
            'Company_Name': self.text_box_company_name.text,
            'Contact_Name': self.text_box_contact_name.text,
            'Email': self.text_box_email.text,
            'Phone_Number': self.text_box_phone_number.text,
            'Website': self.text_box_website.text,
            'Address': self.text_box_address.text,
            'License': self.text_box_license.text,
            'Service_Area': self.text_box_service_area.text
        }

Server side

@anvil.server.callable
def save_company_info(data):
    """
    Save company information using data passed as a dictionary.
    """
    # Extract data from the dictionary
    app_tables.move_company.add_row(
        Company_ID=data.get('Company_ID'),
        Company_Name=data.get('Company_Name'),
        Email=data.get('Email'),
        Phone_Number=data.get('phone_Number'),
        Website=data.get('Website'),
        Address=data.get('Address'),
        License=data.get('License'),
        Service_Area=data.get('Service_Area')
    )

Main Page

  def button_1_click(self, **event_args):
    """This method is called when the component is clicked."""
    company_info_form = Company_Info_Form()
    
    result = alert(
              content=company_info_form,
              large=True,
              buttons=[("Save", True), ("Cancel", False)]
              )

    if result:
        data = company_info_form.get_data()
        anvil.server.call('save_company_info', **data)
      
    pass 

I went ahead and revised my code based on @jshaffstall suggestion. Now I am getting a different error

TypeError: save_company_info() got an unexpected keyword argument 'Website'
at /home/anvil/downlink/anvil/_threaded_server.py:169
called from Home, line 28
called from app/m3/_Components/Button/__init__.py:39

It occurs every I try to save the information from “Company_Info_Form” to the server.

That’s because you are unpacking the data dictionary as kwargs with ** rather than just sending that dictionary as one argument – save_company_info only has one argument you should be sending.

4 Likes

You sir are absolutely correct! Thank you for the second pair of eyes. :slight_smile:

1 Like

This is a problem with the M3 Text component. I opened an issue and even a PR in the M3 GitHub page for fixing this, with a few examples.

Edit: the link to the issue: Event.key error on TextBox while autofill email + password · Issue #234 · anvil-works/material-3-theme · GitHub

5 Likes

Thank you, @gabriel.duro.s for fixing the issue with the M3 Text component!

I am getting this error message for some of my users, so hope that the next release of the m3 dependency with your fix in it is just around the corner.

2 Likes