Capture optional element with css and post later if present

Hi,

I have an html response that may or may not contain a particular element. When it’s there I capture it by css selector and save it to session. Subsequently I can retrieve it from session and post it with .formParam using EL.

How can I deal with the situation when the element is legitimately not there? I’ve been looking at checkIf, formParam’s overloads, doIf (which is not what I want - I would like to keep things just to http scope).

I’ve looked at examples and documentation but can’t see how to do it.

Thanks,
Tom

iress.com

Tom Abnett​

Technical Lead

Office: +441926621445

tom.abnett@iress.com

www.iress.com

1 Kingmaker Court, Warwick Technology Park, Gallows Hill,
Warwick, Warwickshire, CV34 6DY

The contents of this email originated from Iress. For this purpose Iress includes Iress Limited and/or any of its subsidiaries, holding companies and trading entities.
​If you have received this email in error please notify the sender immediately and delete this email.

nosig

There are many ways to deal with this:

It really depends on what you want to do when value is missing.

Hi Stéphane,

Thanks for your quick response, as ever.

And thank you for the first tip - I’ve changed to using .check(css(…).optional.saveAs(…)) which is writing into session when present and not when missing, which is perfect.

But then when I’m building up a post back I want to submit an optional formParam only if it’s there in session. This is from a helper method where I create the http action but don’t have session available.

Am I right that doIf operates within an exec ‘chain’, rather than within an http ‘chain’ - i.e. doIf is operating at a level above?

I’m looking for something like
rq = rq
.formParam(“MandParam1”, “${MandValue1}”)
.formParam(“MandParam2”, “${MandParam2}”)
.formParamIf("${OptParam.exists()}", “OptParamKey”, “${OptParamValue}”)

(so that I don’t get “No attribute named ‘…’ is defined”), or any way I can achive this by chaining onto an http(…) action.

Thanks,
Tom

No, that’s not possible.
Then, are you sure you shouldn’t be using the form check to capture the whole form, including fields that might or might not be here?

I’ve not actually looked at using form to capture the whole lot. I’ll take a look.
Thanks.