Form validation in Page Processors

152 views
Skip to first unread message

Danny flack

unread,
Mar 4, 2014, 12:37:45 PM3/4/14
to mezzani...@googlegroups.com
Is it possible to validate a form in a page processor?

I know you're able to override the associated form by something like:

@processor_for('rsvp')
def rsvp_form(request, page):  
    form = RsvpForm()  #  a custom form i created to override the page's form
    return {"form": form}

But how would you go about intercepting and adding custom validation to the default page's form? 

For instance, I am trying to do something like:

@processor_for('rsvp')
def rsvp_form(request, page):  
    form = page.getAssociatedForm()  #  fabricated method to get the Page's form instance
    form.addError('Some Error')  #  add custom validation
    return {"form": form}


Am I going down the wrong path?  Is there another way to go about this?

Josh Cartmell

unread,
Mar 4, 2014, 12:45:52 PM3/4/14
to mezzani...@googlegroups.com
Hey Danny,

Check out the example at https://mezzanine.jupo.org/docs/content-architecture.html#page-processors, I think it will get you started down the right path and shows how to handle validation in a processor.


--
You received this message because you are subscribed to the Google Groups "Mezzanine Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mezzanine-use...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Danny flack

unread,
Mar 4, 2014, 1:33:43 PM3/4/14
to mezzani...@googlegroups.com
Hey Josh,

Thanks for that.  

I believe I have a grasp on validating a custom form and returning it in the page's context, but what would be the strategy when you've created a page that has the "Form" content model, where you dynamically define all the form fields in Mezzanine's admin?  Is it possible to add validation to those fields?

I've attached a screenshot where the form fields were added.

I may be going about this all wrong, but I just want to verify.

Thanks
Screenshot from 2014-03-04 10:21:10.png

Josh Cartmell

unread,
Mar 4, 2014, 1:41:02 PM3/4/14
to mezzani...@googlegroups.com
So I'm not actually sure about adding extra validation to the Form content type.  I would start by looking at the default form page processor, https://bitbucket.org/stephenmcd/mezzanine/src/ea1095020def16365c418dd3a7541635d38a2916/mezzanine/forms/page_processors.py?at=default#cl-26.

One problem you may run into is that I think that processor will always run, afaik there isn't a way to unregister a particular processor.

Danny flack

unread,
Mar 5, 2014, 2:42:45 PM3/5/14
to mezzani...@googlegroups.com
Well, I have a solution to my issue but I have a feeling its more of a hack.  Let me explain, but bear with me because I'm typing/coding with one arm as I broke my wrist last weekend and am in a cast. It's a painfully slow experience.

Problem: Be able to run extra validation on a Page's Form via a Page Processor (created by adding a new Page with a Form as the content model in Mezzanine's admin)

Solution
  • Run any extra validation necessary in the page processor
  • Reassign request.POST after removing whatever keys from the POST data which didn't validate
  • Manually add form field errors and return the form
I'm getting married and am creating a website with an RSVP page.  I added a form field to the page where the user is expected to enter the bride or groom's last name to prevent spam.  That context will hopefully shed some light on the code below.

@processor_for('rsvp')
def rsvp_form(request, page):

    if request.POST:
       
        # get the right field for secret question from the form model
        secret_field = page.form.fields.filter(label="What is Jen or Danny's last name?")
        
        if secret_field.exists():
           
            secret_field = secret_field[0]
            secret_field_post_key = 'field_%s' % secret_field.id
            secret_value = request.POST.get(secret_field_post_key)
            
            # answer should be one of our last names
            prog = re.compile('sanchez|flack', re.I)
            
            if request.POST.get(secret_field_post_key) and not prog.match(secret_value):
               
                # wrong answer to the secret question so invalidate form by removing the secret value 
                # from the post data and reassigning the data back to the request object's POST
                form_data = request.POST.copy()
                del form_data[secret_field_post_key]
                request.POST = form_data
                
                # add the error to the form
                form = FormForForm(page.form, RequestContext(request), form_data, None)
                form_error = {
                    secret_field_post_key: form.error_class(['Are you sure you know these folks?'])
                }   
                if form.errors:
                    form._errors.update(form_error)
                else:    
                    form._errors = form_error
                
                return {'form': form}
        else:   
            raise forms.ValidationError('Missing expected secret field')

    return {}




--
You received this message because you are subscribed to a topic in the Google Groups "Mezzanine Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mezzanine-users/-NvL4qHZR4s/unsubscribe.
To unsubscribe from this group and all its topics, send an email to mezzanine-use...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages