Thanks Bruno and Anthony, that worked. For others who might need this, this 
is my code:

 def validate_my_form(form):
    destination = form.vars.destination
    log.error("call_channel='%s'" % (form.vars.call_channel))
    if form.vars.call_channel == VOICEMAIL_CHANNEL:
        _, err_msg = IS_EMAIL()(destination)
        if err_msg: form.errors.destination = err_msg
    elif form.vars.call_channel == PHONE_CHANNEL:
        _, err_msg = IS_MATCH('\+[0-9]+', error_message=T('Enter a phone 
number, in international format'))(destination)
        if err_msg: form.errors.destination = err_msg
    elif form.vars.call_channel == SIP_CHANNEL:
        _, err_msg = IS_EMAIL(error_message=T('Enter a valid SIP address'))(
destination)
        if err_msg: form.errors.destination = err_msg
    else:
        form.errors.call_channel = "call_channel=%s not supported" % (form.
vars.call_channel)


On Tuesday, September 4, 2012 9:55:20 PM UTC+2, Anthony wrote:
>
> For phone and sip address, you could use the IS_MATCH validator with an 
> appropriate regular expression defined. Or you could create a custom 
> validator.
>
> Bruno covered your other question.
>
> Anthony
>
> On Tuesday, September 4, 2012 3:09:42 PM UTC-4, Daniel Gonzalez wrote:
>>
>> Hi,
>>
>> In my application I have a form with (among other data), the following 
>> two fields:
>>
>>    - channel: a set with three possible values: "voicemail", 
>>    "sipaddress" or "phone".
>>    - destination: a string, the meaningf of which depends on the value 
>>    of channel.
>>
>> I have the problem that I attach a validation rule ("require") to the 
>> destination field, because the validation rule depends on the value of 
>> "channel"
>>
>> What I would like to do is to leave the validation rule of the 
>> destination empty, and then do a manual validation, depending on the value 
>> of "channel".
>> The rest of the fields would be validated as usual, with form.process(), 
>> but the destination would be validated manually.
>>
>> Something like this:
>>
>> if form.process().accepted:
>>     if request.var.channel == 'voicemail': validate requests.var.destination 
>> as a mail address.
>>     elif request.var.channel == 'sipaddress' validate 
>> requests.var.destination 
>> as a sip address.
>>     elfi request.var.channel == 'phone' validate requests.var.destination 
>> as a telephone number
>>
>>
>> I have two questions:
>>
>>    - has web2py suppot for validation of telephone numbers and sip 
>>    addresses?
>>    - how can I manually apply validation rules, so that I can apply 
>>    complex algorithms as the one described while validating?
>>
>> Thanks
>> Daniel
>>
>

-- 



Reply via email to