Permalink
Browse files

Demo update and Global filter function

  • Loading branch information...
1 parent a8d8054 commit 48b29ce5487837509c65a565ff06b70a820634f0 @Horttcore committed Oct 21, 2011
Showing with 136 additions and 69 deletions.
  1. +0 −2 .gitignore
  2. +64 −37 class.formz.php
  3. +16 −13 demo/filters.php
  4. +7 −5 demo/input.php
  5. +35 −0 demo/mail.php
  6. +5 −3 demo/radio-checkbox.php
  7. +5 −5 demo/select.php
  8. +4 −4 demo/textarea.php
View
@@ -1,2 +0,0 @@
-.gitIgnore
-.DS_Store
View
@@ -122,41 +122,11 @@ class formz {
/**
- * Filter Dictonary
+ * Global filter callback
*
- * @access protected
- * @var array
+ * @var mixed Callback function or Array with callback functions
**/
- protected $filter = array(
- 'FILTER_VALIDATE_BOOLEAN' => 258,
- 'FILTER_VALIDATE_EMAIL' => 274,
- 'FILTER_VALIDATE_FLOAT' => 259,
- 'FILTER_VALIDATE_INT' => 257,
- 'FILTER_VALIDATE_IP' => 275,
- 'FILTER_VALIDATE_REGEXP' => 272,
- 'FILTER_VALIDATE_URL' => 273
- );
-
-
-
- /**
- * Filter flags
- *
- * @access protected
- * @var array
- **/
- protected $filter_flags = array(
- 'FILTER_NULL_ON_FAILURE' => 134217728,
- 'FILTER_FLAG_ALLOW_THOUSAND' => 8192,
- 'FILTER_FLAG_ALLOW_OCTAL' => 1,
- 'FILTER_FLAG_ALLOW_HEX' => 2,
- 'FILTER_FLAG_IPV4' => 1048576,
- 'FILTER_FLAG_IPV6' => 2097152,
- 'FILTER_FLAG_NO_PRIV_RANGE' => 8388608,
- 'FILTER_FLAG_NO_RES_RANGE' => 4194304,
- 'FILTER_FLAG_PATH_REQUIRED' => 262144,
- 'FILTER_FLAG_QUERY_REQUIRED' => 524288
- );
+ public $filter;
@@ -455,6 +425,45 @@ class formz {
/**
+ * Filter Dictonary
+ *
+ * @access protected
+ * @var array
+ **/
+ protected $validate_dictonary = array(
+ 'FILTER_VALIDATE_BOOLEAN' => 258,
+ 'FILTER_VALIDATE_EMAIL' => 274,
+ 'FILTER_VALIDATE_FLOAT' => 259,
+ 'FILTER_VALIDATE_INT' => 257,
+ 'FILTER_VALIDATE_IP' => 275,
+ 'FILTER_VALIDATE_REGEXP' => 272,
+ 'FILTER_VALIDATE_URL' => 273
+ );
+
+
+
+ /**
+ * Filter flags
+ *
+ * @access protected
+ * @var array
+ **/
+ protected $validate_dictonary_flags = array(
+ 'FILTER_NULL_ON_FAILURE' => 134217728,
+ 'FILTER_FLAG_ALLOW_THOUSAND' => 8192,
+ 'FILTER_FLAG_ALLOW_OCTAL' => 1,
+ 'FILTER_FLAG_ALLOW_HEX' => 2,
+ 'FILTER_FLAG_IPV4' => 1048576,
+ 'FILTER_FLAG_IPV6' => 2097152,
+ 'FILTER_FLAG_NO_PRIV_RANGE' => 8388608,
+ 'FILTER_FLAG_NO_RES_RANGE' => 4194304,
+ 'FILTER_FLAG_PATH_REQUIRED' => 262144,
+ 'FILTER_FLAG_QUERY_REQUIRED' => 524288
+ );
+
+
+
+ /**
* Input and labels are wrapped in this tag
*
* @var string
@@ -979,9 +988,27 @@ protected function checkfield( $e, $return_error_message = FALSE )
// Filter
if ( isset($e['filter']) && is_callable($e['filter']) ) :
- $this->data[$e['name']] = call_user_func( $e['filter'], $this->data[$e['name']] );
+ $this->data[$e['name']] = call_user_func( $e['filter'], $this->data[$e['name']], $e['name'] );
endif;
+
+ // Global filter
+ if ( isset($this->filter) ) :
+
+ if ( is_array($this->filter) ) :
+ foreach ( $this->filter as $filter ) :
+ if ( is_callable($filter) ) :
+ $this->data[$e['name']] = call_user_func( $filter, $this->data[$e['name']], $e['name'] );
+ endif;
+ endforeach;
+ else :
+ if ( is_callable($this->filter) ) :
+ $this->data[$e['name']] = call_user_func( $this->filter, $this->data[$e['name']], $e['name'] );
+ endif;
+ endif;
+
+ endif;
+
// Field is empty
if ( isset($e['required']) && !$this->data[$e['name']] ) :
@@ -1046,14 +1073,14 @@ protected function checkfield( $e, $return_error_message = FALSE )
endif;
- // Filter Validation
+ // PHP Validation
if ( isset($e['validate']) ) :
if ( is_array($e['validate'])) :
foreach ( $e['validate'] as $filter ) :
- if ( FALSE === filter_var($this->data[$e['name']], $this->filter[$e['validate']], $this->filter_flag[$e['validate_flag']]) ) :
+ if ( FALSE === filter_var($this->data[$e['name']], $this->validate_dictonary[$e['validate']], $this->validate_dictonary_flags[$e['validate_flag']]) ) :
if ( TRUE === $return_error_message ) :
return $this->_render_error( $e['data-validate-message'] );
else :
@@ -1065,7 +1092,7 @@ protected function checkfield( $e, $return_error_message = FALSE )
else :
$this->filter[$filter];
- if ( FALSE === filter_var($this->data[$e['name']], $this->filter[$e['validate']], $this->filter_flag[$e['validate_flag']]) ) :
+ if ( FALSE === filter_var($this->data[$e['name']], $this->validate_dictonary[$e['validate']], $this->validate_flag[$e['validate_flag']]) ) :
if ( TRUE === $return_error_message ) :
return $this->_render_error( $e['data-validate-message'] );
else :
View
@@ -9,8 +9,12 @@
require_once( '../class.formz.php' );
# Demo function
-function validate() {
- return true;
+function filter( $data ) {
+ echo $data['string'];
+}
+
+function filter_function( $str) {
+ return str_replace(' 123', '', $str );
}
# Include Header
@@ -21,31 +25,30 @@ function validate() {
<h2>Arguments</h2>
<dl class="clearfix">
- <dt>validate</dt>
- <dd>Validate name</dd>
- <dt>validate_flag</dt>
- <dd>Validate flag</dd>
- <dt>validate_message</dt>
- <dd>Error message if filter can't validate</dd>
+ <dt>filter</dt>
+ <dd>callback function</dd>
</dl>
<h2>Filtering</h2>
<?php
# Init
-$form = new formz('id=first&callback=validate&success_message=Test passed');
+$form = new formz('id=first&callback=filter&success_message=');
$form->sendform = false;
$form->error_position = 'after';
# Fields
-$form->input('name=email&label=E-Mail:');
-$form->button('label=Validate&type=submit&name=send');
-# Validation
-$form->required('name=email&error_message=Please enter something&validate=FILTER_VALIDATE_EMAIL&validate_message=Falsches Format');
+$form->input('name=string&value=Hello World 123!&label=Filter&filter=filter_function');
+$form->button('label=Remove \'123\'&type=submit&name=send');
# Output
$form->render();
?>
<pre><code><?php highlight_string('<?php
+function filter_function( $str) {
+ return str_replace(\' 123\', \'\', $str );
+}
+
+$form->input(\'name=string&value=Hello World 123!&label=Filter&filter=filter_function\');
?>') ?></code></pre>
View
@@ -17,10 +17,12 @@ function validate() {
<dl class="clearfix">
<dt>label</dt>
<dd>Label text</dd>
+ <dt>HTML attributes</dt>
+ <dd>You can pass any valid HTML attriubte in the string, even data- attributes!</dd>
<dt>wrap</dt>
- <dd>Possible values: both (default), before, after or none; Should the wrapping element included?<br>You can pass any html attribute of input with a value, even data-attributes</dd>
- <dt>note:</dt>
- <dd>If you use ',', '&amp;' or '=' in an attribute you have to escape them</dd>
+ <dd>Possible values: both (default), before, after or none; Should the wrapping element included?</dd>
+ <dt><small>note:</small></dt>
+ <dd><small>If you use ',', '&amp;' or '=' in an attribute you have to escape them</small></dd>
</dl>
<h2>Input</h2>
@@ -95,8 +97,8 @@ function validate() {
<?php
$form = new formz();
$form->id = 'default';
-$form->input('name=defaultvalue&label=Input:&default=This is a default value');
-$form->html('<pre><code>' . highlight_string('<?php $form->input(\'name=input&label=Input:&default=This is a default value\'); ?>',true) . '</code></pre>');
+$form->input('name=defaultvalue&label=Input:&value=This is a default value');
+$form->html('<pre><code>' . highlight_string('<?php $form->input(\'name=input&label=Input:&value=This is a default value\'); ?>',true) . '</code></pre>');
$form->render();
?>
View
@@ -0,0 +1,35 @@
+<?php
+$path = '../';
+$title = '&raquo; Mail';
+require_once( '../class.formz.php' );
+mysql_connect('localhost', 'root', 'root');
+mysql_select_db( 'test' );
+?>
+
+<?php require_once('../include/header.php'); ?>
+
+<h2>Mail</h2>
+<?php
+$form = new formz;
+$form->from = 'My Website';
+$form->from_email = 'noreply@example.com';
+$form->to = 'test@example.com';
+$form->subject = 'Look I got spam!!!';
+$form->textarea('name=content&title=Content');
+$form->button('label=Send&type=submit');
+?>
+
+<pre><code><?php highlight_string('<?php
+$form = new formz;
+$form->from = \'My Website\';
+$form->from_email = \'noreply@example.com\';
+$form->to = \'test@example.com\';
+$form->subject = \'Look I got spam!!!\';
+$form->textarea(\'name=content&title=Content\');
+$form->button(\'label=Send&type=submit\');
+?>') ?>
+</code></pre>
+
+
+
+<?php require_once('../include/footer.php') ?>
View
@@ -9,14 +9,16 @@
<dl class="clearfix">
<dt>label</dt>
<dd>Label text</dd>
+ <dt>HTML attributes</dt>
+ <dd>You can pass any valid HTML attriubte in the string, even data- attributes!</dd>
<dt>value</dt>
- <dd>Comma seperated values; Use realvalue::Labelvalue style, if the label and values are different</dd>
+ <dd>Comma seperated values;<br>Use realvalue::Label style, if the label and values are different<br>Use $::Label for input field</dd>
<dt>align</dt>
<dd>horizontal (default) or vertical</dd>
<dt>checked</dt><dd>Comma seperated values of checked values</dd>
<dt>wrap</dt><dd>Possible values: both (default), before, after or none; Should the wrapping element included?<br>You can pass any html attribute of input with a value, even data-attributes</dd>
- <dt>note:</dt>
- <dd>If you use ',', '&amp;' or '=' in an attribute you have to escape them</dd>
+ <dt><small>note:</small></dt>
+ <dd><small>If you use ',', '&amp;' or '=' in an attribute you have to escape them</small></dd>
</dl>
<h2>Default</h2>
View
@@ -9,16 +9,16 @@
<dl class="clearfix">
<dt>label</dt>
<dd>Label text</dd>
- <dt>value</dt>
- <dd>Comma seperated values; Use realvalue::Labelvalue style, if the label and values are different</dd>
+ <dt>HTML attributes</dt>
+ <dd>You can pass any valid HTML attriubte in the string, even data- attributes!</dd>
<dt>default</dt>
- <dd>Prechecked fields</dd>
+ <dd>empty value options label</dd>
<dt>selected</dt>
<dd>Comma seperated values of selected values</dd>
<dt>wrap</dt>
<dd>Possible values: both (default), before, after or none; Should the wrapping element included?<br>You can pass any html attribute of input with a value, even data-attributes</dd>
- <dt>note:</dt>
- <dd>If you use ',', '&amp;' or '=' in an attribute you have to escape them</dd>
+ <dt><small>note:</small></dt>
+ <dd><small>If you use ',', '&amp;' or '=' in an attribute you have to escape them</small></dd>
</dl>
<h2>Default</h2>
View
@@ -10,12 +10,12 @@
<dt>Arguments</dt>
<dt>label</dt>
<dd>Label text</dd>
- <dt>value</dt>
- <dd>Comma seperated values; Use realvalue::Labelvalue style, if the label and values are different</dd>
+ <dt>HTML attributes</dt>
+ <dd>You can pass any valid HTML attriubte in the string, even data- attributes!</dd>
<dt>wrap</dt>
<dd>Possible values: both (default), before, after or none; Should the wrapping element included?<br>You can pass any html attribute of input with a value, even data-attributes</dd>
- <dt>note:</dt>
- <dd>If you use ',', '&amp;' or '=' in an attribute you have to escape them</dd>
+ <dt><small>note:</small></dt>
+ <dd><small>If you use ',', '&amp;' or '=' in an attribute you have to escape them</small></dd>
</dl>
<h2>Default</h2>

0 comments on commit 48b29ce

Please sign in to comment.