README.md in hungryform-0.0.2 vs README.md in hungryform-0.0.4
- old
+ new
@@ -1,29 +1,30 @@
# HungryForm
+[![Build Status](https://travis-ci.org/andrba/hungryform.svg?branch=master)](https://travis-ci.org/andrba/hungryform)
+
HungryForm is a gem for managing multiple page forms. The main purpose of this gem is to give developers an easy DSL to build complex forms.
## Usage
```ruby
-require 'hungryform'
-
-form = HungryForm.new do
+form = HungryForm::Form.new do
page :first do
- text_field :first_name
- text_field :last_name
+ text_field :first_name, required: true
+ text_field :last_name, required: true
end
page :second, do
text_field :address
+ select_field :gender, options: { "M" => "Male", "F" => "Female" }, required: true
end
page :third do
text_field :occupation
# Show this group only when the occupation field is not empty
- group :employment_history, visible: false, dependency: '{"SET": "third_occupation"}' do
+ group :employment_history, dependency: '{"SET": "third_occupation"}' do
html :before, value: "Employment history over the last 5 years"
- text_field :history, value: "Default value"
+ text_area :history, value: "Default value"
end
end
end
```
@@ -36,11 +37,11 @@
"second_address" => "John's address",
"third_occupation" => "Software engineer",
"third_employment_history_history" => "John's employment hisotory"
}
-form = HungryForm.new :params => params do
+form = HungryForm::Form.new :params => params do
...
end
```
@@ -83,11 +84,11 @@
```
If the dependency is resolved positively it makes the element visible. Otherwise the element will be hidden and not required. It is allowed to use element names or params keys as parameters inside expressions.
```ruby
-HungryForm.new do
+HungryForm::Form.new do
page :about do
text_field :age
text_field :favourite_alcohol, required: true, dependency: '{"GT": ["about_age", "18"]}'
end
end
@@ -104,24 +105,59 @@
```ruby
text_field :name, required: true
text_field :email, validation: ->(el) { "is unexpected email" unless el.value == "me@yahoo.com" }
```
-You can extend the list of validation rules by opening the HungryForm::Validator singleton class and creating your own validation methods:
+You can extend the list of validation rules by creating your own validation methods:
```ruby
-class HungryForm
- class Validator
+module HungryForm
+ module Validator
class << self
def my_validation_method(element, rule)
"is not #{rule}" unless element.value == rule
end
end
end
end
text_field :vegetable, value: "tomato", my_validation_method: "potato" # => is not potato
+```
+
+## Custom form fields
+You can create your own field type by adding a new class into the HungryForm::Elements module. There are three base classes that you can choose to inherit from:
+
+- Base::Element - use this class when you don't need the field to have a value and validation. As an example it can be used for text/html output
+- Base::ActiveElement - use this class when you need the field to have a value and validation
+- Base::OptionsElement - this class inherits the Base::ActiveElement. Use it when you need to create an element with an options hash, like a dropdown
+
+```ruby
+module HungryForm
+ module Elements
+ class MyField < Base::ActiveElement
+ attr_accessor :my_param
+
+ hashable :my_param
+
+ def initialize(name, parent, resolver, options = {}, &block)
+ self.my_param = options[:my_param] || true
+
+ super
+ end
+
+ def valid?
+ self.value == 'valid_value'
+ end
+ end
+ end
+end
+
+form = HungryForm::Form.new do
+ page :main do
+ my_field :my_field_name, my_param: "Param Value"
+ end
+end
```
## Contributing
1. Fork it ( https://github.com/andrba/hungryform/fork )