Skip to content

Add :PREFER-POST as an request-type to compute-parameter.#200

Open
oruppert wants to merge 3 commits into
edicl:masterfrom
oruppert:master
Open

Add :PREFER-POST as an request-type to compute-parameter.#200
oruppert wants to merge 3 commits into
edicl:masterfrom
oruppert:master

Conversation

@oruppert

Copy link
Copy Markdown

Useful when using redirects to the same script-name to validate form parameters.

@stassats

Copy link
Copy Markdown
Member

Having :both and :prefer-post is pretty confusing. Can they be made into :get-or-post and :post-or-get, with :both as an alias for the former?

@oruppert

Copy link
Copy Markdown
Author

I am not so sure anymore if depending on the order of the get and post parameters is such a smart thing.

@oruppert

Copy link
Copy Markdown
Author

Here Is the usecase for post-parameters: Simple server side form validation:
In the inital request to the handler name is nil and no error message is shown.
After the submit the post part checks the form values and redirects to the get
part.

    (name)
  (flet ((invalid-form-values ()
	   (redirect
	    (format nil "~a?~{~(~a~)=~a~^&~}"
		    (script-name*)
		    (loop for (k . v) in (append
					  (post-parameters*)
					  (get-parameters*))
			  collect (url-encode k)
			  collect (url-encode v))))))
  (ecase (request-method*)
    (:post
     (when (or (null name)
	       (zerop (length name)))
       (invalid-form-values))
     (with-html-output-to-string (out)
       (:html
	(:h1 "Submit Successful"))))
    (:get
     (with-html-output-to-string (out)
       (:html
	(:head
	 (:style
	  "form { width:40ch }"
	  "label { font-size:85% }"
	  "input { width:100% }"))
	(:body
	 ;; to make this form work with current hunchentoot, we have
	 ;; to add an :action attribute.
	 (:form

	  :method :post
	  (:p (:label
	       "Name"
	       ;; show an error description
	       (when (and name (zerop (length name)))
		 (htm (:span "Name must not be empty")))
	       (:input :name :name :value name)))
	  (:p (:button "Create"))))))))))```

I think the request-type argument should also accept a
function as a request-type parameter.

@oruppert

Copy link
Copy Markdown
Author

stassats: I have followed your suggestions and added the keywords
:GET-OR-POST and :POST-OR-GET

@stassats

Copy link
Copy Markdown
Member

The documentation needs to be adjusted too. And I'm thinking, maybe :both shouldn't be documented anymore.

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

Successfully merging this pull request may close these issues.

2 participants