Skip to content

Commit

Permalink
Adds support for cancel-options
Browse files Browse the repository at this point in the history
  • Loading branch information
BenjaminVanRyseghem committed Jan 2, 2015
1 parent 8428093 commit 9ba4edf
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 81 deletions.
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ See a __live demo__ at [formidable-demo.herokuapp.com](http://formidable-demo.he
Leiningen coordinate:

```clj
[formidable "0.1.8"]
[formidable "0.1.9"]
```

## Usage
Expand Down Expand Up @@ -265,16 +265,20 @@ The following special keys are also supported:
:renderer - Determines the type of renderer to use. Built-in options:
:bootstrap-horizontal (the default)
:bootstrap-stacked
:bootstrap3-stacked
:table
:inline
:fields - Sequence of form field specifications. See below.
:values - Map of values used to populate the form fields, or a
form-data-encoded string
form-data-encoded string.
:submit-label - Label to use on the submit button. Defaults to "Submit"
:cancel-label - Label to use on the cancel button. Defaults to "Cancel"
:cancel-options - Options map provided to the cancel DOM node (see Hiccup).
If this options is provided, `:cancel-href` MUST NOT be used.
:cancel-href - When provided, shows a "Cancel" link or button next to the
submit button
:validations - A sequence of validation specifications
submit button.
If this options is provided, `:cancel-options` MUST NOT be used.
:validations - A sequence of validation specifications.
:validator - A function to call to validate parsed values for this
form. The function should take a map of values and return
a sequence of problem maps for each field that failed to
Expand Down
2 changes: 1 addition & 1 deletion project.clj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(defproject formidable "0.1.8"
(defproject formidable "0.1.9"
:description "Web forms - rendering, parsing, and validating"
:url "https://github.com/teamwall/formidable"
:license {:name "Eclipse Public License"
Expand Down
140 changes: 71 additions & 69 deletions src/formidable/core.cljx
Original file line number Diff line number Diff line change
Expand Up @@ -221,10 +221,10 @@
[fields values])
fields (prep-fields fields values spec)
;; Attach :cancel-href to submit button
fields (if (or (:cancel-label spec) (:cancel-href spec))
fields (if (or (:cancel-label spec) (:cancel-href spec) (:cancel-options spec))
(for [field fields]
(if (= :submit (:type field))
(merge field (select-keys spec [:cancel-label :cancel-href]))
(merge field (select-keys spec [:cancel-label :cancel-href :cancel-options]))
field))
fields)
;; Add submit if not already present
Expand All @@ -236,6 +236,7 @@
:name "submit"
:cancel-label (:cancel-label spec "Cancel")
:cancel-href (:cancel-href spec)
:cancel-options (:cancel-options spec)
:value (:submit-label spec "Submit")}]))
;; Problems
problems (prep-problems (:problems spec))
Expand All @@ -262,78 +263,79 @@
The following special keys are also supported:
:renderer - Determines renderer to use. Built-in options:
:bootstrap-horizontal (the default)
:bootstrap-stacked
:bootstrap3-stacked
:table
:inline
Custom renderers can be created by implementing the
formidable.render/render-form multimethod.
:fields - Sequence of form field specifications. See below.
:values - Map of values used to populate the form fields, or a
form-data-encoded string
:submit-label - Label to use on the submit button. Defaults to \"Submit\"
:cancel-label - Label to use on the cancel button. Defaults to \"Cancel\"
:cancel-href - When provided, shows a \"Cancel\" hyperlink next to the
submit button
:validations - A sequence of validation specifications
:validator - A function to call to validate parsed values for this
form. The function should take a map of values and return
a sequence of problem maps for each field that failed to
validate. The problem map should contain the keys :keys
and :msg.
:renderer - Determines renderer to use. Built-in options:
:bootstrap-horizontal (the default)
:bootstrap-stacked
:bootstrap3-stacked
:table
:inline
Custom renderers can be created by implementing the
formidable.render/render-form multimethod.
:fields - Sequence of form field specifications. See below.
:values - Map of values used to populate the form fields, or a
form-data-encoded string
:submit-label - Label to use on the submit button. Defaults to \"Submit\"
:cancel-label - Label to use on the cancel button. Defaults to \"Cancel\"
:cancel-options - Label to use on the cancel button. Defaults to \"Cancel\"
:cancel-href - When provided, shows a \"Cancel\" hyperlink next to the
submit button
:validations - A sequence of validation specifications
:validator - A function to call to validate parsed values for this
form. The function should take a map of values and return
a sequence of problem maps for each field that failed to
validate. The problem map should contain the keys :keys
and :msg.
:validate-types - Whether to validate datatypes; true by default.
:blank-nil - When values are parsed, replace blank strings with nil
:problems - Sequence of field names or problem maps. Form
renderers typically add a class and style to highlight
problem fields and, if problem maps are provided,
show descriptive messages.
:timezone - String of timezone with which to localize the display of
:datetime-select fields. The default is UTC. JVM only.
:blank-nil - When values are parsed, replace blank strings with nil
:problems - Sequence of field names or problem maps. Form
renderers typically add a class and style to highlight
problem fields and, if problem maps are provided,
show descriptive messages.
:timezone - String of timezone with which to localize the display of
:datetime-select fields. The default is UTC. JVM only.
A field specification is a map with the following keys:
:name - Required name of the field, a keyword or string. Use
dotted keywords like :foo.bar to represent fields that
will parse as nested map values.
:label - Optional display name. Auto-generated from :name if not
provided
:type - Type of the field. Defaults to :text. See below for
built-in types. If an unrecognized type is provided,
an <input> element with that type will be assumed.
Certain types imply particular parsing or validation
rules - e.g., an :email field must be a valid email.
:datatype - Optional. Datatype of the field used for parsing. Can be
one of:
:str, :int, :long, :boolean, :float, :double, :decimal,
:bigint, :date, :time, :instant, :file.
Defaults to :str.
All types can be appended with an \"s\" when a sequence
is expected - e.g., :ints for a sequence of integers. This
is useful for fields that have composite values, such as
:checkboxes.
:date field values are expected to be in yyyy-MM-dd
format by default. Set :date-format to change that. :time
fields may be in H:m or H:m:s format. :instant fields
are in EDN instant (RFC-3339) format.
All date/time fields are parsed into java.util.Date
or java.sql.Time (or Date for ClojureScript) objects
created using the UTC timezone.
:name - Required name of the field, a keyword or string. Use
dotted keywords like :foo.bar to represent fields that
will parse as nested map values.
:label - Optional display name. Auto-generated from :name if not
provided
:type - Type of the field. Defaults to :text. See below for
built-in types. If an unrecognized type is provided,
an <input> element with that type will be assumed.
Certain types imply particular parsing or validation
rules - e.g., an :email field must be a valid email.
:datatype - Optional. Datatype of the field used for parsing. Can be
one of:
:str, :int, :long, :boolean, :float, :double, :decimal,
:bigint, :date, :time, :instant, :file.
Defaults to :str.
All types can be appended with an \"s\" when a sequence
is expected - e.g., :ints for a sequence of integers. This
is useful for fields that have composite values, such as
:checkboxes.
:date field values are expected to be in yyyy-MM-dd
format by default. Set :date-format to change that. :time
fields may be in H:m or H:m:s format. :instant fields
are in EDN instant (RFC-3339) format.
All date/time fields are parsed into java.util.Date
or java.sql.Time (or Date for ClojureScript) objects
created using the UTC timezone.
:datatype-error - Optional custom error message to use if datatype
validation fails.
:blank-nil - When the value is parsed, replace a blank string with nil
:flatten - If a value parses to a map (e.g. for :compound fields),
adds each key of the map to the top level values map,
prefixed with the field name and a dash.
:note - A bit of explanatory content to accompany the field
:prefix - Content to insert before a field
:suffix - Content to insert after a field
validation fails.
:blank-nil - When the value is parsed, replace a blank string with nil
:flatten - If a value parses to a map (e.g. for :compound fields),
adds each key of the map to the top level values map,
prefixed with the field name and a dash.
:note - A bit of explanatory content to accompany the field
:prefix - Content to insert before a field
:suffix - Content to insert after a field
Built-in field types:
Expand Down
10 changes: 8 additions & 2 deletions src/formidable/render/bootstrap.cljx
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,15 @@
(when (:suffix field)
[:span.suffix (:suffix field)])
(when (and (= :submit (:type field))
(:cancel-href field))
(:cancel-href field)
(not (:cancel-options field)))
[:span.cancel-link " " [:a.btn {:href (:cancel-href field)}
(:cancel-label field)]])
(when (and (= :submit (:type field))
(:cancel-options field)
(not (:cancel-href field)))
[:span.cancel-link " " [:a.btn (:cancel-options field)
(:cancel-label field)]])
(when (:note field)
[:div.note.help-inline (:note field)])]))]))

