hunchentoot icon indicating copy to clipboard operation
hunchentoot copied to clipboard

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

Open oruppert opened this issue 4 years ago • 5 comments

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

oruppert avatar Sep 18 '21 13:09 oruppert

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?

stassats avatar Sep 18 '21 14:09 stassats

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

oruppert avatar Sep 18 '21 14:09 oruppert

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 avatar Sep 20 '21 09:09 oruppert

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

oruppert avatar Sep 20 '21 10:09 oruppert

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

stassats avatar Sep 20 '21 13:09 stassats