Class: WWW::Mechanize::Form

Class | WWW::Mechanize::Form
In: | lib/www/mechanize/form.rb lib/www/mechanize/form/button.rb lib/www/mechanize/form/check_box.rb lib/www/mechanize/form/field.rb lib/www/mechanize/form/file_upload.rb lib/www/mechanize/form/image_button.rb lib/www/mechanize/form/multi_select_list.rb lib/www/mechanize/form/option.rb lib/www/mechanize/form/radio_button.rb lib/www/mechanize/form/select_list.rb lib/www/mechanize/monkey_patch.rb
Parent: | Object

# Synopsis

This class encapsulates a form parsed out of an HTML page. Each type of input fields available in a form can be accessed through this object. See GlobalForm for more methods.

## Example

Find a form and print out its fields

     form = page.forms.first # => WWW::Mechanize::Form
     form.fields.each { |f| puts f.name }

Set the input field ‘name’ to "Aaron"

     form['name'] = 'Aaron'
     puts form['name']

### Methods

[] []= add_button_to_query add_field! build_query click_button delete_field! has_field? has_key? has_value? keys method_missing new request_data set_fields submit values

### Classes and Modules

Class WWW::Mechanize::Form::Button
Class WWW::Mechanize::Form::CheckBox
Class WWW::Mechanize::Form::Field
Class WWW::Mechanize::Form::FileUpload
Class WWW::Mechanize::Form::ImageButton
Class WWW::Mechanize::Form::MultiSelectList
Class WWW::Mechanize::Form::Option
Class WWW::Mechanize::Form::RadioButton
Class WWW::Mechanize::Form::SelectList

### External Aliases

fields | -> | elements
pretty_inspect | -> | inspect

### Attributes

action | [RW] |
buttons | [R] |
checkboxes | [R] |
enctype | [RW] |
fields | [R] |
file_uploads | [R] |
form_node | [R] |
method | [RW] |
name | [RW] |
page | [R] |
radiobuttons | [R] |

### Public Class methods

new(node, mech=nil, page=nil)

### Public Instance methods

[](field_name)

Fetch the value of the first input field with the name passed in

## Example

Fetch the value set in the input field ‘name‘

     puts form['name']

[]=(field_name, value)

Set the value of the first input field with the name passed in

## Example

Set the value in the input field ‘name’ to "Aaron"

     form['name'] = 'Aaron'

add_button_to_query(button)

This method adds a button to the query. If the form needs to be submitted with multiple buttons, pass each button to this method.

add_field!(field_name, value = nil)

Add a field with `field_name` and `value`

build_query(buttons = [])

This method builds an array of arrays that represent the query parameters to be used with this form. The return value can then be used to create a query string for this form.

click_button(button = buttons.first)

Submit form using `button`. Defaults to the first button.

delete_field!(field_name)

Removes all fields with name `field_name`.

has_field?(field_name)

Returns whether or not the form contains a field with `field_name`

has_key?(field_name)

Alias for has_field?

has_value?(value)
keys()
method_missing(id,*args)

Treat form fields like accessors.

request_data()

This method calculates the request data to be sent back to the server for this form, depending on if this is a regular post, get, or a multi-part post,

set_fields(fields = {})

This method sets multiple fields on the form. It takes a list of field name, value pairs. If there is more than one field found with the same name, this method will set the first one found. If you want to set the value of a duplicate field, use a value which is a Hash with the key as the index in to the form. The index is zero based. For example, to set the second field named ‘foo’, you could do the following:

     form.set_fields( :foo => { 1 => 'bar' } )

submit(button=nil, headers = {})

Submit this form with the button passed in

values()

[Validate]