Expand Down Expand Up @@ -88,4 +94,4 @@
(render-bootstrap-form form-attrs fields "form-shell form-horizontal" opts))

(defmethod render-form :bootstrap-stacked [form-attrs fields opts]
(render-bootstrap-form form-attrs fields "form-shell" opts))
(render-bootstrap-form form-attrs fields "form-shell" opts))
11 changes: 9 additions & 2 deletions src/formidable/render/bootstrap3.cljx
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,16 @@
(when (:prefix field)
[:span.prefix (:prefix field)])
(when (and (= :submit (:type field))
(:cancel-href field))
(:cancel-href field)
(not (:cancel-options field)))
[:span.cancel-link.visible-xs-inline-block.visible-sm-inline-block.visible-md-inline-block.visible-lg-inline-block
" " [:a.btn {:href (:cancel-href field)}
" " [:a.btn.cancel-form {:href (:cancel-href field)}
(:cancel-label field)]])
(when (and (= :submit (:type field))
(:cancel-options field)
(not (:cancel-href field)))
[:span.cancel-link.visible-xs-inline-block.visible-sm-inline-block.visible-md-inline-block.visible-lg-inline-block
" " [:a.btn.cancel-form (:cancel-options field)
(:cancel-label field)]])
(cond
checkbox? [:label {:for field-id} " "
Expand Down
4 changes: 3 additions & 1 deletion src/formidable/render/div.cljx
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@
[:label {:for field-id} " " [:span.cb-label (:label field)]])
(when (:suffix field)
[:span.suffix (:suffix field)])
(when (and (= :submit (:type field)) (:cancel-href field))
(when (and (= :submit (:type field)) (:cancel-href field) (not (:cancel-options field)))
[:span.cancel-link " " [:a {:href (:cancel-href field)} (:cancel-label field)]])
(when (and (= :submit (:type field)) (:cancel-options field) (not (:cancel-href field)))
[:span.cancel-link " " [:a (:cancel-options field) (:cancel-label field)]])
(when (:note field)
[:div.note (:note field)])]))]))

