diff --git a/license.txt b/LICENSE similarity index 100% rename from license.txt rename to LICENSE diff --git a/Phorm/Field.class.php b/Phorm/Field.class.php new file mode 100644 index 0000000..8a9f402 --- /dev/null +++ b/Phorm/Field.class.php @@ -0,0 +1,371 @@ + + * @license http://www.opensource.org/licenses/mit-license.php MIT + * @package Phorms + * @subpackage Fields + */ +abstract class Phorm_Field +{ + + /** + * The field's text label. + * @var string + */ + public $label; + /** + * Store's the field's value. Set during validation. + * @var string + */ + private $value; + /** + * Array of callbacks used to validate field data. May be either a string + * denoting a function or an array of array(instance, string method) to use + * a class instance method. + * @var array + */ + private $validators; + /** + * Associative array of key/value pairs representing HTML attributes of the field. + * @var array + */ + private $attributes; + /** + * Array storing errors generated during field validation. + * @var array + */ + private $errors; + /** + * Storage of the "cleaned" field value. + */ + private $imported; + /** + * Help text for the field. This is printed out with the field HTML. + * @var string + */ + private $help_text = ''; + /** + * If true, this field uses multiple field widgets. + * @see widgets.php + * @var boolean + */ + public $multi_field = false; + /** + * Stores the result of field validation to prevents double-validation. + * @var boolean + */ + private $valid; + + /** + * @param string $label the field's label + * @param array $validators callbacks used to validate field data + * @param array $attributes an assoc of key/value pairs representing HTML attributes + * @return null + */ + public function __construct($label, array $validators=array(), array $attributes=array(), $lang='en') + { + if( !isset($attributes['class']) ) + { + $attributes['class'] = strtolower(get_class($this)); + } + else + { + $attributes['class'] .= ' '.strtolower(get_class($this)); + } + + $this->label = (string) $label; + $this->attributes = $attributes; + $this->validators = $validators; + $this->lang = new Phorm_Language($lang); + } + + /** + * Sets the value of the field. + * + * @param mixed $value the field's value + * @return null + */ + public function set_value($value) + { + $this->value = $value; + } + + /** + * Returns the "cleaned" value of the field. + * + * @return mixed the field's "cleaned" value + */ + public function get_value() + { + return $this->imported; + } + + /** + * Returns the "raw" value of the field. + * + * @author Aaron Stone + * @return mixed the field's raw value (unsanitized) + */ + public function get_raw_value() + { + return $this->value; + } + + /** + * Sets an HTML attribute of the field. + * + * @param string $key the attribute name + * @param string $value the attribute's value + * @return null + */ + public function set_attribute($key, $value) + { + $this->attributes[$key] = $value; + } + + /** + * Returns the value of an HTML attribute or null if not set. + * + * @param string $key the attribute name to look up + * @return string|null the attribute's value or null if not set + */ + public function get_attribute($key) + { + if( array_key_exists($key, $this->attributes) ) + { + return $this->attributes[$key]; + } + return null; + } + + /** + * Returns a list of errors generated during validation. If the field is not + * yet validated, returns null. + * + * @return array|null + */ + public function get_errors() + { + return $this->errors; + } + + /** + * Adds to the error list. + * + * @author Aaron Stone + * @return null + */ + public function add_error($error) + { + $this->errors[] = $error; + } + + /** + * Returns an HTML string containing the field's help text. + * If provided with $text it assigns help text to the field. + * + * @param string $text the help text + * @return null|string + */ + public function help_text($text='') + { + if( !empty($text) ) + { + $this->help_text = $text; + } + elseif( !empty($this->help_text) ) + { + return ''.htmlentities($this->help_text).''; + } + } + + /** + * Returns the HTML field label. + * + * @param boolean $tag determines whether or not label is wrapped in