Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor bug in HiddenField #5

Open
wants to merge 44 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
9b03d82
Add support for non-tabbed forms (temporary in a new herited class).
Nov 17, 2009
fffda62
New example file to demonstrate PhormsExt.
Nov 17, 2009
c1da8df
Add integration with "Really Easy Field Validation" (based on prototy…
Nov 17, 2009
d33b317
Correct support for fieldsets and add an example. (+ minor doc fixes)
Nov 18, 2009
f8337b3
Add client-side validation in the fieldsets' example.
Nov 18, 2009
8269267
Add new field types : AlphaField, AlphaNumField.
Nov 30, 2009
b1fec8b
Add missing translations.
Nov 30, 2009
ed2b8cb
Fix a typo.
Nov 30, 2009
4fc8e48
prepare_values method that handles an array value for multiple input …
sodabrew Dec 23, 2009
4321cd1
When validating an array input field, don't throw an error if the val…
sodabrew Dec 23, 2009
6f72183
Corrected variable to $this->choices, not $this->options.
sodabrew Dec 23, 2009
5058722
The choices field needs to be public in order to be inherited properl…
sodabrew Dec 23, 2009
8ff7072
Final newlines.
sodabrew Dec 24, 2009
eba0ef1
Add newlines to the output and optional attributes to opening tags.
sodabrew Dec 24, 2009
908666f
Rearrange and label CheckboxWidget.
sodabrew Dec 24, 2009
dfd55e5
Add OptionField for an array of RadioWidgets, just like OptionsField …
sodabrew Dec 24, 2009
a1e9313
Few fixes, improved HTML markup.
Dec 26, 2009
aee438e
- Update scriptaculous and prototype
Dec 27, 2009
25c7518
Line wrapping for better readability.
sodabrew Jan 1, 2010
5fbb858
The MultipleChoiceField class will now use the MultiSelectWidget by d…
sodabrew Jan 1, 2010
4c5a735
Allow arguments to field validation functions.
sodabrew Jan 3, 2010
bc49cff
add_error method in Field class.
sodabrew Jan 4, 2010
1791a7f
form tag actions should be lowercase. Also rearrange switch to remove…
sodabrew Jan 4, 2010
7830f0d
Spacing to match 4-space tabs.
sodabrew Jan 4, 2010
47ad88a
Rearrange the is_valid method, add a post-validate hook, remove the c…
sodabrew Jan 4, 2010
eba6536
Bump to v2 and commit all the changes !
Jan 21, 2010
e6c5e12
added bistory's changes and resolved the conflicts
petsagouris Aug 16, 2010
016f503
coding style cleanup, removed docs.zip
petsagouris Aug 16, 2010
7cf80c6
Currently broken, fixing a lot.
petsagouris Aug 17, 2010
cf2697c
Still broken, Just fixing trailing whitespace and removing PHP close …
petsagouris Aug 17, 2010
9adfe37
Made the examples work as expected. Cleaned up the HTML too.
petsagouris Sep 8, 2010
8a2c913
All done and working. Fixing a previous bad commit.
petsagouris Sep 8, 2010
7bcb51a
Added petsagouris's comments about what changes he made to the README…
jordanlev Sep 21, 2010
f0a50df
Improved class usability (IMHO) -- changed "Numeric" field name to "I…
jordanlev Sep 21, 2010
ad428d2
text/integer fields now set html maxlength based on param; refactore…
jordanlev Sep 22, 2010
3c2f3d6
updated README to reflect new changes
jordanlev Sep 22, 2010
19dd756
HiddenField passing wrong params to parent TextField (thanks Tris)
jordanlev May 15, 2011
5bfe108
add validate_required_field to check if checkbox is checked in case o…
mhyz Jul 5, 2011
e2510a5
add further checks in file_was_uploaded
mhyz Jul 5, 2011
b6f561f
Merge pull request #1 from lemats/patch-1
jordanlev Jul 5, 2011
e981dc3
Merge pull request #2 from lemats/master
jordanlev Jul 5, 2011
3a8a35b
Fixed bug where you cant uncheck a checkbox
tf198 Jan 13, 2012
bbb6904
Fixed widget bug - was applying htmlentites twice to values
tf198 Apr 2, 2012
096b221
Fixed bug with interger validation of zero
tf198 May 1, 2012
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
371 changes: 371 additions & 0 deletions Phorm/Field.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,371 @@
<?php if(!defined('PHORMS_ROOT')) { die('Phorms not loaded properly'); }
/**
* @package Phorms
* @subpackage Fields
*/
/**
* Phorm_Field
*
* Abstract class from which all other field classes are derived.
*
* @author Jeff Ober <[email protected]>
* @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 <[email protected]>
* @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 <[email protected]>
* @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 '<span class="phorm_help">'.htmlentities($this->help_text).'</span>';
}
}

/**
* Returns the HTML field label.
*
* @param boolean $tag determines whether or not label is wrapped in <label> HTML (defaults to TRUE)
* @return string the HTML label tag
*/
public function label($tag=TRUE)
{
if($tag)
{
return sprintf('<label for="%s">%s</label>', (string) $this->get_attribute('id'), $this->label);
}
return $this->label;
}

/**
* Returns the field's tag as HTML.
*
* @return string the field as HTML
*/
public function html()
{
$widget = $this->get_widget();
return $widget->html($this->value, $this->attributes);
}

/**
* Returns the field's errors, optionally wrapped in a div
*
* @param boolean $tag determines whether or not to wrap each error message in a <div> (defaults to TRUE)
* @return string the field errors as an unordered list
*/
public function errors($tag=TRUE)
{
$elts = array();
if( is_array($this->errors) && !empty($this->errors) )
{
foreach( $this->errors as $valid => $error )
{
if($tag)
{
$elts[] = sprintf('<div class="validation-advice" id="advice-%s-%s">%s</div>', $error[0], (string) $this->get_attribute('id'), $this->lang->{$error[1]});
}
else
{
$elts[] = $this->lang->{$error[1]};
}
}
return implode("\n", $elts);
}
return (empty($elts))?'':$elts;
}

/**
* Serializes the field to HTML.
*
* @return string the field's complete HTMl representation.
*/
public function __toString()
{
return $this->html().$this->help_text.$this->errors();
}

/**
* On the first call, calls each validator on the field value, and returns
* true if each returned successfully, false if any raised a
* ValidationError. On subsequent calls, returns the same value as the
* initial call. If $reprocess is set to true (default: false), will
* call each of the validators again. Stores the "cleaned" value of the
* field on success.
*
* @param boolean $reprocess if true, ignores memoized result of initial call
* @return boolean true if the field's value is valid
* @see PhormField::$valid,PhormField::$imported,PhormField::$validators,PhormField::$errors
*/
public function is_valid($reprocess=false)
{
if( $reprocess || is_null($this->valid) )
{
// Pre-process value
$value = $this->prepare_value($this->value);

$this->errors = array();
$v = $this->validators;

foreach( $v as $k => $f )
{
try
{
if ($f == 'required') { //special case -- available to all field types, and $this->validate() isn't even called if value is empty
$this->validate_required_field($value);
} else {
call_user_func($f, $value);
}
}
catch( Phorm_ValidationError $e )
{
$rule_name = is_array($f) ? $f[1] : $f; //handles both string (function name) and array (instance, function name)
$this->errors[] = array( $rule_name, $this->lang->{$e->getMessage()} );
}
}

if( $value !== '' )
{
try
{
$this->validate($value);
}
catch( Phorm_ValidationError $e )
{
$this->errors[] = array( strtolower(get_class($this)), $this->lang->{$e->getMessage()} );
}
}

if( $this->valid = empty($this->errors) )
{
$this->imported = $this->import_value($value);
}
}
return $this->valid;
}

/**
* Pre-processes a value for validation, handling magic quotes if used.
*
* @param string $value the value from the form array
* @return string the pre-processed value
*/
public function prepare_value($value)
{
return ( get_magic_quotes_gpc() ) ? stripslashes($value) : $value;
}

/**
* Defined in derived classes; must return an instance of PhormWidget.
*
* @return PhormWidget the field's widget
* @see PhormWidget
*/
abstract protected function get_widget();

/**
* Raises a Phorm_ValidationError if $value is invalid.
*
* @param string|mixed $value (may be mixed if prepare_value returns a non-string)
* @throws ValidationError
* @return null
* @see ValidationError
*/
public function validate($value)
{
return filter_var($value, FILTER_SANITIZE_STRING);
}

/**
* Returns the field's "imported" value, if any processing is required. For
* example, this function may be used to convert a date/time field's string
* into a unix timestamp or a numeric string into an integer or float.
*
* @param string|mixed $value the pre-processed string value (or mixed if prepare_value returns a non-string)
* @return mixed
*/
abstract public function import_value($value);

/**
* Validates that the value isn't null or an empty string.
* This is a built-in validation rule available to all fields
* (we have hard-coded logic in the is_valid() function
* to call this if 'required' exists in the validation array).
*
* @param string $value
* @return null
* @throws Phorm_ValidationError
*/
public function validate_required_field($value)
{
if ($value == '' || is_null($value))
{
throw new Phorm_ValidationError('validation_required');
}
}
}
Loading