Expand Down
4 changes: 3 additions & 1 deletion src/formidable/render/inline.cljx
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@
[:label {:for field-id} " " [:span.cb-label (:label field)]])
(when (:suffix field)
[:span.suffix (:suffix field)])
(when (and (= :submit (:type field)) (:cancel-href field))
(when (and (= :submit (:type field)) (:cancel-href field) (not (:cancel-options field)))
[:span.cancel-link " " [:a {:href (:cancel-href field)} (:cancel-label field)]])
(when (and (= :submit (:type field)) (:cancel-options field) (not (:cancel-href field)))
[:span.cancel-link " " [:a (:cancel-options field) (:cancel-label field)]])
(when (:note field)
[:span.note (:note field)])])]))

Expand Down
4 changes: 3 additions & 1 deletion src/formidable/render/table.cljx
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
[:label {:for field-id} " " [:span.cb-label (:label field)]])
(when (:suffix field)
[:span.suffix (:suffix field)])
(when (and (= :submit (:type field)) (:cancel-href field))
(when (and (= :submit (:type field)) (:cancel-href field) (not (:cancel-options field)))
[:span.cancel-link " " [:a {:href (:cancel-href field)} (:cancel-label field)]])
(when (and (= :submit (:type field)) (:cancel-options field) (not (:cancel-href field)))
[:span.cancel-link " " [:a (:cancel-options field) (:cancel-label field)]])
(when (:note field)
[:div.note (:note field)])]
label-el (when (and (not (#{:checkbox} (:type field))) (:label field))
Expand Down

0 comments on commit 9ba4edf

Please sign in to comment.