From 3515752b8e73deb49fd35412173e5955f484c862 Mon Sep 17 00:00:00 2001 From: Mirko De Grandis Date: Wed, 24 Sep 2014 00:22:42 +0200 Subject: [PATCH 001/562] modified: src/form.php --- src/form.php | 375 +++++++++++++++++++++++++++++++++++---------------- 1 file changed, 258 insertions(+), 117 deletions(-) diff --git a/src/form.php b/src/form.php index 2c514672..e934efc9 100644 --- a/src/form.php +++ b/src/form.php @@ -8,8 +8,8 @@ ini_set('display_startup_errors', TRUE); /* - * PHP Forms API library configuration - */ + * PHP Forms API library configuration + */ define('FORMS_DEFAULT_PREFIX', '
'); define('FORMS_DEFAULT_SUFFIX', '
'); @@ -25,7 +25,7 @@ class cs_form { - + protected $form_id = 'cs_form'; protected $form_token = ''; protected $action = ''; @@ -41,23 +41,23 @@ class cs_form { protected $valid = TRUE; protected $submit = ''; protected $error = ''; - + protected $fields = array(); - + public function __construct($options = array()) { foreach ($options as $name => $value) { $this->$name = $value; } if (empty($this->submit)) { $this->submit = "{$this->form_id}_submit"; - } + } $sid = session_id(); if (!empty($sid)) { $this->form_token = sha1(mt_rand(0, 1000000)); - $_SESSION['form_token'][$this->form_token] = $_SERVER['REQUEST_TIME']; - } + $_SESSION['form_token'][$this->form_token] = $_SERVER['REQUEST_TIME']; + } } - + // Warning: some messy logic in calling process->submit->values public function values() { if (!$this->processed) { @@ -69,7 +69,7 @@ public function values() { } return $output; } - + public function reset() { foreach ($this->fields as $name => $field) { $field->reset(); @@ -80,11 +80,11 @@ public function reset() { $this->validated = FALSE; $this->submitted = FALSE; } - + public function is_submitted() { return $this->submitted; } - + public function process() { if (!$this->processed) { $request = ($this->method == 'post') ? $_POST : $_GET; @@ -113,9 +113,9 @@ public function process() { } } } - - public function valid() { - if ($this->validated) { + + public function valid() { + if ($this->validated) { return $this->valid; } if (!isset($_REQUEST['form_id'])) { @@ -142,7 +142,7 @@ public function valid() { $this->validated = TRUE; return $this->valid; } - + public function add_field($name, $field) { if (!is_object($field)) { $field_type = isset($field['type']) ? "cs_{$field['type']}" : 'cs_textfield'; @@ -150,11 +150,11 @@ public function add_field($name, $field) { } $this->fields[$name] = $field; } - + public function show_errors() { return empty($this->error) ? '' : "
  • {$this->error}
  • "; } - + public function render() { $output = $this->prefix; if (!$this->valid()) { @@ -178,15 +178,15 @@ public function render() { $output .= "\n"; return $output . $this->suffix; } - + public static function validate_required($value = NULL) { if (!empty($value)) { return TRUE; } else { return "%t is required"; } - } - + } + public static function validate_max_length($value, $options) { if (strlen($value) > $options) { return "Maximum length of %t is {$options}"; @@ -202,7 +202,7 @@ public static function validate_min_length($value, $options) { public static function validate_exact_length($value, $options) { if (strlen($value) != $options) { return "%t must be {$options} characters long."; - } + } return TRUE; } public static function validate_alpha($value) { @@ -211,35 +211,35 @@ public static function validate_alpha($value) { } return TRUE; } - - protected function validate_alpha_numeric($value) { - if (!preg_match("/^([a-z0-9])+$/i", $value)) { - return "%t must only contain alpha numeric characters."; - } - return TRUE; - } - - protected function validate_alpha_dash($value) { - if (!preg_match("/^([-a-z0-9_-])+$/i", $value)) { - return "%t must contain only alpha numeric characters, underscore, or dashes"; - } - return TRUE; - } - - protected function validate_numeric($value) { - if (!is_numeric($value)) { - return "%t must be numeric."; - } - return TRUE; - } - - protected function validate_integer($value) { - if (!preg_match( '/^[\-+]?[0-9]+$/', $value)) { - return "%t must be an integer."; - } - return TRUE; - } - + + protected function validate_alpha_numeric($value) { + if (!preg_match("/^([a-z0-9])+$/i", $value)) { + return "%t must only contain alpha numeric characters."; + } + return TRUE; + } + + protected function validate_alpha_dash($value) { + if (!preg_match("/^([-a-z0-9_-])+$/i", $value)) { + return "%t must contain only alpha numeric characters, underscore, or dashes"; + } + return TRUE; + } + + protected function validate_numeric($value) { + if (!is_numeric($value)) { + return "%t must be numeric."; + } + return TRUE; + } + + protected function validate_integer($value) { + if (!preg_match( '/^[\-+]?[0-9]+$/', $value)) { + return "%t must be an integer."; + } + return TRUE; + } + public static function validate_match($value, $options) { $other = cs_form::scan_array($options, $_REQUEST); if ($value != $other) { @@ -247,7 +247,7 @@ public static function validate_match($value, $options) { } return TRUE; } - + public static function validate_file_extension($value, $options) { $options = explode(',', $options); $ext = substr(strrchr($value['filepath'], '.'), 1); @@ -269,13 +269,13 @@ public static function validate_max_file_size($value, $options) { } return TRUE; } - + private static function format_bytes($size) { $units = array(' B', ' KB', ' MB', ' GB', ' TB'); for ($i = 0; $size >= 1024 && $i < 4; $i++) $size /= 1024; return round($size, 2).$units[$i]; } - + public static function validate_email($email) { if (empty($email)) return TRUE; $check_dns = FORMS_VALIDATE_EMAIL_DNS; @@ -307,14 +307,14 @@ public static function validate_email($email) { } if (in_array($domain, $blocked_domains)) { return "%t is not a valid email. Domain name is in list of disallowed domains."; - } + } if ($check_dns && !(checkdnsrr($domain,"MX") || checkdnsrr($domain,"A"))) { return "%t is not a valid email. Domain name not found in DNS."; } } return TRUE; } - + public static function process_trim($text) { return trim($text); } @@ -324,18 +324,18 @@ public static function process_ltrim($text) { public static function process_rtrim($text) { return rtrim($text); } - + private static function _validate_utf8($text) { if (strlen($text) == 0) { return TRUE; } return (preg_match('/^./us', $text) == 1); } - + public static function process_xss_weak($string) { return filter_xss($string, array('a|abbr|acronym|address|b|bdo|big|blockquote|br|caption|cite|code|col|colgroup|dd|del|dfn|div|dl|dt|em|h1|h2|h3|h4|h5|h6|hr|i|img|ins|kbd|li|ol|p|pre|q|samp|small|span|strong|sub|sup|table|tbody|td|tfoot|th|thead|tr|tt|ul|var')); } - + public static function process_xss($string, $allowed_tags = FORMS_XSS_ALLOWED_TAGS) { // Only operate on valid UTF-8 strings. This is necessary to prevent cross // site scripting issues on Internet Explorer 6. @@ -368,7 +368,7 @@ public static function process_xss($string, $allowed_tags = FORMS_XSS_ALLOWED_TA > # just a > )%x', 'cs_form::_filter_xss_split', $string); } - + private static function _filter_xss_split($m, $store = FALSE) { static $allowed_html; @@ -419,12 +419,12 @@ private static function _filter_xss_split($m, $store = FALSE) { return "<$elem$attr2$xhtml_slash>"; } - + public static function process_plain($text) { // if using PHP < 5.2.5 add extra check of strings for valid UTF-8 return htmlspecialchars($text, ENT_QUOTES, 'UTF-8'); } - + public static function attributes($attributes) { if (is_array($attributes)) { $t = ''; @@ -434,7 +434,7 @@ public static function attributes($attributes) { return $t; } } - + private static function scan_array($string, $array) { list($key, $rest) = preg_split('/[[\]]/', $string, 2, PREG_SPLIT_NO_EMPTY); if ( $key && $rest ) { @@ -445,7 +445,7 @@ private static function scan_array($string, $array) { return FALSE; } } - + } class cs_field { @@ -466,7 +466,7 @@ class cs_field { protected $weight = 0; protected $value = ''; protected $error = ''; - + public function __construct($options = array()) { foreach ($options as $name => $value) { $this->$name = $value; @@ -485,11 +485,11 @@ public function reset() { public function get_weight() { return $this->weight; } - + public function process($value) { $this->value = $value; } - + public function preprocess($process_type = "preprocess") { foreach ($this->$process_type as $processor) { $processor = "process_{$processor}"; @@ -503,7 +503,7 @@ public function preprocess($process_type = "preprocess") { public function postprocess() { $this->preprocess("postprocess"); } - + public function valid() { foreach ($this->validate as $validator) { $matches = array(); @@ -522,42 +522,109 @@ public function valid() { } return TRUE; } - + public function show_errors() { return empty($this->error) ? '' : "
  • {$this->error}
  • "; } + + public function get_attributes($reserved_arr = array('type','name','id','value')){ + return $this->get_attributes_string($this->attributes, $reserved_arr); + } + + public function get_attributes_string( $attributes_arr, $reserved_arr = array('type','name','id','value')){ + $attributes = ''; + foreach ($reserved_arr as $key => $reserved) { + if(isset($attributes_arr[$reserved])) unset($attributes_arr[$reserved]); + } + foreach ($attributes_arr as $key => $value) { + if(!is_string($value)) continue; + $value = cs_form::process_plain($value); + $attributes .= " {$key}=\"{$value}\""; + } + $attributes = trim($attributes); + return empty($attributes) ? '' : ' ' . $attributes; + } + + public function getPrefix(){ + if (!empty($this->error)) { + if(preg_match("/class=\".*?\"/i", $this->prefix)){ + return preg_replace("/class=\"(.*?)\"/i", "class=\"\\1 error\"", $this->prefix); + } + } + return $this->suffix; + } + public function getSuffix(){ + return $this->suffix; + } + } class cs_submit extends cs_field { - + public function render($name) { if (empty($this->value)) { $this->value = 'Submit'; } + + $attributes = $this->get_attributes(array('type','name','id','value')); $output = $this->prefix; - $output .= "value}\" />\n"; + $output .= "value}\"{$attributes} />\n"; return $output . $this->suffix; } + } -class cs_textfield extends cs_field { - +class cs_reset extends cs_field { + public function render($name) { + if (empty($this->value)) { + $this->value = 'Reset'; + } + + $attributes = $this->get_attributes(array('type','name','id','value')); $output = $this->prefix; - $this->attributes['class'] = 'textfield'; + $output .= "value}\"{$attributes} />\n"; + return $output . $this->suffix; + } + +} + +class cs_markup extends cs_field { + + public function render($name) { + $output = $this->prefix; + $output .= $this->value; + return $output . $this->suffix; + } + + public function valid() { + return TRUE; + } +} + +class cs_textfield extends cs_field { + + public function render($name) { + $output = $this->getPrefix(); + $this->attributes['class'] = (isset($this->attributes['class']) ? $this->attributes['class'].' ':'').'textfield'; if (!empty($this->error)) { $this->attributes['class'] .= ' error'; } + + $attributes = $this->get_attributes(array('type','name','id','value')); + $required = (in_array('required', $this->validate)) ? ' *' : ''; - $output .= "\n"; - $output .= "value}\" size=\"{$this->size}\" class=\"{$this->attributes['class']}\" />\n"; + if (!empty($this->title)) { + $output .= "\n"; + } + $output .= "value}\"{$attributes} />\n"; if (!empty($this->description)) { $output .= "
    {$this->description}
    "; } - return $output . $this->suffix; + return $output . $this->getSuffix(); } - + } class cs_textarea extends cs_field { @@ -565,25 +632,45 @@ class cs_textarea extends cs_field { protected $rows = 5; public function render($name) { - $output = $this->prefix; - $output .= "\n"; - $output .= ""; if (!empty($this->description)) { $output .= "
    {$this->description}
    "; } - return $output . $this->suffix; + return $output . $this->getSuffix(); } } class cs_password extends cs_field { public function render($name) { - $output = $this->prefix; - $output .= "\n"; - $output .= "size}\" />\n"; - return $output . $this->suffix; + $output = $this->getPrefix(); + + $this->attributes['class'] = (isset($this->attributes['class']) ? $this->attributes['class'].' ':'').'password'; + if (!empty($this->error)) { + $this->attributes['class'] .= ' error'; + } + $attributes = $this->get_attributes(array('type','name','id','value')); + if (!empty($this->title)) { + $output .= "\n"; + } + $output .= "\n"; + if (!empty($this->description)) { + $output .= "
    {$this->description}
    "; + } + return $output . $this->getSuffix(); } } @@ -592,8 +679,17 @@ class cs_select extends cs_field { protected $multiple = FALSE; public function render($name) { - $output = $this->prefix; - $output .= "\n"; + $output = $this->getPrefix(); + + $this->attributes['class'] = (isset($this->attributes['class']) ? $this->attributes['class'].' ':'').'password'; + if (!empty($this->error)) { + $this->attributes['class'] .= ' error'; + } + $attributes = $this->get_attributes(array('type','name','id','value')); + + if (!empty($this->title)) { + $output .= "\n"; + } $extra = ($this->multiple) ? ' multiple' : ''; $field_name = ($this->multiple) ? "{$name}[]" : $name; $output .= "\n"; - return $output . $this->suffix; - } + if (!empty($this->description)) { + $output .= "
    {$this->description}
    "; + } + return $output . $this->getSuffix(); + } } class cs_radios extends cs_field { public function render($name) { - $output = $this->prefix; - $output .= "\n"; + $output = $this->getPrefix(); + if (!empty($this->title)) { + $output .= "\n"; + } foreach ($this->options as $key => $value) { + $attributes = $this->get_attributes(array('type','name','id','value')); + if(is_array($value) && isset($value['attributes'])){ + $attributes = $this->get_attributes_string($value['attributes'],array('type','name','id','value')); + } + if(is_array($value)){ + $value = $value['value']; + } + $checked = ($this->value == $key) ? ' checked=\"checked\"' : ''; - $output .= "\n"; + $output .= "\n"; } - return $output . $this->suffix; + if (!empty($this->description)) { + $output .= "
    {$this->description}
    "; + } + return $output . $this->getSuffix(); } } class cs_checkboxes extends cs_field { public function render($name) { - $output = $this->prefix; + if(!is_array($this->default_value)) { + $this->default_value = array($this->default_value); + } + + $output = $this->getPrefix(); if (!empty($this->title)) { $output .= "\n"; } foreach ($this->options as $key => $value) { + + $attributes = $this->get_attributes(array('type','name','id','value')); + if(is_array($value) && isset($value['attributes'])){ + $attributes = $this->get_attributes_string($value['attributes'],array('type','name','id','value')); + } + if(is_array($value)){ + $value = $value['value']; + } + $checked = (is_array($this->default_value) && in_array($key, $this->default_value)) ? ' checked=\"checked\"' : ''; - $output .= "\n"; + $output .= "\n"; } - return $output . $this->suffix; + if (!empty($this->description)) { + $output .= "
    {$this->description}
    "; + } + return $output . $this->getSuffix(); } } -class cs_file extends cs_field { +class cs_file extends cs_field { protected $uploaded = FALSE; public function __construct($options = array()) { @@ -642,15 +770,25 @@ public function __construct($options = array()) { } public function render($name) { - $output = $this->prefix; + $output = $this->getPrefix(); + + $this->attributes['class'] = (isset($this->attributes['class']) ? $this->attributes['class'].' ':'').'file'; + if (!empty($this->error)) { + $this->attributes['class'] .= ' error'; + } + $attributes = $this->get_attributes(array('type','name','id','size')); + if (!empty($this->title)) { $output .= "\n"; } - $output .= ""; + $output .= ""; $output .= "size}\" />"; - return $output . $this->suffix; + if (!empty($this->description)) { + $output .= "
    {$this->description}
    "; + } + return $output . $this->getSuffix(); } - + public function process($value, $name) { $this->value = array( 'filepath' => $this->destination .'/'. basename($_FILES[$name]['name']), @@ -663,8 +801,8 @@ public function process($value, $name) { $this->uploaded = TRUE; } } - - public function valid() { + + public function valid() { if ($this->uploaded) { return TRUE; } @@ -674,12 +812,12 @@ public function valid() { class cs_fieldset extends cs_field { - + protected $collapsible = FALSE; protected $collapsed = FALSE; - + protected $fields = array(); - + public function add_field($name, $field) { if (!is_object($field)) { $field_type = isset($field['type']) ? "cs_{$field['type']}" : 'cs_textfield'; @@ -687,7 +825,7 @@ public function add_field($name, $field) { } $this->fields[$name] = $field; } - + public function values() { $output = array(); foreach ($fields as $name => $field) { @@ -695,10 +833,10 @@ public function values() { } return $output; } - + public function render($parent_name) { $output = $this->prefix; - $this->attributes['class'] = 'fieldset'; + $this->attributes['class'] = (isset($this->attributes['class']) ? $this->attributes['class'].' ':'').'fieldset'; if ($this->collapsible) { $this->attributes['class'] .= ' collapsible'; if ($this->collapsed) { @@ -707,13 +845,18 @@ public function render($parent_name) { $this->attributes['class'] .= ' expanded'; } } - $output .= "
    attributes['class']}\">\n{$this->title}\n
    \n"; + $attributes = $this->get_attributes(array('type','name','id','value')); + $output .= "\n"; + if (!empty($this->title)) { + $output .= "{$this->title}\n"; + } + $output .= "
    \n"; foreach ($this->fields as $name => $field) { $output .= $field->render("{$parent_name}[{$name}]"); } return $output ."
    \n". $this->suffix; } - + public function preprocess() { foreach ($this->fields as $field) { $field->preprocess(); @@ -724,7 +867,7 @@ public function process($values) { $this->fields[$name]->process($value); } } - + public function valid() { $valid = TRUE; foreach ($this->fields as $field) { @@ -741,12 +884,10 @@ public function show_errors() { } return $output; } - + public function reset() { foreach ($this->fields as $field) { $field->reset(); } - } + } } - - From c046450bd5ccc61302a6b97978830e423e71be52 Mon Sep 17 00:00:00 2001 From: Mirko De Grandis Date: Wed, 24 Sep 2014 23:15:06 +0200 Subject: [PATCH 002/562] modified: src/form.php --- examples/contact.php | 15 +++- src/form.php | 162 +++++++++++++++++++++++++++++++------------ 2 files changed, 129 insertions(+), 48 deletions(-) diff --git a/examples/contact.php b/examples/contact.php index d25cd566..8e4bcb43 100644 --- a/examples/contact.php +++ b/examples/contact.php @@ -1,4 +1,4 @@ - array('xss'), 'title' => 'Your message', )); +$form->add_field('hidden1', array( + 'type' => 'hidden', + 'default_value' => 'aaaa', +)); +$form->add_field('markup1', array( + 'type' => 'markup', + 'value' => 'aaaa', +)); $form->add_field('submit', array( 'type' => 'submit', + 'value' => 'Send', )); // Submit function to call when the form is submitted and passes validation. -// This is where you would send the email (using PHP mail function) +// This is where you would send the email (using PHP mail function) // as this is not a real example I'm just outputting the values for now. function contact_submit(&$form) { $form_values = $form->values(); print_r($form_values); // Reset the form if you want it to display again. // $form->reset(); -} +} ?> diff --git a/src/form.php b/src/form.php index e934efc9..3ffd0a0a 100644 --- a/src/form.php +++ b/src/form.php @@ -540,21 +540,23 @@ public function get_attributes_string( $attributes_arr, $reserved_arr = array('t foreach ($attributes_arr as $key => $value) { if(!is_string($value)) continue; $value = cs_form::process_plain($value); - $attributes .= " {$key}=\"{$value}\""; + if(!empty($value)){ + $attributes .= " {$key}=\"{$value}\""; + } } $attributes = trim($attributes); return empty($attributes) ? '' : ' ' . $attributes; } - public function getPrefix(){ + public function get_prefix(){ if (!empty($this->error)) { if(preg_match("/class=\".*?\"/i", $this->prefix)){ - return preg_replace("/class=\"(.*?)\"/i", "class=\"\\1 error\"", $this->prefix); + return preg_replace("/class=\"(.*?)\"/i", "class=\"\${1} error\"", $this->prefix); } } - return $this->suffix; + return $this->prefix; } - public function getSuffix(){ + public function get_suffix(){ return $this->suffix; } @@ -562,40 +564,91 @@ public function getSuffix(){ class cs_submit extends cs_field { + public function __construct($options = array()) { + foreach ($options as $name => $value) { + $this->$name = $value; + } + } + public function render($name) { if (empty($this->value)) { $this->value = 'Submit'; } - - $attributes = $this->get_attributes(array('type','name','id','value')); - $output = $this->prefix; + $this->attributes['class'] = trim('submit '.(isset($this->attributes['class']) ? $this->attributes['class'] : '')); + if($this->disabled == TRUE) $this->attributes['disabled']='disabled'; + $attributes = $this->get_attributes(); + $output = $this->get_prefix(); $output .= "value}\"{$attributes} />\n"; - return $output . $this->suffix; + return $output . $this->get_suffix(); + } + + public function valid() { + return TRUE; } } class cs_reset extends cs_field { + public function __construct($options = array()) { + foreach ($options as $name => $value) { + $this->$name = $value; + } + } + public function render($name) { if (empty($this->value)) { $this->value = 'Reset'; } - - $attributes = $this->get_attributes(array('type','name','id','value')); - $output = $this->prefix; + $this->attributes['class'] = trim('reset '.(isset($this->attributes['class']) ? $this->attributes['class'] : '')); + if($this->disabled == TRUE) $this->attributes['disabled']='disabled'; + $attributes = $this->get_attributes(); + $output = $this->get_prefix(); $output .= "value}\"{$attributes} />\n"; - return $output . $this->suffix; + return $output . $this->get_suffix(); + } + + public function valid() { + return TRUE; + } + +} + +class cs_button extends cs_field { + + public function __construct($options = array()) { + foreach ($options as $name => $value) { + $this->$name = $value; + } + } + + public function render($name) { + $this->attributes['class'] = trim('button '.(isset($this->attributes['class']) ? $this->attributes['class'] : '')); + if($this->disabled == TRUE) $this->attributes['disabled']='disabled'; + $attributes = $this->get_attributes(); + $output = $this->get_prefix(); + $output .= "\n"; + return $output . $this->get_suffix(); + } + + public function valid() { + return TRUE; } } class cs_markup extends cs_field { + public function __construct($options = array()) { + foreach ($options as $name => $value) { + $this->$name = $value; + } + } + public function render($name) { - $output = $this->prefix; + $output = $this->get_prefix(); $output .= $this->value; - return $output . $this->suffix; + return $output . $this->get_suffix(); } public function valid() { @@ -603,16 +656,26 @@ public function valid() { } } +class cs_hidden extends cs_field { + + public function render($name) { + $this->attributes['class'] = trim('hidden '.(isset($this->attributes['class']) ? $this->attributes['class'] : '')); + $attributes = $this->get_attributes(); + return "value}\"{$attributes} />\n"; + } + +} + class cs_textfield extends cs_field { public function render($name) { - $output = $this->getPrefix(); - $this->attributes['class'] = (isset($this->attributes['class']) ? $this->attributes['class'].' ':'').'textfield'; + $output = $this->get_prefix(); + $this->attributes['class'] = trim('textfield '.(isset($this->attributes['class']) ? $this->attributes['class'] : '')); if (!empty($this->error)) { $this->attributes['class'] .= ' error'; } - - $attributes = $this->get_attributes(array('type','name','id','value')); + if($this->disabled == TRUE) $this->attributes['disabled']='disabled'; + $attributes = $this->get_attributes(); $required = (in_array('required', $this->validate)) ? ' *' : ''; if (!empty($this->title)) { @@ -622,7 +685,7 @@ public function render($name) { if (!empty($this->description)) { $output .= "
    {$this->description}
    "; } - return $output . $this->getSuffix(); + return $output . $this->get_suffix(); } } @@ -632,13 +695,13 @@ class cs_textarea extends cs_field { protected $rows = 5; public function render($name) { - $output = $this->getPrefix(); + $output = $this->get_prefix(); - $this->attributes['class'] = (isset($this->attributes['class']) ? $this->attributes['class'].' ':'').'textarea'; + $this->attributes['class'] = trim('textarea '.(isset($this->attributes['class']) ? $this->attributes['class'] : '')); if (!empty($this->error)) { $this->attributes['class'] .= ' error'; } - + if($this->disabled == TRUE) $this->attributes['disabled']='disabled'; $attributes = $this->get_attributes(array('name','id','value','rows','cols')); if (!empty($this->title)) { $output .= "\n"; @@ -649,20 +712,21 @@ public function render($name) { if (!empty($this->description)) { $output .= "
    {$this->description}
    "; } - return $output . $this->getSuffix(); + return $output . $this->get_suffix(); } } class cs_password extends cs_field { public function render($name) { - $output = $this->getPrefix(); + $output = $this->get_prefix(); - $this->attributes['class'] = (isset($this->attributes['class']) ? $this->attributes['class'].' ':'').'password'; + $this->attributes['class'] = trim('password '.(isset($this->attributes['class']) ? $this->attributes['class'] : '')); if (!empty($this->error)) { $this->attributes['class'] .= ' error'; } - $attributes = $this->get_attributes(array('type','name','id','value')); + if($this->disabled == TRUE) $this->attributes['disabled']='disabled'; + $attributes = $this->get_attributes(); if (!empty($this->title)) { $output .= "\n"; } @@ -670,7 +734,7 @@ public function render($name) { if (!empty($this->description)) { $output .= "
    {$this->description}
    "; } - return $output . $this->getSuffix(); + return $output . $this->get_suffix(); } } @@ -679,20 +743,21 @@ class cs_select extends cs_field { protected $multiple = FALSE; public function render($name) { - $output = $this->getPrefix(); + $output = $this->get_prefix(); - $this->attributes['class'] = (isset($this->attributes['class']) ? $this->attributes['class'].' ':'').'password'; + $this->attributes['class'] = trim('select '.(isset($this->attributes['class']) ? $this->attributes['class'] : '')); if (!empty($this->error)) { $this->attributes['class'] .= ' error'; } - $attributes = $this->get_attributes(array('type','name','id','value')); + if($this->disabled == TRUE) $this->attributes['disabled']='disabled'; + $attributes = $this->get_attributes(); if (!empty($this->title)) { $output .= "\n"; } $extra = ($this->multiple) ? ' multiple' : ''; $field_name = ($this->multiple) ? "{$name}[]" : $name; - $output .= "\n"; foreach ($this->options as $key => $value) { $output .= "\n"; } @@ -700,18 +765,21 @@ public function render($name) { if (!empty($this->description)) { $output .= "
    {$this->description}
    "; } - return $output . $this->getSuffix(); + return $output . $this->get_suffix(); } } class cs_radios extends cs_field { public function render($name) { - $output = $this->getPrefix(); + $output = $this->get_prefix(); if (!empty($this->title)) { $output .= "\n"; } + + if($this->disabled == TRUE) $this->attributes['disabled']='disabled'; + foreach ($this->options as $key => $value) { - $attributes = $this->get_attributes(array('type','name','id','value')); + $attributes = $this->get_attributes(); if(is_array($value) && isset($value['attributes'])){ $attributes = $this->get_attributes_string($value['attributes'],array('type','name','id','value')); } @@ -725,7 +793,7 @@ public function render($name) { if (!empty($this->description)) { $output .= "
    {$this->description}
    "; } - return $output . $this->getSuffix(); + return $output . $this->get_suffix(); } } @@ -735,13 +803,16 @@ public function render($name) { $this->default_value = array($this->default_value); } - $output = $this->getPrefix(); + $output = $this->get_prefix(); if (!empty($this->title)) { $output .= "\n"; } + + if($this->disabled == TRUE) $this->attributes['disabled']='disabled'; + foreach ($this->options as $key => $value) { - $attributes = $this->get_attributes(array('type','name','id','value')); + $attributes = $this->get_attributes(); if(is_array($value) && isset($value['attributes'])){ $attributes = $this->get_attributes_string($value['attributes'],array('type','name','id','value')); } @@ -755,7 +826,7 @@ public function render($name) { if (!empty($this->description)) { $output .= "
    {$this->description}
    "; } - return $output . $this->getSuffix(); + return $output . $this->get_suffix(); } } @@ -770,12 +841,13 @@ public function __construct($options = array()) { } public function render($name) { - $output = $this->getPrefix(); + $output = $this->get_prefix(); - $this->attributes['class'] = (isset($this->attributes['class']) ? $this->attributes['class'].' ':'').'file'; + $this->attributes['class'] = trim('file '.(isset($this->attributes['class']) ? $this->attributes['class'] : '')); if (!empty($this->error)) { $this->attributes['class'] .= ' error'; } + if($this->disabled == TRUE) $this->attributes['disabled']='disabled'; $attributes = $this->get_attributes(array('type','name','id','size')); if (!empty($this->title)) { @@ -786,7 +858,7 @@ public function render($name) { if (!empty($this->description)) { $output .= "
    {$this->description}
    "; } - return $output . $this->getSuffix(); + return $output . $this->get_suffix(); } public function process($value, $name) { @@ -836,7 +908,7 @@ public function values() { public function render($parent_name) { $output = $this->prefix; - $this->attributes['class'] = (isset($this->attributes['class']) ? $this->attributes['class'].' ':'').'fieldset'; + $this->attributes['class'] = trim('fieldset '.(isset($this->attributes['class']) ? $this->attributes['class'] : '')); if ($this->collapsible) { $this->attributes['class'] .= ' collapsible'; if ($this->collapsed) { @@ -845,8 +917,8 @@ public function render($parent_name) { $this->attributes['class'] .= ' expanded'; } } - $attributes = $this->get_attributes(array('type','name','id','value')); - $output .= "\n"; + $attributes = $this->get_attributes(); + $output .= "
    \n"; if (!empty($this->title)) { $output .= "{$this->title}\n"; } From f4d413792d12bbe32b8928b0b23fcc5575a9cfa6 Mon Sep 17 00:00:00 2001 From: Mirko De Grandis Date: Thu, 25 Sep 2014 14:03:16 +0200 Subject: [PATCH 003/562] modified: examples/contact.php modified: src/form.php --- examples/contact.php | 1 + src/form.php | 66 +++++++++++++++++++++++++++++++------------- 2 files changed, 48 insertions(+), 19 deletions(-) diff --git a/examples/contact.php b/examples/contact.php index 8e4bcb43..d3c93f45 100644 --- a/examples/contact.php +++ b/examples/contact.php @@ -29,6 +29,7 @@ $form->add_field('markup1', array( 'type' => 'markup', 'value' => 'aaaa', + 'weight' => -10, )); $form->add_field('submit', array( 'type' => 'submit', diff --git a/src/form.php b/src/form.php index 3ffd0a0a..9d3a9a92 100644 --- a/src/form.php +++ b/src/form.php @@ -169,6 +169,9 @@ public function render() { foreach ($this->attributes as $key => $value) { $attributes .= " {$key}=\"{$value}\""; } + + usort($this->fields, 'cs_form::order_by_weight'); + $output .= "
    action}\" method=\"{$this->method}\"{$attributes}>\n"; foreach ($this->fields as $name => $field) { $output .= $field->render($name); @@ -446,6 +449,14 @@ private static function scan_array($string, $array) { } } + public static function order_by_weight($a, $b){ + if ($a->get_weight() == $b->get_weight()) { + // return 0; + return 1; + } + return ($a->get_weight() < $b->get_weight()) ? -1 : 1; + } + } class cs_field { @@ -466,6 +477,7 @@ class cs_field { protected $weight = 0; protected $value = ''; protected $error = ''; + protected $id = null; public function __construct($options = array()) { foreach ($options as $name => $value) { @@ -571,6 +583,7 @@ public function __construct($options = array()) { } public function render($name) { + $id = !empty($this->id) ? $this->id : $name; if (empty($this->value)) { $this->value = 'Submit'; } @@ -578,7 +591,7 @@ public function render($name) { if($this->disabled == TRUE) $this->attributes['disabled']='disabled'; $attributes = $this->get_attributes(); $output = $this->get_prefix(); - $output .= "value}\"{$attributes} />\n"; + $output .= "value}\"{$attributes} />\n"; return $output . $this->get_suffix(); } @@ -597,6 +610,7 @@ public function __construct($options = array()) { } public function render($name) { + $id = !empty($this->id) ? $this->id : $name; if (empty($this->value)) { $this->value = 'Reset'; } @@ -604,7 +618,7 @@ public function render($name) { if($this->disabled == TRUE) $this->attributes['disabled']='disabled'; $attributes = $this->get_attributes(); $output = $this->get_prefix(); - $output .= "value}\"{$attributes} />\n"; + $output .= "value}\"{$attributes} />\n"; return $output . $this->get_suffix(); } @@ -623,11 +637,12 @@ public function __construct($options = array()) { } public function render($name) { + $id = !empty($this->id) ? $this->id : $name; $this->attributes['class'] = trim('button '.(isset($this->attributes['class']) ? $this->attributes['class'] : '')); if($this->disabled == TRUE) $this->attributes['disabled']='disabled'; $attributes = $this->get_attributes(); $output = $this->get_prefix(); - $output .= "\n"; + $output .= "\n"; return $output . $this->get_suffix(); } @@ -659,9 +674,10 @@ public function valid() { class cs_hidden extends cs_field { public function render($name) { + $id = !empty($this->id) ? $this->id : $name; $this->attributes['class'] = trim('hidden '.(isset($this->attributes['class']) ? $this->attributes['class'] : '')); $attributes = $this->get_attributes(); - return "value}\"{$attributes} />\n"; + return "value}\"{$attributes} />\n"; } } @@ -669,7 +685,9 @@ public function render($name) { class cs_textfield extends cs_field { public function render($name) { + $id = !empty($this->id) ? $this->id : $name; $output = $this->get_prefix(); + $this->attributes['class'] = trim('textfield '.(isset($this->attributes['class']) ? $this->attributes['class'] : '')); if (!empty($this->error)) { $this->attributes['class'] .= ' error'; @@ -679,9 +697,9 @@ public function render($name) { $required = (in_array('required', $this->validate)) ? ' *' : ''; if (!empty($this->title)) { - $output .= "\n"; + $output .= "\n"; } - $output .= "value}\"{$attributes} />\n"; + $output .= "value}\"{$attributes} />\n"; if (!empty($this->description)) { $output .= "
    {$this->description}
    "; } @@ -695,6 +713,7 @@ class cs_textarea extends cs_field { protected $rows = 5; public function render($name) { + $id = !empty($this->id) ? $this->id : $name; $output = $this->get_prefix(); $this->attributes['class'] = trim('textarea '.(isset($this->attributes['class']) ? $this->attributes['class'] : '')); @@ -704,9 +723,9 @@ public function render($name) { if($this->disabled == TRUE) $this->attributes['disabled']='disabled'; $attributes = $this->get_attributes(array('name','id','value','rows','cols')); if (!empty($this->title)) { - $output .= "\n"; + $output .= "\n"; } - $output .= ""; if (!empty($this->description)) { @@ -719,6 +738,7 @@ public function render($name) { class cs_password extends cs_field { public function render($name) { + $id = !empty($this->id) ? $this->id : $name; $output = $this->get_prefix(); $this->attributes['class'] = trim('password '.(isset($this->attributes['class']) ? $this->attributes['class'] : '')); @@ -728,9 +748,9 @@ public function render($name) { if($this->disabled == TRUE) $this->attributes['disabled']='disabled'; $attributes = $this->get_attributes(); if (!empty($this->title)) { - $output .= "\n"; + $output .= "\n"; } - $output .= "\n"; + $output .= "\n"; if (!empty($this->description)) { $output .= "
    {$this->description}
    "; } @@ -743,6 +763,7 @@ class cs_select extends cs_field { protected $multiple = FALSE; public function render($name) { + $id = !empty($this->id) ? $this->id : $name; $output = $this->get_prefix(); $this->attributes['class'] = trim('select '.(isset($this->attributes['class']) ? $this->attributes['class'] : '')); @@ -753,11 +774,11 @@ public function render($name) { $attributes = $this->get_attributes(); if (!empty($this->title)) { - $output .= "\n"; + $output .= "\n"; } $extra = ($this->multiple) ? ' multiple' : ''; $field_name = ($this->multiple) ? "{$name}[]" : $name; - $output .= "\n"; foreach ($this->options as $key => $value) { $output .= "\n"; } @@ -771,9 +792,10 @@ public function render($name) { class cs_radios extends cs_field { public function render($name) { + $id = !empty($this->id) ? $this->id : $name; $output = $this->get_prefix(); if (!empty($this->title)) { - $output .= "\n"; + $output .= "\n"; } if($this->disabled == TRUE) $this->attributes['disabled']='disabled'; @@ -788,7 +810,7 @@ public function render($name) { } $checked = ($this->value == $key) ? ' checked=\"checked\"' : ''; - $output .= "\n"; + $output .= "\n"; } if (!empty($this->description)) { $output .= "
    {$this->description}
    "; @@ -799,13 +821,14 @@ public function render($name) { class cs_checkboxes extends cs_field { public function render($name) { + $id = !empty($this->id) ? $this->id : $name; if(!is_array($this->default_value)) { $this->default_value = array($this->default_value); } $output = $this->get_prefix(); if (!empty($this->title)) { - $output .= "\n"; + $output .= "\n"; } if($this->disabled == TRUE) $this->attributes['disabled']='disabled'; @@ -821,7 +844,7 @@ public function render($name) { } $checked = (is_array($this->default_value) && in_array($key, $this->default_value)) ? ' checked=\"checked\"' : ''; - $output .= "\n"; + $output .= "\n"; } if (!empty($this->description)) { $output .= "
    {$this->description}
    "; @@ -841,6 +864,7 @@ public function __construct($options = array()) { } public function render($name) { + $id = !empty($this->id) ? $this->id : $name; $output = $this->get_prefix(); $this->attributes['class'] = trim('file '.(isset($this->attributes['class']) ? $this->attributes['class'] : '')); @@ -851,10 +875,10 @@ public function render($name) { $attributes = $this->get_attributes(array('type','name','id','size')); if (!empty($this->title)) { - $output .= "\n"; + $output .= "\n"; } $output .= ""; - $output .= "size}\" />"; + $output .= "size}\" />"; if (!empty($this->description)) { $output .= "
    {$this->description}
    "; } @@ -907,6 +931,7 @@ public function values() { } public function render($parent_name) { + $id = !empty($this->id) ? $this->id : $parent_name; $output = $this->prefix; $this->attributes['class'] = trim('fieldset '.(isset($this->attributes['class']) ? $this->attributes['class'] : '')); if ($this->collapsible) { @@ -918,10 +943,13 @@ public function render($parent_name) { } } $attributes = $this->get_attributes(); - $output .= "
    \n"; + $output .= "
    \n"; if (!empty($this->title)) { $output .= "{$this->title}\n"; } + + usort($this->fields, 'cs_form::order_by_weight'); + $output .= "
    \n"; foreach ($this->fields as $name => $field) { $output .= $field->render("{$parent_name}[{$name}]"); From eac81ba957000a1b02ba82d782128adce7fbe379 Mon Sep 17 00:00:00 2001 From: Mirko De Grandis Date: Fri, 26 Sep 2014 14:07:14 +0200 Subject: [PATCH 004/562] modified: examples/contact.php modified: src/form.php --- examples/contact.php | 31 +++++++++++++++++++++++-------- src/form.php | 5 +++-- 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/examples/contact.php b/examples/contact.php index d3c93f45..c0a14ca2 100644 --- a/examples/contact.php +++ b/examples/contact.php @@ -6,31 +6,46 @@ // Generate a simple contact form $form = new cs_form(array('form_id' => 'contact')); -$form->add_field('name', array( +$fieldset = new cs_fieldset(array('attributes'=>array( + 'style' => 'width: 400px', +))); +$fieldset->add_field('name', array( 'type' => 'textfield', 'validate' => array('required'), 'preprocess' => array('trim'), 'title' => 'Your name', + 'attributes' => array( + 'style' => 'width: 100%', + ), )); -$form->add_field('email', array( +$fieldset->add_field('email', array( 'type' => 'textfield', 'validate' => array('required', 'email'), 'title' => 'Your email address', + 'attributes' => array( + 'style' => 'width: 100%', + ), )); -$form->add_field('message', array( +$fieldset->add_field('message', array( 'type' => 'textarea', 'postprocess' => array('xss'), 'title' => 'Your message', + 'rows' => 10, + 'attributes' => array( + 'placeholder' => 'Type your message', + 'style' => 'width: 100%', + ), )); -$form->add_field('hidden1', array( - 'type' => 'hidden', - 'default_value' => 'aaaa', -)); -$form->add_field('markup1', array( +$fieldset->add_field('markup1', array( 'type' => 'markup', 'value' => 'aaaa', 'weight' => -10, )); +$form->add_field('fieldset', $fieldset); +$form->add_field('hidden1', array( + 'type' => 'hidden', + 'default_value' => 'aaaa', +)); $form->add_field('submit', array( 'type' => 'submit', 'value' => 'Send', diff --git a/src/form.php b/src/form.php index 9d3a9a92..1e74759e 100644 --- a/src/form.php +++ b/src/form.php @@ -119,7 +119,8 @@ public function valid() { return $this->valid; } if (!isset($_REQUEST['form_id'])) { - $this->valid = FALSE; + return NULL; + //$this->valid = FALSE; } else if ($_REQUEST['form_id'] == $this->form_id) { $sid = session_id(); if (!empty($sid)) { @@ -157,7 +158,7 @@ public function show_errors() { public function render() { $output = $this->prefix; - if (!$this->valid()) { + if ( $this->valid() === FALSE) { $output .= "
      "; $output .= $this->show_errors(); foreach ($this->fields as $field) { From a9c35cd9099bd19cf1ae5e8d5c935714c41c1318 Mon Sep 17 00:00:00 2001 From: Mirko De Grandis Date: Fri, 26 Sep 2014 14:20:27 +0200 Subject: [PATCH 005/562] modified: src/form.php --- src/form.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/form.php b/src/form.php index 1e74759e..7973f643 100644 --- a/src/form.php +++ b/src/form.php @@ -171,7 +171,7 @@ public function render() { $attributes .= " {$key}=\"{$value}\""; } - usort($this->fields, 'cs_form::order_by_weight'); + uasort($this->fields, 'cs_form::order_by_weight'); $output .= "action}\" method=\"{$this->method}\"{$attributes}>\n"; foreach ($this->fields as $name => $field) { @@ -949,7 +949,7 @@ public function render($parent_name) { $output .= "{$this->title}\n"; } - usort($this->fields, 'cs_form::order_by_weight'); + uasort($this->fields, 'cs_form::order_by_weight'); $output .= "
      \n"; foreach ($this->fields as $name => $field) { From 62d9faaff83b218c4eaadf9411cc493acdfd3210 Mon Sep 17 00:00:00 2001 From: Mirko De Grandis Date: Fri, 26 Sep 2014 15:16:08 +0200 Subject: [PATCH 006/562] modified: src/form.php --- src/form.php | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/src/form.php b/src/form.php index 7973f643..dd5abcb7 100644 --- a/src/form.php +++ b/src/form.php @@ -42,6 +42,7 @@ class cs_form { protected $submit = ''; protected $error = ''; + protected $insert_field_order = array(); protected $fields = array(); public function __construct($options = array()) { @@ -150,6 +151,7 @@ public function add_field($name, $field) { $field = new $field_type($field); } $this->fields[$name] = $field; + $this->insert_field_order[] = $name; } public function show_errors() { @@ -171,7 +173,15 @@ public function render() { $attributes .= " {$key}=\"{$value}\""; } - uasort($this->fields, 'cs_form::order_by_weight'); + // uasort($this->fields, 'cs_form::order_by_weight'); + + $insertorder = array_flip($this->insert_field_order); + $weights = array(); + foreach ($this->fields as $key => $elem) { + $weights[$key] = $elem->get_weight(); + $order[$key] = $insertorder[$key]; + } + array_multisort($weights, SORT_ASC, $order, SORT_ASC, $this->fields); $output .= "action}\" method=\"{$this->method}\"{$attributes}>\n"; foreach ($this->fields as $name => $field) { @@ -457,7 +467,6 @@ public static function order_by_weight($a, $b){ } return ($a->get_weight() < $b->get_weight()) ? -1 : 1; } - } class cs_field { @@ -913,6 +922,7 @@ class cs_fieldset extends cs_field { protected $collapsible = FALSE; protected $collapsed = FALSE; + protected $insert_field_order = array(); protected $fields = array(); public function add_field($name, $field) { @@ -921,6 +931,7 @@ public function add_field($name, $field) { $field = new $field_type($field); } $this->fields[$name] = $field; + $this->insert_field_order[] = $name; } public function values() { @@ -949,7 +960,14 @@ public function render($parent_name) { $output .= "{$this->title}\n"; } - uasort($this->fields, 'cs_form::order_by_weight'); + // uasort($this->fields, 'cs_form::order_by_weight'); + $insertorder = array_flip($this->insert_field_order); + $weights = array(); + foreach ($this->fields as $key => $elem) { + $weights[$key] = $elem->get_weight(); + $order[$key] = $insertorder[$key]; + } + array_multisort($weights, SORT_ASC, $order, SORT_ASC, $this->fields); $output .= "
      \n"; foreach ($this->fields as $name => $field) { From 7a0b132f87617c411288507a2eb2d2b56c7b3147 Mon Sep 17 00:00:00 2001 From: Mirko De Grandis Date: Fri, 26 Sep 2014 22:57:55 +0200 Subject: [PATCH 007/562] modified: examples/contact.php modified: src/form.php --- examples/contact.php | 5 +++ src/form.php | 100 ++++++++++++++++++++++++++----------------- 2 files changed, 65 insertions(+), 40 deletions(-) diff --git a/examples/contact.php b/examples/contact.php index c0a14ca2..539dc162 100644 --- a/examples/contact.php +++ b/examples/contact.php @@ -36,6 +36,11 @@ 'style' => 'width: 100%', ), )); +$fieldset->add_field('checkboxes', array( + 'type' => 'checkboxes', + 'options' => array(0=>'zero',1=>'one',2=>'two'), + 'default_value' => 1, +)); $fieldset->add_field('markup1', array( 'type' => 'markup', 'value' => 'aaaa', diff --git a/src/form.php b/src/form.php index dd5abcb7..28703be4 100644 --- a/src/form.php +++ b/src/form.php @@ -154,6 +154,14 @@ public function add_field($name, $field) { $this->insert_field_order[] = $name; } + public function get_fields(){ + return $this->fields; + } + + public function get_field($field_name){ + return isset($this->fields[$field_name]) ? $this->fields[$field_name] : NULL; + } + public function show_errors() { return empty($this->error) ? '' : "
    • {$this->error}
    • "; } @@ -462,8 +470,7 @@ private static function scan_array($string, $array) { public static function order_by_weight($a, $b){ if ($a->get_weight() == $b->get_weight()) { - // return 0; - return 1; + return 0; } return ($a->get_weight() < $b->get_weight()) ? -1 : 1; } @@ -917,14 +924,19 @@ public function valid() { } -class cs_fieldset extends cs_field { - - protected $collapsible = FALSE; - protected $collapsed = FALSE; +class cs_fields_container extends cs_field { protected $insert_field_order = array(); protected $fields = array(); + public function get_fields(){ + return $this->fields; + } + + public function get_field($field_name){ + return isset($this->fields[$field_name]) ? $this->fields[$field_name] : NULL; + } + public function add_field($name, $field) { if (!is_object($field)) { $field_type = isset($field['type']) ? "cs_{$field['type']}" : 'cs_textfield'; @@ -936,12 +948,53 @@ public function add_field($name, $field) { public function values() { $output = array(); - foreach ($fields as $name => $field) { + foreach ($this->fields as $name => $field) { $output[$name] = $field->values(); } return $output; } + public function preprocess() { + foreach ($this->fields as $field) { + $field->preprocess(); + } + } + public function process($values) { + foreach ($values as $name => $value) { + $this->fields[$name]->process($value); + } + } + + public function valid() { + $valid = TRUE; + foreach ($this->fields as $field) { + if (!$field->valid()) { + $valid = FALSE; + } + } + return $valid; + } + public function show_errors() { + $output = ""; + foreach ($this->fields as $field) { + $output .= $field->show_errors(); + } + return $output; + } + + public function reset() { + foreach ($this->fields as $field) { + $field->reset(); + } + } + +} + +class cs_fieldset extends cs_fields_container { + + protected $collapsible = FALSE; + protected $collapsed = FALSE; + public function render($parent_name) { $id = !empty($this->id) ? $this->id : $parent_name; $output = $this->prefix; @@ -976,37 +1029,4 @@ public function render($parent_name) { return $output ."
    \n". $this->suffix; } - public function preprocess() { - foreach ($this->fields as $field) { - $field->preprocess(); - } - } - public function process($values) { - foreach ($values as $name => $value) { - $this->fields[$name]->process($value); - } - } - - public function valid() { - $valid = TRUE; - foreach ($this->fields as $field) { - if (!$field->valid()) { - $valid = FALSE; - } - } - return $valid; - } - public function show_errors() { - $output = ""; - foreach ($this->fields as $field) { - $output .= $field->show_errors(); - } - return $output; - } - - public function reset() { - foreach ($this->fields as $field) { - $field->reset(); - } - } } From 20368dd66107a0632833609c136220b85c01a219 Mon Sep 17 00:00:00 2001 From: Mirko De Grandis Date: Sun, 28 Sep 2014 09:14:06 +0200 Subject: [PATCH 008/562] modified: examples/contact.php modified: src/form.php --- examples/contact.php | 10 +++++- src/form.php | 73 ++++++++++++++++++++++++++------------------ 2 files changed, 53 insertions(+), 30 deletions(-) diff --git a/examples/contact.php b/examples/contact.php index 539dc162..35a912c7 100644 --- a/examples/contact.php +++ b/examples/contact.php @@ -5,7 +5,10 @@ require '../src/form.php'; // Generate a simple contact form -$form = new cs_form(array('form_id' => 'contact')); +$form = new cs_form(array( + 'form_id' => 'contact', + 'attributes'=>array('enctype'=>'multipart/form-data') +)); $fieldset = new cs_fieldset(array('attributes'=>array( 'style' => 'width: 400px', ))); @@ -41,11 +44,16 @@ 'options' => array(0=>'zero',1=>'one',2=>'two'), 'default_value' => 1, )); +$fieldset->add_field('file', array( + 'type' => 'file', + 'destination' => dirname(__FILE__), +)); $fieldset->add_field('markup1', array( 'type' => 'markup', 'value' => 'aaaa', 'weight' => -10, )); + $form->add_field('fieldset', $fieldset); $form->add_field('hidden1', array( 'type' => 'hidden', diff --git a/src/form.php b/src/form.php index 28703be4..7e987de9 100644 --- a/src/form.php +++ b/src/form.php @@ -65,14 +65,14 @@ public function values() { $this->process(); } $output = array(); - foreach ($this->fields as $name => $field) { + foreach ($this->get_fields() as $name => $field) { $output[$name] = $field->values(); } return $output; } public function reset() { - foreach ($this->fields as $name => $field) { + foreach ($this->get_fields() as $name => $field) { $field->reset(); unset($_POST[$name]); } @@ -90,16 +90,24 @@ public function process() { if (!$this->processed) { $request = ($this->method == 'post') ? $_POST : $_GET; if (isset($request['form_id']) && $request['form_id'] == $this->form_id) { - foreach ($request as $name => $value) { - if (isset($this->fields[$name])) { - $this->fields[$name]->process($value, $name); + // foreach ($request as $name => $value) { + // if ( $this->get_field($name) !== NULL ) { + // $this->get_field($name)->process($value, $name); + // } + // } + + foreach ($this->get_fields() as $name => $field) { + if( $field instanceof cs_fields_container ) $this->get_field($name)->process($request); + else if ( !empty($request[$name]) ) { + $this->get_field($name)->process($request[$name], $name); } } + } $this->processed = TRUE; } if (!$this->preprocessors) { - foreach ($this->fields as $name => $field) { + foreach ($this->get_fields() as $name => $field) { $field->preprocess(); } } @@ -107,7 +115,7 @@ public function process() { $this->submitted = TRUE; $submit_function = $this->submit; if (function_exists($submit_function)) { - foreach ($this->fields as $name => $field) { + foreach ($this->get_fields() as $name => $field) { $field->postprocess(); } $submit_function($this, ($this->method == 'post') ? $_POST : $_GET); @@ -135,7 +143,7 @@ public function valid() { } } } - foreach ($this->fields as $field) { + foreach ($this->get_fields() as $field) { if (!$field->valid()) { $this->valid = FALSE; } @@ -154,7 +162,7 @@ public function add_field($name, $field) { $this->insert_field_order[] = $name; } - public function get_fields(){ + public function &get_fields(){ return $this->fields; } @@ -171,7 +179,7 @@ public function render() { if ( $this->valid() === FALSE) { $output .= "
      "; $output .= $this->show_errors(); - foreach ($this->fields as $field) { + foreach ($this->get_fields() as $field) { $output .= $field->show_errors(); } $output .= "
    "; @@ -185,14 +193,14 @@ public function render() { $insertorder = array_flip($this->insert_field_order); $weights = array(); - foreach ($this->fields as $key => $elem) { + foreach ($this->get_fields() as $key => $elem) { $weights[$key] = $elem->get_weight(); $order[$key] = $insertorder[$key]; } - array_multisort($weights, SORT_ASC, $order, SORT_ASC, $this->fields); + array_multisort($weights, SORT_ASC, $order, SORT_ASC, $this->get_fields()); $output .= "action}\" method=\"{$this->method}\"{$attributes}>\n"; - foreach ($this->fields as $name => $field) { + foreach ($this->get_fields() as $name => $field) { $output .= $field->render($name); } $output .= "form_id}\" />\n"; @@ -826,7 +834,7 @@ public function render($name) { $value = $value['value']; } - $checked = ($this->value == $key) ? ' checked=\"checked\"' : ''; + $checked = ($this->value == $key) ? ' checked="checked"' : ''; $output .= "\n"; } if (!empty($this->description)) { @@ -860,7 +868,7 @@ public function render($name) { $value = $value['value']; } - $checked = (is_array($this->default_value) && in_array($key, $this->default_value)) ? ' checked=\"checked\"' : ''; + $checked = (is_array($this->default_value) && in_array($key, $this->default_value)) ? ' checked="checked"' : ''; $output .= "\n"; } if (!empty($this->description)) { @@ -872,6 +880,7 @@ public function render($name) { class cs_file extends cs_field { protected $uploaded = FALSE; + protected $destination; public function __construct($options = array()) { parent::__construct($options); @@ -894,8 +903,8 @@ public function render($name) { if (!empty($this->title)) { $output .= "\n"; } - $output .= ""; - $output .= "size}\" />"; + $output .= ""; + $output .= "size}\"{$attributes} />"; if (!empty($this->description)) { $output .= "
    {$this->description}
    "; } @@ -929,7 +938,7 @@ class cs_fields_container extends cs_field { protected $insert_field_order = array(); protected $fields = array(); - public function get_fields(){ + public function &get_fields(){ return $this->fields; } @@ -948,26 +957,31 @@ public function add_field($name, $field) { public function values() { $output = array(); - foreach ($this->fields as $name => $field) { + foreach ($this->get_fields() as $name => $field) { $output[$name] = $field->values(); } return $output; } public function preprocess() { - foreach ($this->fields as $field) { + foreach ($this->get_fields() as $field) { $field->preprocess(); } } public function process($values) { - foreach ($values as $name => $value) { - $this->fields[$name]->process($value); + // foreach ($values as $name => $value) { + // $this->get_field($name)->process($value, $name); + // } + foreach ($this->get_fields() as $name => $field) { + if(isset($values[$name])){ + $this->get_field($name)->process($values[$name], $name); + } } } public function valid() { $valid = TRUE; - foreach ($this->fields as $field) { + foreach ($this->get_fields() as $field) { if (!$field->valid()) { $valid = FALSE; } @@ -976,14 +990,14 @@ public function valid() { } public function show_errors() { $output = ""; - foreach ($this->fields as $field) { + foreach ($this->get_fields() as $field) { $output .= $field->show_errors(); } return $output; } public function reset() { - foreach ($this->fields as $field) { + foreach ($this->get_fields() as $field) { $field->reset(); } } @@ -1016,15 +1030,16 @@ public function render($parent_name) { // uasort($this->fields, 'cs_form::order_by_weight'); $insertorder = array_flip($this->insert_field_order); $weights = array(); - foreach ($this->fields as $key => $elem) { + foreach ($this->get_fields() as $key => $elem) { $weights[$key] = $elem->get_weight(); $order[$key] = $insertorder[$key]; } - array_multisort($weights, SORT_ASC, $order, SORT_ASC, $this->fields); + array_multisort($weights, SORT_ASC, $order, SORT_ASC, $this->get_fields()); $output .= "
    \n"; - foreach ($this->fields as $name => $field) { - $output .= $field->render("{$parent_name}[{$name}]"); + foreach ($this->get_fields() as $name => $field) { + // $output .= $field->render("{$parent_name}[{$name}]"); + $output .= $field->render("{$name}"); } return $output ."
    \n". $this->suffix; } From 4d137e4bd8764816e43471543579bd503c7cc617 Mon Sep 17 00:00:00 2001 From: Mirko De Grandis Date: Sun, 28 Sep 2014 09:18:22 +0200 Subject: [PATCH 009/562] modified: src/form.php --- src/form.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/form.php b/src/form.php index 7e987de9..66b0fbda 100644 --- a/src/form.php +++ b/src/form.php @@ -973,7 +973,8 @@ public function process($values) { // $this->get_field($name)->process($value, $name); // } foreach ($this->get_fields() as $name => $field) { - if(isset($values[$name])){ + if( $field instanceof cs_fields_container ) $this->get_field($name)->process($values); + else if(!empty($values[$name])){ $this->get_field($name)->process($values[$name], $name); } } From 984117c4e79d765ad9343ec414317243f17c8b88 Mon Sep 17 00:00:00 2001 From: Mirko De Grandis Date: Sun, 28 Sep 2014 09:39:05 +0200 Subject: [PATCH 010/562] modified: src/form.php --- src/form.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/form.php b/src/form.php index 66b0fbda..c53e6fda 100644 --- a/src/form.php +++ b/src/form.php @@ -919,8 +919,9 @@ public function process($value, $name) { 'mimetype' => $_FILES[$name]['type'], ); if ($this->valid()) { - move_uploaded_file($_FILES[$name]['tmp_name'], $this->value['filepath']); - $this->uploaded = TRUE; + if( @move_uploaded_file($_FILES[$name]['tmp_name'], $this->value['filepath']) == TRUE ){ + $this->uploaded = TRUE; + } } } From 1a4f0a2bb5c208192b1be871aebf3464f2026339 Mon Sep 17 00:00:00 2001 From: Mirko De Grandis Date: Sun, 28 Sep 2014 17:54:47 +0200 Subject: [PATCH 011/562] modified: examples/contact.php modified: src/form.php --- examples/contact.php | 55 ++++++++++--- src/form.php | 180 +++++++++++++++++++++++++++++++++++++++---- 2 files changed, 209 insertions(+), 26 deletions(-) diff --git a/examples/contact.php b/examples/contact.php index 35a912c7..409b35d4 100644 --- a/examples/contact.php +++ b/examples/contact.php @@ -10,7 +10,7 @@ 'attributes'=>array('enctype'=>'multipart/form-data') )); $fieldset = new cs_fieldset(array('attributes'=>array( - 'style' => 'width: 400px', + 'style' => 'width: 500px', ))); $fieldset->add_field('name', array( 'type' => 'textfield', @@ -39,22 +39,54 @@ 'style' => 'width: 100%', ), )); -$fieldset->add_field('checkboxes', array( + +$fieldset->add_field('markup1', array( + 'type' => 'markup', + 'value' => 'aaaa', + 'weight' => -10, +)); +$form->add_field('fieldset', $fieldset); + +$tabs = new cs_tabs(array('attributes'=>array( + 'style' => 'width: 500px', +))); + +$tabs->add_tab('tab1'); +$tabs->add_tab('tab2'); + + +$tabs->add_field('date', array( + 'type' => 'date', +)); + +$tabs->add_field('markup2',array( + 'type' => 'markup', + 'value' => 'bbb', +),0); +$tabs->add_field('markup3',array( + 'type' => 'markup', + 'value' => 'ccc', +),1); +$tabs->add_field('checkboxes', array( 'type' => 'checkboxes', 'options' => array(0=>'zero',1=>'one',2=>'two'), 'default_value' => 1, )); -$fieldset->add_field('file', array( +$tabs->add_field('file', array( 'type' => 'file', 'destination' => dirname(__FILE__), -)); -$fieldset->add_field('markup1', array( - 'type' => 'markup', - 'value' => 'aaaa', - 'weight' => -10, -)); +),1); + +$tabs->add_field('select', array( + 'type' => 'select', + 'options' => array('1'=>'one','2'=>'two','3'=>'three'), + 'default_value' => '2', +),1); + + + +$form->add_field('tabs', $tabs); -$form->add_field('fieldset', $fieldset); $form->add_field('hidden1', array( 'type' => 'hidden', 'default_value' => 'aaaa', @@ -85,6 +117,9 @@ function contact_submit(&$form) { span.required { color: red; } .error { background: #FFA07A; } + + + diff --git a/src/form.php b/src/form.php index c53e6fda..81fcd7f8 100644 --- a/src/form.php +++ b/src/form.php @@ -41,6 +41,7 @@ class cs_form { protected $valid = TRUE; protected $submit = ''; protected $error = ''; + protected $js = array(); protected $insert_field_order = array(); protected $fields = array(); @@ -201,14 +202,28 @@ public function render() { $output .= "action}\" method=\"{$this->method}\"{$attributes}>\n"; foreach ($this->get_fields() as $name => $field) { - $output .= $field->render($name); + $output .= $field->render($name, $this); } $output .= "form_id}\" />\n"; $output .= "form_token}\" />\n"; $output .= "\n"; + if(!empty($this->js)){ + $output .= " + "; + } return $output . $this->suffix; } + public function add_js($js){ + $this->js[] = $js; + } + public static function validate_required($value = NULL) { if (!empty($value)) { return TRUE; @@ -607,7 +622,7 @@ public function __construct($options = array()) { } } - public function render($name) { + public function render($name, cs_form $form) { $id = !empty($this->id) ? $this->id : $name; if (empty($this->value)) { $this->value = 'Submit'; @@ -634,7 +649,7 @@ public function __construct($options = array()) { } } - public function render($name) { + public function render($name, cs_form $form) { $id = !empty($this->id) ? $this->id : $name; if (empty($this->value)) { $this->value = 'Reset'; @@ -661,7 +676,7 @@ public function __construct($options = array()) { } } - public function render($name) { + public function render($name, cs_form $form) { $id = !empty($this->id) ? $this->id : $name; $this->attributes['class'] = trim('button '.(isset($this->attributes['class']) ? $this->attributes['class'] : '')); if($this->disabled == TRUE) $this->attributes['disabled']='disabled'; @@ -685,7 +700,7 @@ public function __construct($options = array()) { } } - public function render($name) { + public function render($name, cs_form $form) { $output = $this->get_prefix(); $output .= $this->value; return $output . $this->get_suffix(); @@ -698,7 +713,7 @@ public function valid() { class cs_hidden extends cs_field { - public function render($name) { + public function render($name, cs_form $form) { $id = !empty($this->id) ? $this->id : $name; $this->attributes['class'] = trim('hidden '.(isset($this->attributes['class']) ? $this->attributes['class'] : '')); $attributes = $this->get_attributes(); @@ -709,7 +724,7 @@ public function render($name) { class cs_textfield extends cs_field { - public function render($name) { + public function render($name, cs_form $form) { $id = !empty($this->id) ? $this->id : $name; $output = $this->get_prefix(); @@ -737,7 +752,7 @@ class cs_textarea extends cs_field { protected $rows = 5; - public function render($name) { + public function render($name, cs_form $form) { $id = !empty($this->id) ? $this->id : $name; $output = $this->get_prefix(); @@ -762,7 +777,7 @@ public function render($name) { class cs_password extends cs_field { - public function render($name) { + public function render($name, cs_form $form) { $id = !empty($this->id) ? $this->id : $name; $output = $this->get_prefix(); @@ -787,7 +802,7 @@ class cs_select extends cs_field { protected $multiple = FALSE; - public function render($name) { + public function render($name, cs_form $form) { $id = !empty($this->id) ? $this->id : $name; $output = $this->get_prefix(); @@ -805,7 +820,8 @@ public function render($name) { $field_name = ($this->multiple) ? "{$name}[]" : $name; $output .= "\n"; if (!empty($this->description)) { @@ -816,7 +832,7 @@ public function render($name) { } class cs_radios extends cs_field { - public function render($name) { + public function render($name, cs_form $form) { $id = !empty($this->id) ? $this->id : $name; $output = $this->get_prefix(); if (!empty($this->title)) { @@ -845,7 +861,7 @@ public function render($name) { } class cs_checkboxes extends cs_field { - public function render($name) { + public function render($name, cs_form $form) { $id = !empty($this->id) ? $this->id : $name; if(!is_array($this->default_value)) { $this->default_value = array($this->default_value); @@ -889,7 +905,7 @@ public function __construct($options = array()) { } } - public function render($name) { + public function render($name, cs_form $form) { $id = !empty($this->id) ? $this->id : $name; $output = $this->get_prefix(); @@ -933,6 +949,77 @@ public function valid() { } } +class cs_date extends cs_field { + private $start_year; + private $end_year; + + public function __construct($options = array()) { + + $this->start_year = date('Y')-100; + $this->end_year = date('Y')+100; + $this->default_value = array( + 'year'=>date('Y'), + 'month'=>date('m'), + 'day'=>date('d'), + ); + + parent::__construct($options); + } + + public function render($name, cs_form $form) { + $id = !empty($this->id) ? $this->id : $name; + $output = $this->get_prefix(); + + $this->attributes['class'] = trim('date '.(isset($this->attributes['class']) ? $this->attributes['class'] : '')); + if (!empty($this->error)) { + $this->attributes['class'] .= ' error'; + } + if($this->disabled == TRUE) $this->attributes['disabled']='disabled'; + $attributes = $this->get_attributes(array('type','name','id','size')); + + if (!empty($this->title)) { + $output .= "\n"; + } + $output .= "
    "; + $output .= ""; + $output .= ""; + $output .= ""; + $output .= "
    "; + + if (!empty($this->description)) { + $output .= "
    {$this->description}
    "; + } + return $output . $this->get_suffix(); + } + + public function process($value, $name) { + $this->value = array( + 'year' => $value['year'], + 'month' => $value['month'], + 'day' => $value['day'], + ); + } + + public function valid() { + + return parent::valid(); + } +} class cs_fields_container extends cs_field { @@ -1011,7 +1098,7 @@ class cs_fieldset extends cs_fields_container { protected $collapsible = FALSE; protected $collapsed = FALSE; - public function render($parent_name) { + public function render($parent_name, cs_form $form) { $id = !empty($this->id) ? $this->id : $parent_name; $output = $this->prefix; $this->attributes['class'] = trim('fieldset '.(isset($this->attributes['class']) ? $this->attributes['class'] : '')); @@ -1041,9 +1128,70 @@ public function render($parent_name) { $output .= "
    \n"; foreach ($this->get_fields() as $name => $field) { // $output .= $field->render("{$parent_name}[{$name}]"); - $output .= $field->render("{$name}"); + $output .= $field->render("{$name}", $form); } return $output ."
    \n". $this->suffix; } } + +class cs_tabs extends cs_fields_container { + protected $tabs = array(); + + public function add_tab($title){ + $this->tabs[] = array('title'=>$title,'fieldnames'=>array()); + } + + public function add_field($name, $field, $tabindex = 0) { + if (!is_object($field)) { + $field_type = isset($field['type']) ? "cs_{$field['type']}" : 'cs_textfield'; + $field = new $field_type($field); + } + $this->fields[$name] = $field; + $this->insert_field_order[$tabindex][] = $name; + $this->tabs[$tabindex]['fieldnames'][] = $name; + } + + public function get_tab_fields($tabindex){ + $out = array(); + $fieldsnames = $this->tabs[$tabindex]['fieldnames']; + foreach($fieldsnames as $name){ + $out[$name] = $this->get_field($name); + } + return $out; + } + + public function render($parent_name, cs_form $form) { + $id = !empty($this->id) ? $this->id : $parent_name; + $output = $this->prefix; + $this->attributes['class'] = trim('tabs '.(isset($this->attributes['class']) ? $this->attributes['class'] : '')); + $attributes = $this->get_attributes(); + + $form->add_js("$('#{$id}').tabs();"); + $output .= "
    \n"; + + $tabs_html = array(); + $tab_links = array(); + foreach($this->tabs as $tabindex => $tab){ + $insertorder = array_flip($this->insert_field_order[$tabindex]); + $weights = array(); + $order = array(); + foreach ($this->get_tab_fields($tabindex) as $key => $elem) { + $weights[$key] = $elem->get_weight(); + $order[$key] = $insertorder[$key]; + } + array_multisort($weights, SORT_ASC, $order, SORT_ASC, $this->get_tab_fields($tabindex)); + + $tab_links[$tabindex] = "
  • ".$this->tabs[$tabindex]['title']."
  • "; + $tabs_html[$tabindex] = "
    \n"; + foreach ($this->get_tab_fields($tabindex) as $name => $field) { + $tabs_html[$tabindex] .= $field->render("{$name}", $form); + } + $tabs_html[$tabindex] .= "
    \n"; + } + $output .= "
      ".implode("",$tab_links)."
    ".implode("",$tabs_html). "
    \n"; + + return $output . $this->suffix; + } + +} From 69a9101b4ba87a3d60a4b41389849e4236fe299c Mon Sep 17 00:00:00 2001 From: Mirko De Grandis Date: Sun, 28 Sep 2014 18:21:42 +0200 Subject: [PATCH 012/562] modified: examples/contact.php modified: src/form.php --- examples/contact.php | 2 +- src/form.php | 23 +++++++++++++++++------ 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/examples/contact.php b/examples/contact.php index 409b35d4..a2fdd0d1 100644 --- a/examples/contact.php +++ b/examples/contact.php @@ -7,7 +7,7 @@ // Generate a simple contact form $form = new cs_form(array( 'form_id' => 'contact', - 'attributes'=>array('enctype'=>'multipart/form-data') +// 'attributes'=>array('enctype'=>'multipart/form-data') )); $fieldset = new cs_fieldset(array('attributes'=>array( 'style' => 'width: 500px', diff --git a/src/form.php b/src/form.php index 81fcd7f8..cd41072b 100644 --- a/src/form.php +++ b/src/form.php @@ -154,6 +154,10 @@ public function valid() { return $this->valid; } + public function set_attribute($name,$value){ + $this->attributes[$name] = $value; + } + public function add_field($name, $field) { if (!is_object($field)) { $field_type = isset($field['type']) ? "cs_{$field['type']}" : 'cs_textfield'; @@ -185,10 +189,6 @@ public function render() { } $output .= ""; } - $attributes = ''; - foreach ($this->attributes as $key => $value) { - $attributes .= " {$key}=\"{$value}\""; - } // uasort($this->fields, 'cs_form::order_by_weight'); @@ -200,10 +200,19 @@ public function render() { } array_multisort($weights, SORT_ASC, $order, SORT_ASC, $this->get_fields()); - $output .= "
    action}\" method=\"{$this->method}\"{$attributes}>\n"; + $fields_html = ''; foreach ($this->get_fields() as $name => $field) { - $output .= $field->render($name, $this); + $fields_html .= $field->render($name, $this); } + + $attributes = ''; + foreach ($this->attributes as $key => $value) { + if($key == 'action' || $key == 'method') continue; + $attributes .= " {$key}=\"{$value}\""; + } + + $output .= "action}\" method=\"{$this->method}\"{$attributes}>\n"; + $output .= $fields_html; $output .= "form_id}\" />\n"; $output .= "form_token}\" />\n"; $output .= "
    \n"; @@ -909,6 +918,8 @@ public function render($name, cs_form $form) { $id = !empty($this->id) ? $this->id : $name; $output = $this->get_prefix(); + $form->set_attribute('enctype', 'multipart/form-data'); + $this->attributes['class'] = trim('file '.(isset($this->attributes['class']) ? $this->attributes['class'] : '')); if (!empty($this->error)) { $this->attributes['class'] .= ' error'; From afdc2e4769ec88d7cb2ed7f8e43448735647213e Mon Sep 17 00:00:00 2001 From: Mirko De Grandis Date: Wed, 1 Oct 2014 16:00:47 +0200 Subject: [PATCH 013/562] modified: examples/contact.php modified: src/form.php --- examples/contact.php | 10 ++++++- src/form.php | 64 +++++++++++++++++++++++++++++++++++++------- 2 files changed, 64 insertions(+), 10 deletions(-) diff --git a/examples/contact.php b/examples/contact.php index a2fdd0d1..a3176886 100644 --- a/examples/contact.php +++ b/examples/contact.php @@ -79,7 +79,7 @@ $tabs->add_field('select', array( 'type' => 'select', - 'options' => array('1'=>'one','2'=>'two','3'=>'three'), + 'options' => array('1'=>'one','2'=>'two','3'=>'three','four'=>array('5'=>'five','6'=>'six','7'=>'seven'),'8'=>'eight'), 'default_value' => '2', ),1); @@ -91,6 +91,14 @@ 'type' => 'hidden', 'default_value' => 'aaaa', )); + +$form->add_field('checkbox', array( + 'type' => 'checkbox', + 'default_value' => 'checkbox', + 'title' => 'Check me', + 'validate' => array( array('validator'=>'required','error_message'=>'You must check the %t checkbox!' ) ), +)); + $form->add_field('submit', array( 'type' => 'submit', 'value' => 'Send', diff --git a/src/form.php b/src/form.php index cd41072b..b2c10e77 100644 --- a/src/form.php +++ b/src/form.php @@ -89,14 +89,13 @@ public function is_submitted() { public function process() { if (!$this->processed) { - $request = ($this->method == 'post') ? $_POST : $_GET; + $request = (strtolower($this->method) == 'post') ? $_POST : $_GET; if (isset($request['form_id']) && $request['form_id'] == $this->form_id) { // foreach ($request as $name => $value) { // if ( $this->get_field($name) !== NULL ) { // $this->get_field($name)->process($value, $name); // } // } - foreach ($this->get_fields() as $name => $field) { if( $field instanceof cs_fields_container ) $this->get_field($name)->process($request); else if ( !empty($request[$name]) ) { @@ -119,7 +118,7 @@ public function process() { foreach ($this->get_fields() as $name => $field) { $field->postprocess(); } - $submit_function($this, ($this->method == 'post') ? $_POST : $_GET); + $submit_function($this, (strtolower($this->method) == 'post') ? $_POST : $_GET); } } } @@ -568,16 +567,24 @@ public function postprocess() { public function valid() { foreach ($this->validate as $validator) { $matches = array(); - preg_match('/^([A-Za-z0-9_]+)(\[(.+)\])?$/', $validator, $matches); - $validator = "validate_{$matches[1]}"; + if(is_array($validator)){ + $validator_func = $validator['validator']; + }else{ + $validator_func = $validator; + } + preg_match('/^([A-Za-z0-9_]+)(\[(.+)\])?$/', $validator_func, $matches); + $validator_func = "validate_{$matches[1]}"; $options = isset($matches[3]) ? $matches[3] : NULL; - if (function_exists($validator)) { - $error = $validator($this->value, $options); + if (function_exists($validator_func)) { + $error = $validator_func($this->value, $options); } else { - $error = cs_form::$validator($this->value, $options); + $error = cs_form::$validator_func($this->value, $options); } if ($error !== TRUE) { $this->error = str_replace('%t', $this->title, $error); + if(is_array($validator) && !empty($validator['error_message'])){ + $this->error = str_replace('%t', $this->title, $validator['error_message']); + } return FALSE; } } @@ -830,7 +837,16 @@ public function render($name, cs_form $form) { $output .= "\n"; if (!empty($this->description)) { @@ -903,6 +919,36 @@ public function render($name, cs_form $form) { } } + +class cs_checkbox extends cs_field { + public function __construct($options = array()) { + foreach ($options as $name => $value) { + $this->$name = $value; + } + } + + public function render($name, cs_form $form) { + $id = !empty($this->id) ? $this->id : $name; + + $output = $this->get_prefix(); + if (empty($this->title)) { + $this->title = $this->value; + } + + if($this->disabled == TRUE) $this->attributes['disabled']='disabled'; + $attributes = $this->get_attributes(); + + $checked = ($this->value == $this->default_value) ? ' checked="checked"' : ''; + $output .= "\n"; + + if (!empty($this->description)) { + $output .= "
    {$this->description}
    "; + } + return $output . $this->get_suffix(); + } +} + + class cs_file extends cs_field { protected $uploaded = FALSE; protected $destination; From efbb2c76d8a0c1183c34777e94165e48ad0d44b8 Mon Sep 17 00:00:00 2001 From: Mirko De Grandis Date: Tue, 7 Oct 2014 07:43:57 +0200 Subject: [PATCH 014/562] modified: src/form.php --- src/form.php | 63 ++++++++++++++++++++++++++++++---------------------- 1 file changed, 36 insertions(+), 27 deletions(-) diff --git a/src/form.php b/src/form.php index b2c10e77..edb8bf2a 100644 --- a/src/form.php +++ b/src/form.php @@ -38,7 +38,7 @@ class cs_form { protected $preprocessors = FALSE; protected $validated = FALSE; protected $submitted = FALSE; - protected $valid = TRUE; + protected $valid = NULL; protected $submit = ''; protected $error = ''; protected $js = array(); @@ -102,23 +102,24 @@ public function process() { $this->get_field($name)->process($request[$name], $name); } } - - } - $this->processed = TRUE; - } - if (!$this->preprocessors) { - foreach ($this->get_fields() as $name => $field) { - $field->preprocess(); + $this->processed = TRUE; } } - if ((!$this->submitted) && $this->valid()) { - $this->submitted = TRUE; - $submit_function = $this->submit; - if (function_exists($submit_function)) { + if($this->processed == TRUE){ + if (!$this->preprocessors) { foreach ($this->get_fields() as $name => $field) { - $field->postprocess(); + $field->preprocess(); + } + } + if ((!$this->submitted) && $this->valid()) { + $this->submitted = TRUE; + $submit_function = $this->submit; + if (function_exists($submit_function)) { + foreach ($this->get_fields() as $name => $field) { + $field->postprocess(); + } + $submit_function($this, (strtolower($this->method) == 'post') ? $_POST : $_GET); } - $submit_function($this, (strtolower($this->method) == 'post') ? $_POST : $_GET); } } } @@ -148,9 +149,10 @@ public function valid() { $this->valid = FALSE; } } + $this->validated = TRUE; + return $this->valid; } - $this->validated = TRUE; - return $this->valid; + return NULL; } public function set_attribute($name,$value){ @@ -181,7 +183,7 @@ public function show_errors() { public function render() { $output = $this->prefix; if ( $this->valid() === FALSE) { - $output .= "