Checking many fields, how were they?

My input is pretty weirdly formatted, but according to spec:

enter image description here

The user must enter the identifier in 4 fields (!), Where id is the form 460 000 005 001, where 46 is the country code, and the right part is the application identifier. Now I want to add confirmation to this field, but I cannot do this using wtforms. Here is my form class:

class RegisterWTForm(Form): soc_sec = TextField(_('Soc security number'), [validators.Regexp('^[0-9]+$', message=_('This is not a social security number, please see the example and try again' )), validators.Required(message=_('Social security number is required')), unique_soc_sec], widget=MyTextInput()) email = TextField(_('Email'), [validators.Required(message=_('Email is required' )), validators.Email(message=_('Your email is invalid' ))], widget=MyTextInput()) def validate_soc_sec(form, field): if len(field.data) != 10: raise ValidationError(_('Soc sec must be 10 characters' )) def validate_email(form, field): if len(field.data) > 60: raise ValidationError(_('Email must be less than 60 characters' )) 

And here is how I now use variables, they are not part of WTForm, but are regular http post parameters:

 def post(self): Log1 = self.request.POST.get('Log1') Log2 = self.request.POST.get('Log2') Log3 = self.request.POST.get('Log3') Log4 = self.request.POST.get('Log4') form = RegisterWTForm(self.request.params) if form.validate(): logging.info('validated successfully') else: logging.info('form did not validate') self.render_jinja('register.html', form=form, Log1=Log1, Log2=Log2, Log3=Log3, Log4=Log4 ) return '' email = self.request.POST.get('email') sponsor_id = '%s%s%s' % (Log2, Log3, Log4) if sponsor_id: user = User.get_by_id(long(sponsor_id)) else: return 'Sponsor with sponsor id %s does not exist' % sponsor_id if not user: return 'Sponsor with sponsor id %s does not exist' % sponsor_id # make a token for a one-time login link that sets the password # Passing password_raw=password so password will be hashed # Returns a tuple, where first value is BOOL. If True ok, If False no new user is created user = self.auth.store.user_model.create_user(email) 

Is there a way to add my 4 variables as a single variable to my WTForm and add validation there, or should I do my own check for these fields? For my other fields, I made them with a red border and red text, if the field does not pass the test, and I would like to do the same here, but there will be a lot of logic in the presentation layer, if I cannot do it with the form class and add the code into the template. Can you suggest what to do?

thanks

Update

I can create a FormField that combines the fields, but they do not display correctly, and I do not need all 4 checks. Maybe you can tell me more how to do it the way I want?

 class SponsorWTForm(Form): log1 = IntegerField('log1', [validators.required()]) log2 = IntegerField('log2', [validators.required()]) log3 = IntegerField('log3', [validators.required()]) log4 = IntegerField('log4', [validators.required()]) class RegisterWTForm(Form): sponsor_id = FormField(SponsorWTForm) 

...

enter image description here

+4
source share
1 answer

WTForms seems to solve this using field wrappers . An example of this link is a phone number with separate fields of an area code and a field number, which is very similar to your use of several identification numbers.

To display the subform correctly, you will need to define your own widget. Check out the source code for ListWidget . Your widget will probably be similar, but simpler (since I think you just need the space between the different fields, not the HTML tags). Something as simple as

 return HTMLString(u' '.join([subfield() for subfield in field])) 

can satisfy your needs. Then put widget=SingleLineWidget() (or whatever you call your widget class) in the arguments to the FormField constructor.

A little off topic, the appropriate way to do this in Django Forms is quite different: you define custom cleanup and validation methods that transform your data (i.e., in this case, combining the four ID fields into one).

+5
source

Source: https://habr.com/ru/post/1395360/


All Articles