class TextBoxValues(DynamicDocument):
entity_id = StringField(max_length=200, required=True)
textbox_type = StringField(max_length=1000, required=True)
regexp = re.compile('[A-Za-z]')
entity_value = StringField(regex=regexp,max_length=None, required=True)
I was using the regex parameter to perform validation which is not working for me,it still takes input in any format, why?
The regex to provide to StringField(regex=)
should in fact be a string but it also work if you give it a compiled regex.
The problem is your regex actually. It should be regexp=r'^[A-Za-z]+$'
as @wiktor-stribiżew suggested in the comment.
The minimal example below demonstrates that the regex works as expected
from mongoengine import *
connect() # connect to 'test' database
class TextBoxValues(Document):
entity_value = StringField(regex=r'^[A-Za-z]+$')
TextBoxValues(entity_value="AZaz").save() # passes validation
TextBoxValues(entity_value="AZaz1").save() # raises ValidationError (String value did not match validation regex: ['entity_value'])
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.