-
Notifications
You must be signed in to change notification settings - Fork 374
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implemenatation of chooseRadio function along with tests #1840
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -177,6 +177,7 @@ module Yesod.Test | |||||||||||||||||
, fileByLabelContain | ||||||||||||||||||
, fileByLabelPrefix | ||||||||||||||||||
, fileByLabelSuffix | ||||||||||||||||||
, chooseRadio | ||||||||||||||||||
|
||||||||||||||||||
-- *** CSRF Tokens | ||||||||||||||||||
-- | In order to prevent CSRF exploits, yesod-form adds a hidden input | ||||||||||||||||||
|
@@ -958,6 +959,40 @@ genericNameFromHTML match label html = | |||||||||||||||||
name:_ -> Right name | ||||||||||||||||||
_ -> Left $ "More than one label contained " <> label | ||||||||||||||||||
|
||||||||||||||||||
|
||||||||||||||||||
-- | Choose a given value from a radio input, identified by the value of it's corresponding label. | ||||||||||||||||||
-- This is a very simple and naive function, which could fail in more complex scenarios. Potentially still quite useful. | ||||||||||||||||||
-- | ||||||||||||||||||
-- Given this HTML, and we want to submit @color=Red@ to the server: | ||||||||||||||||||
-- | ||||||||||||||||||
-- > <form method="POST"> | ||||||||||||||||||
-- > <label for="color-red">Red</label> | ||||||||||||||||||
-- > <input id="color-red" name="color" value="Red" /> | ||||||||||||||||||
-- > <label for="color-blue">Blue</label> | ||||||||||||||||||
-- > <input id="color-blue" name="color" value="Blue" /> | ||||||||||||||||||
-- > </form> | ||||||||||||||||||
-- | ||||||||||||||||||
-- You can set this parameter like so: | ||||||||||||||||||
-- | ||||||||||||||||||
-- > request $ do | ||||||||||||||||||
-- > chooseRadio "Red" | ||||||||||||||||||
-- | ||||||||||||||||||
-- @since 1.6.17 | ||||||||||||||||||
chooseRadio :: T.Text -- ^ The text contained within the @\<label>@. | ||||||||||||||||||
-> RequestBuilder site () | ||||||||||||||||||
chooseRadio v = do | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think |
||||||||||||||||||
mres <- fmap rbdResponse getSIO | ||||||||||||||||||
res <- | ||||||||||||||||||
case mres of | ||||||||||||||||||
Nothing -> failure "chooseRadio: No response available" | ||||||||||||||||||
Just res -> return res | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since you're only using
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm pretty sure this can't be inlined unless using LamdbdaCase |
||||||||||||||||||
let body = simpleBody res | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar deal here — this binding is only used once, so it may as well be inlined below. |
||||||||||||||||||
|
||||||||||||||||||
let name = genericNameFromHTML (==) (v) body | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unless my eyes (and brain) are failing me, I think the parentheses are redundant here.
Suggested change
|
||||||||||||||||||
case name of | ||||||||||||||||||
Right name' -> addPostParam name' $ v | ||||||||||||||||||
Left e -> failure $ e | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The function application operators are redundant in both cases. Also, let's try to use slightly more descriptive names 🙂
Suggested change
|
||||||||||||||||||
|
||||||||||||||||||
byLabelWithMatch :: (T.Text -> T.Text -> Bool) -- ^ The matching method which is used to find labels (i.e. exact, contains) | ||||||||||||||||||
-> T.Text -- ^ The text contained in the @\<label>@. | ||||||||||||||||||
-> T.Text -- ^ The value to set the parameter to. | ||||||||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
name: yesod-test | ||
version: 1.6.16 | ||
version: 1.6.17 | ||
license: MIT | ||
license-file: LICENSE | ||
author: Nubis <[email protected]> | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think typical radio input markup should look like this:
i.e., the input should come before the label. Also, self-closing tags don't need a trailing slash.