Skip to content
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

Form encoding is not set to MultiPart if the file fields are within a list (Snap/Heist) #161

Open
cimmanon opened this issue May 24, 2023 · 0 comments

Comments

@cimmanon
Copy link
Contributor

I'm building a fairly simple form for managing images that contains 2 lists: uploaded images and existing images.

data Image = Image
	{ path :: FilePath
	, source :: Maybe Text
	} deriving (Eq)

imagesForm :: Monad m => Maybe [Image] -> Form Text m ([Image], [Image])
imagesForm xs = ( , )
	<$> "upload" .: listOf uploadForm Nothing
	<*> "update" .: listOf updateForm xs

uploadForm :: Monad m => Maybe Image -> Form Text m Image
uploadForm _ = validate hasFile $ ( , )
	<$> "path" .: file
	<*> "source" .: optionalText Nothing
	where
		hasFile (Just p, s) = Success $ Image p s
		hasFile (_, s) = Error $ "No file provided: " <> fromMaybe "no source provided" s

updateForm :: Monad m => Maybe Image -> Form Text m Image
updateForm x = Image
	<$> "path" .: string (path <$> x)
	<*> "source" .: optionalText (source =<< x)

The rendered form has enctype="application/x-www-form-urlencoded", so no files get uploaded. If I manually set the enctype on the form to be multipart/form-data, all of the POST data is ignored and the submitted form always fails, with failure state containing the form's defaults. Checking the contents of POST from Snap gives me an empty list.

If I add a dummy file field to my form, one that's not within a list, the form is correctly set to multipart and the form works as expected.

imagesForm :: Monad m => Maybe [Image] -> Form Text m ([Image], [Image])
imagesForm xs = (\a b c -> (a, b))
	<$> "upload" .: listOf uploadForm Nothing
	<*> "update" .: listOf updateForm xs
	<*> "foo" .: file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant