Add validator if another field has a specific value

66 views
Skip to first unread message

Daniel Crisan

unread,
Jun 5, 2013, 12:02:11 PM6/5/13
to we...@googlegroups.com
Hello,

I have a form with 2 inputs (textboxes, checkboxes, doesn't matter) and I need to add a validator for another field only if one of the inputs is equal to some value.

Is this possible?

--
This message is confidential. It may also be privileged or otherwise protected by work product immunity or other legal rules. If you have received it by mistake please let me know by reply and then delete it from your system; you should not copy the message or disclose its contents to anyone.

Matteo Landi

unread,
Jun 6, 2013, 6:20:02 AM6/6/13
to we...@googlegroups.com
On Wed, Jun 5, 2013 at 6:02 PM, Daniel Crisan <crisan...@gmail.com> wrote:
> Hello,
>
> I have a form with 2 inputs (textboxes, checkboxes, doesn't matter) and I
> need to add a validator for another field only if one of the inputs is equal
> to some value.
>
> Is this possible?

Sure, by using a general form validator. Have a look at the following snippet:

signup = form.Form(
form.Textbox('username'),
form.Password('password'),
form.Password('password_again'),
validators = [form.Validator("Passwords didn't match.", lambda i:
i.password == i.password_again)]
)

As you can see, when you create a form you can pass an additional list
of which that can work on the form as whole (i.e. access all the
registered fields); there shouldn't be too hard to adapt the previous
example to your specific use case.

Please, get back to us if you need further help.


Cheers,

Matteo


>
> --
> This message is confidential. It may also be privileged or otherwise
> protected by work product immunity or other legal rules. If you have
> received it by mistake please let me know by reply and then delete it from
> your system; you should not copy the message or disclose its contents to
> anyone.
>
> --
> You received this message because you are subscribed to the Google Groups
> "web.py" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to webpy+un...@googlegroups.com.
> To post to this group, send email to we...@googlegroups.com.
> Visit this group at http://groups.google.com/group/webpy?hl=en.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

Daniel Crisan

unread,
Jun 6, 2013, 6:49:28 AM6/6/13
to we...@googlegroups.com
Yes, you are right but that is the case when you can compare the value from one field with the one from another field. It's a bit complicated. There are more than 2 fields and different cases like:
 if field_1 == '"some value"'
       if field_2 == "some value"
              field_3 must be ....
       if field_2 == "other value"
              field_3 must be.....

Anyway, solved the issue (don't have the code here but as I recall it it goes something like):

args = ()
args = args + (form inputs.....,)

class page():
     def __init__(self): - will define validators and the form as class variables
         self.v=[] - this will hold the validators
         self.myform =form.Form(validators=self.v, *args)
     def GET()
          define the classic GET here
     def POST():
          pdata = web.input(checkboxes_array_with_same_name=[])
          if not self.myform.validates(pdata):
               return render.page(self.myform)
          else:
               if pdata.some_input == do all the checks here
                        if pdata.some_other_input - do some other checks and if necessary make another if
                              self.v.append(form.Validator("error message", False)
                              self.myform = form.Form(validators=self.v, *args)
                              return render.page(self.myform)
               elif .....add additional restrictions based on any post data received 
               else:
                      code for form valid

I don't see anything wrong with this approach, it loops the same page until the form data is valid. Do you see something wrong?

Matteo Landi

unread,
Jun 10, 2013, 3:38:53 PM6/10/13
to we...@googlegroups.com
On Thu, Jun 6, 2013 at 12:49 PM, Daniel Crisan <crisan...@gmail.com> wrote:
> Yes, you are right but that is the case when you can compare the value from
> one field with the one from another field. It's a bit complicated. There are
> more than 2 fields and different cases like:
> if field_1 == '"some value"'
> if field_2 == "some value"
> field_3 must be ....
> if field_2 == "other value"
> field_3 must be.....

I don't see why you shouldn't be able to implement this using a form validator.
There is nothing wrong with that besides a less maintainable
controller; compare the snippet above with one missing the the else
branch (all the validation logic has been segregated into the form
validator): you will agree with me that the resulting controller
would be easier to read and maintain.


Cheers,

Matteo
Reply all
Reply to author
Forward
0 new messages