ValidForm::Field (Class)

In: ValidForm.rb
ValidForm_html.rb
Parent: Object

A generic ValidForm field. See also the subclasses ::Text, ::Password, ::Option, ::OptionSet, ::RadioSet, ::CheckboxSet, ::Reset, and ::Submit

Methods

Constants

VALIDATION_KEYS = { :required=>1,:minlength=>1,:maxlength=>1,:type=>1,:match=>1,:nomatch=>1,:minvalue=>1,:maxvalue=>1,:reqfail_msg=>1,:typefail_msg=>1 }
  Complete list of valid keys; used to ensure that validationRules are valid.
FIELD_WRAPPER_START = '<span class="field">'
  Code to put before the output of each field_tohtml call
FIELD_WRAPPER_CLOSE = '</span>'
  Code to put after the output of each field_tohtml call

Attributes

attributes  [RW]  A Hash of additional attributes to associate with this field.
id  [R]  The unique identifier for this field.
label  [RW]  The human-friendly label for this field.
name  [RW]  The name associated with this field.
validationRules  [RW]  A Hash of validation rules which apply to this field. See validate for more information.
value  [RW]  The current value of this field.

Included Modules

Public Class methods

id:The unique identifier for this field. [reqd]
name:The name associated with this field (used for name/value pairs).
value:The initial String value for the field.
label:The human-friendly label for the field.
attributes:A Hash of various custom attributes to be associated with the field.
validationRules:A Hash of validation rules which apply to this field. See validate for more information.
  email = ValidForm::Field.new('email','email',nil,'Your email address')
  email.validationRules[:required]=true
  email.validationRules[:reqfail_msg]="Please provide your email " +
                                      "address so we can spam you!"

  story = ValidForm::Text.new('story','story',nil,'Your email address',
             {:cols=>80, :rows=> 10}
             {:minlength=>50, :maxlength=>2000}
          )
  story.multiline=true

Public Instance methods

Returns false if the field has not been validated against the current value; nil if the field has been validated successfully, or an array of one or more ::ValidationError objects otherwise.

withValidation:Boolean value to control whether the custom attributes for client-side validation (using FormAutoValidate) are included or not.
indentLevel:Number of tabs to indent the HTML code (for source code readability).

Returns an HTML representation of the field itself. All subclasses of ::Field override this with their own correct implementation.

indentLevel:Number of tabs to indent the HTML code (for source code readability).

Returns an HTML representation of the label for the field.

withValidation:Boolean value to control whether the custom attributes for client-side validation (using FormAutoValidate) are included or not.
indentLevel:Number of tabs to indent the HTML code (for source code readability).

Returns an HTML representation of the field, including a label.

See also label_tohtml and field_tohtml.

Checks the field’s value against the validationRules set for the field. The following validation keys apply to every type of field:

required:A truth value representing whether or not the value may be nil or "". If required is true and the value is nil or an empty string, the validation fails.
minlength:An Integer representing the minimum number of characters allowed for the value (if it has a value).
maxlength:An Integer representing the maximum number of characters allowed for the value.
type:One of the following Symbols: :phone, :email, :zipcode, :integer, :float, :date, :time, :datetime.
match:A Regexp which will be matched against the value; if a match is not found, the validation fails.
nomatch:A Regexp which will be matched against the value; if a match is found, the validation fails.
minvalue:A Number (for :type=>:integer or :type=>:float) or Time (for :type=>:date, :type=>:time, :type=>:datetime) which specifies the minimum legal value.
maxvalue:A Number or Time which specifies the maximum legal value.

See also ::OptionSet#validate for the additional minchosen and maxchosen validation rules applicable to option sets.

In addition to the above, the following strings can be set in the validationRules Hash to customize certain error messages:

reqfail_msg:The message to display if the field is required, but was not filled out.
typefail_msg:The message to display if the field has a type, match, or nomatch validation set and fails.

In the above message strings, instances of the token %label% will be replaced by the label for the field. (If the field has no label, its name will be used; if it has no name, its id will be used.)

Returns true if the field validates successfully; false if one or more validation rules are broken. (If the field fails to validate properly, the errors will be stored as an Array of instances of ::ValidationError, accessible via errors.)

Returns true if the field has been validated against the current value; false otherwise.

Note that validated?==true only indicates that validation has occurred; the validation may have failed, in which case error would have a non-nil value.

[Validate]