-2

I am trying to place a restriction on the minimum number of characters that a subject can use in a form field using Otree. I want that if the subjects put a name that is less than 4 characters long, the application shows them an error that says they must write a longer word.

I am using len but I am getting the following error:

'NoneType' has no len()

Can someone help me figure out what is wrong in the code and help me fix it?

This is my pages.py code.


class Consentimiento(Page):

    form_model = 'player'
    form_fields = ['consentimiento', 'consentimienton',]

    def consentimiento_error_message(self, value):
        print('El nombre es', value)
        if len(self.player.consentimiento)) < Constants.number:
            return 'Por favor en el campo de nombre debe poner mínimo 4 letras'
 

This is my models.py code

consentimienton =  models.StringField( max_length=50 )
Amir Afianian
  • 2,679
  • 4
  • 22
  • 46

1 Answers1

0

I noticed several errors in your working example.

  • len(self.player.consentimiento) is checking the data, not the input provided by the user. You should replace it with len(value). (Also mind the extra paranthesis)

  • You are using fieldname_error_message(self,value) function. This should be defined:

    • for the field, not for the page.
    • in models.py under your model, not in pages.py.

So in the end you should have the following in models.py:

# Don't forget to define number field in Constants

class Player(BasePlayer):
    consentimienton =  models.StringField( max_length=50 )

    def consentimienton_error_message(self, value):
        print('El nombre es', value)
        if len(value) < Constants.number:
            return 'Por favor en el campo de nombre debe poner mínimo 4 letras'


and this in your pages.py:

class Consentimiento(Page):
    form_model = 'player'
    form_fields = ['consentimienton']
s0-0s
  • 134
  • 10