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

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

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

wants to merge 3 commits into from

Conversation

oruppert
Copy link

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

@stassats
Copy link
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
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
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
Author

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

@stassats
Copy link
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