PhpToolCase
Api Documentation Version 0.9.2
PtcForm.php
Go to the documentation of this file.
1 <?php
2 
3  /**
4  * PHP TOOLCASE HTML FORMS GENERATOR/VALIDATOR CLASS
5  * PHP version 5.3
6  * @category Library
7  * @version 0.9.2
8  * @author Irony <carlo@salapc.com>
9  * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License
10  * @link http://phptoolcase.com
11  */
12 
13  class PtcForm
14  {
15  /**
16  * Alias of PtcForm::getOptions( )
17  * @param string $name the name of the option
18  */
19  public function getOption( $name = null ){ return $this->getOptions( $name ); }
20  /**
21  * Sets form method(POST/GET) and retrieves sent values. See @ref dyn_class_options
22  * @param array $options see PtcForm::$_defaultOptions for available options
23  */
24  public function __construct( $options = array( ) )
25  {
26  $namespace = @strtoupper( @str_replace( '\\' , '_' , __NAMESPACE__ ) ) . '_';
27  if ( !defined( '_PTCFORM_' . $namespace ) ) // declare the class namespace
28  {
29  @define( '_PTCFORM_' . $namespace , get_called_class( ) );
30  }
31  $this->_options = ( !empty( $options ) ) ?
32  array_merge( $this->_options , $options ) : $this->_options;
33  $this->_options = ( !empty( $this->_options ) ) ?
34  array_merge( $this->_defaultOptions , $this->_options ) : $this->_defaultOptions;
35  if ( method_exists( get_called_class( ) , 'boot' ) ){ $this->boot( ); }
36  if ( $this->_options[ 'keep_values' ] )
37  {
38  $method = $this->_getFormValues( );
39  if ( !empty( $method ) )
40  {
41  foreach ( @$method as $k => $v )
42  {
43  if ( false !== @strpos( $k , '_ptcgen' ) )
44  {
45  $this->_hiddenValues[ $key = substr( $k , 0 , -12 ) ] = $v;
46  }
47  }
48  $this->_editFormValues( $method );
49  }
50  }
51  if ( method_exists( get_called_class( ) , 'formFields' ) )
52  {
53  $this->formFields( );
54  $method = $this->_getFormValues( );
55  foreach ( $method as $k => $v )
56  {
57  if ( in_array( $k , $this->_submit ) )
58  {
59  $this->_fireEvent( 'submit' , array( $k , $this ) );
60  unset( $this->_submit[ array_search( $k, $this->_submit ) ] );
61  }
62  }
63  }
64  if ( !defined( '_PTC_RANDID_' ) )
65  {
66  @$_SESSION[ '_PTC_RANDID_' ] = 0;
67  @define( '_PTC_RANDID_' , 'PhpToolCase Random Id Generator' ); // start counting id's
68  }
69  }
70  /**
71  * Adds elemtns to the form object. See @ref adding_fields
72  * @param array $params parameters for the field, see @ref fieldParams
73  */
74  public function addElement( $params )
75  {
76  if ( !@array_key_exists( 'name' , $params ) ) // check if field name has been set
77  {
78  trigger_error( __CLASS__ . '::' . __FUNCTION__ . '( ) missing "name" for defined field!' ,
79  $this->_options[ 'err_msg_level' ] );
80  return;
81  }
82  if ( !@array_key_exists( 'type' , $params ) ){ $params[ 'type' ] = 'text'; } // set "type" if not present
83  $field_name = $params[ 'name' ];
84  $field_type = $params[ 'type' ];
85  $this->_fields[ $field_name ] = array( 'type' => $params[ 'type' ] );
86  unset( $params[ 'name' ] );
87  unset( $params[ 'type' ] );
88  if ( array_key_exists( 'values' , $params ) ) // move "values" to the top
89  {
90  $params = array_merge( array( 'values' => $params[ 'values' ] ) , $params );
91  }
92  if ( array_key_exists( 'value' , $params ) ) // move "value" to the top
93  {
94  $params = array_merge( array( 'value' => $params[ 'value' ] ) , $params );
95  }
96  if ( array_key_exists( 'attributes' , $params ) ) // move "attributes" before "validate"
97  {
98  $params = array_merge( array( 'attributes' => $params[ 'attributes' ] ) , $params );
99  }
100  $this->_fireEvent( 'adding' , array( $field_name , $field_type , &$params , $this ) );
101  foreach( $params as $k => $v )
102  {
103  $v = ( is_callable( $v ) ) ? call_user_func( $v , $this ) : $v;
104  if ( $match = preg_match( '/\[.*?\]/' , $k , $matches ) ) // work with []
105  {
106  /* brackets[] are supported only by select and radio/checkboxgroup */
107  if($field_type!='radiogroup' && $field_type!='checkboxgroup' && $field_type!='select')
108  {
109  trigger_error(__CLASS__.'::'.__FUNCTION__.'() brackets[] not supported for "'.
110  $field_type.'" field',$this->_options['err_msg_level']);
111  continue;
112  }
113  /* check if field has values */
114  if ( $err = $this->_checkErrors( $field_name , 2 , __FUNCTION__ . '()' ) ){ break; }
115  $type = explode( '[' , trim( $k ) );
116  /* check storage keys */
117  if ( $err = $this->_checkErrors( $type[ 0 ] , 3 , __FUNCTION__ . '( )' ) ){ continue; }
118  /* select only supports attributes[] with brackets[] */
119  if ( $field_type == 'select' && $type[ 0 ] != 'attributes' )
120  {
121  trigger_error( __CLASS__ . '::' . __FUNCTION__ . '( )"' . $type[ 0 ] .
122  '[]" parameter is not supported for "select" field!' ,
123  $this->_options[ 'err_msg_level' ] );
124  continue;
125  }
126  if ( $type[ 1 ] === ']' )
127  {
128  foreach ( $this->_fields[ $field_name ][ 'values' ] as $key => $arr )
129  {
130  $this->_addFieldParams( $field_name . '=>' . $key , $type[ 0 ] , $v );
131  }
132  continue;
133  }
134  else
135  {
136  $type[ 1 ] = str_replace( ']' , '' , $type[ 1 ] );
137  $this->_addFieldParams( $field_name . '=>' . $type[ 1 ] , $type[ 0 ] , $v );
138  continue;
139  }
140  }
141  if ( $k === 'values' || $k === 'value' ){ $this->_addFieldValues( $field_name , $v ); continue; }
142  if ( $k=== 'events' && ( $field_type === 'radiogroup' || $field_type === 'checkboxgroup'))
143  {
144  /* check if field has values */
145  if ( $err = $this->_checkErrors( $field_name , 2 , __FUNCTION__ . '( )' ) ){ continue; }
146  foreach ( $this->_fields[ $field_name ][ 'values' ] as $key => $arr )
147  {
148  $this->_addFieldParams( $field_name . '=>' . $key , $k , $v );
149  }
150  continue;
151  }
152  if ( $err = $this->_checkErrors( $k , 3 , __FUNCTION__ . '( )' ) ){ continue; }// check storage keys
153  $this->_addFieldParams( $field_name , $k , $v );
154  }
155  $this->_fields[ $field_name ] = $this->_addDefaultValues( $this->_fields[ $field_name ] );
156  if ( 'submit' === $field_type ){ $this->_submit[ ] = $field_name; }
157  $this->_fireEvent( 'added' , array( $field_name , &$this->_fields[ $field_name ] , $this ) );
158  }
159  /**
160  * Adds a spacer div. See @ref add_custom
161  * @param string $spacerVal the height for the spacer in px
162  * @return the output html
163  */
164  public function addSpacer( $spacerVal = null )
165  {
166  $spacer_height = ( $spacerVal ) ? $spacerVal : $this->_options[ 'spacer_height' ];
167  $spacer_el = str_replace( '{id}','id="ptc-gen' . $this->_randomId( ) . '"' , $this->_htmlTpls[ 'spacer' ] ) . "\n";
168  return $this->_options[ 'start_tab' ] . $spacer_el = str_replace( '{spacerVal}' , $spacer_height , $spacer_el );
169  }
170  /**
171  * Renders the form. See @ref render_form
172  * @param array $attributes form attributes
173  * @param array $events form events
174  * @return the html will be returned if the option "print_form" is set to false, see PtcForm::$_defaultOptions
175  */
176  public function render( $attributes = array( ) , $events = array( ) )
177  {
178  if ( $err = $this->_checkErrors( '' , 5 , __FUNCTION__ . '( )' ) ){ return; }
179  $method = $this->_getFormValues( );
180  foreach ( $method as $k => $v )
181  {
182  if ( in_array( $k , $this->_submit ) ){ $this->_fireEvent( 'submit' , array( $k , $this ) ); }
183  }
184  $main_container = str_replace( '{form_width}' , $this->_options[ 'form_width' ] ,
185  "\n" . $this->_options[ 'start_tab' ] . $this->_htmlTpls[ 'main_container' ] );
186  $start_tab = $this->_options[ 'start_tab' ];
187  $this->_options[ 'start_tab' ] = $this->_options[ 'start_tab' ] . "\t";
188  $container = "\n" . $this->_options[ 'start_tab' ] . $this->_htmlTpls[ 'form' ];
189  $container = str_replace( '{action}' , 'action="' . $this->_options[ 'form_action' ] . '"' , $container );
190  $form_method = strtolower( $this->_options[ 'form_method' ] );
191  $container = str_replace( '{method}' , 'method="' . $form_method . '"' , $container );
192  $this->_options[ 'start_tab' ] = $this->_options[ 'start_tab' ] . "\t";
193  $container = $this->_buildElAttributes( $container );
194  $js = '';
195  foreach ( $events as $k => $v ){ $js .= $k . '="' . $v . '" '; }
196  $container = str_replace( '{events}' , $js_clean = @substr( $js , 0 , -1 ) , $container );
197  if ( !array_key_exists( 'id' , $attributes ) ){ $attributes[ 'id' ] = 'ptc-gen' . $this->_randomId( ); }
198  foreach ( $attributes as $k => $v )
199  {
200  $container = str_replace( '{' . $k . '}' , $k . '="' . $v . '"' , $container );
201  }
202  $container = preg_replace( '# {.*?}|{.*?}^fields#i' , '' , $container );
203  $container = str_replace( ' >{fields}' , '>{fields}' , $container );
204  $fields = '';
205  foreach ( $this->_fields as $k => $arrV )
206  {
207  $this->_fireEvent( 'building' , array( $k , &$this->_fields[ $k ] , $this ) );
208  $fields .= $this->_buildField( $k );
209  $this->_fireEvent( 'built' , array( $k , &$fields , $this ) );
210  }
211  $container = str_replace( '{fields}' , "\n" . $fields . $start_tab . "\t" , $container );
212  $container = str_replace( '{form}' , $container . "\n" . $start_tab , $main_container );
213  /* DEBUGGING */
214  self::_debug( $this->_options , $attributes[ 'id' ] .' form options' , $this->_options[ 'debug_category' ] );
215  self::_debug( $this->_fields , $attributes[ 'id' ] .' form fields' , $this->_options[ 'debug_category' ] );
216  if ( @$this->_validate[ 'fields' ] )
217  {
218  self::_debug( $this->_validate[ 'fields' ] , $attributes[ 'id' ] . ' validator config' ,
219  $this->_options[ 'debug_category' ] );
220  }
221  self::_debug( $this,$attributes[ 'id' ] . ' form object' , $this->_options[ 'debug_category' ] );
222  /* END DEBUGGING */
223  $container = ( $this->_errMsg ) ? $this->_errMsg . $container : $container;
224  $this->_fireEvent( 'rendering' , array( &$container , $this ) );
225  if ( $this->_options[ 'print_form' ] ){ print $container . "\n"; }
226  else{ return $container . "\n"; }
227  $this->_fireEvent( 'rendered' , array( &$container , $this ) );
228  }
229  /**
230  * Validates form fields defined with the "validate" parameter. See @ref validate_form
231  * @return the validation results, isValid(bool) and errors(array) as array keys
232  */
233  public function validate( )
234  {
235  $signature = __CLASS__ . '::' . __FUNCTION__;
236  if ( is_array( $this->_validate ) )
237  {
238  $method = $this->_getFormValues( );
239  $errs = array( );
240 
241  $validate = array( 'errors' => false , 'isValid' => true , 'fields' => $this->_validate );
242  foreach ( $validate[ 'fields' ] as $k => $arr )
243  {
244  foreach ( $arr as $key => $val )
245  {
246  $this->_fireEvent( 'validating' , array( $key , &$k , &$method ) );
247  switch ( $key )
248  {
249  case 'required' :
250  if ( !$is_valid = $this->validateRequired( $k , $method ) )
251  {
252  @$errs[ $key ][ $k ] = 1;
253  }
254  break;
255  case 'email' :
256  if ( !$is_valid = $this->validateEmail( $k , $method ) )
257  {
258  @$errs[ $key ][ $k ] = 1;
259  }
260  $this->_fireEvent( 'validated' , array( $k , $method , &$errs ) );
261  break;
262  case 'number':
263  if ( !$is_valid = $this->validateNumber( $k , $method ) )
264  {
265  @$errs[ $key ][ $k ] = 1;
266  }
267  break;
268  case 'equalTo':
269  if ( !$is_valid = $this->validateEqualTo( $k , $val , $method ) )
270  {
271  @$errs[ $key ][ $k ] = 1;
272  }
273  break;
274  case 'pattern':
275  if ( !$is_valid = $this->validatePattern( $k , $val , $method ) )
276  {
277  @$errs[ $key ][ $k ] = 1;
278  }
279  break;
280  //case "custom":
281  default:
282  if ( method_exists( $this , $val ) )
283  {
284  if ( !$is_valid = @call_user_func( array( $this , $val ) , $k ) )
285  {
286  @$errs[ $val ][ $k ] = 1;
287  }
288  }
289  else if ( is_callable( $val ) )
290  {
291  if ( !$is_valid = @call_user_func( $val , $k ) )
292  {
293  @$errs[ $val ][ $k ] = 1;
294  }
295  }
296  else
297  {
298  trigger_error( $signature . ' could not run validator "'.
299  $val . '"!' , $this->_options[ 'err_msg_level' ] );
300  }
301  }
302  $this->_fireEvent( 'validated' , array( $key , $k , &$method , &$errs ) );
303  }
304  }
305  $this->_validate = $validate;
306  if ( !empty( $errs ) )
307  {
308  $this->_validate[ 'errors' ] = $errs;
309  $this->_validate[ 'isValid' ] = false;
310  $this->_fireEvent( 'error' , array( &$this->_validate , &$this->_errMsg , $this ) );
311  }
312  else{ $this->_fireEvent( 'valid' , array( &$this->_validate , &$this->_errMsg , $this ) ); }
313  self::_debug( $this->_validate , 'validator result' , $this->_options[ 'debug_category' ] );
314  return $this->_validate;
315  }
316  trigger_error( $signature . ' no fields to validate found, quitting now!' , $this->_options[ 'err_msg_level' ] );
317  return false;
318  }
319  /**
320  * Checks if value is empty
321  * @param string $fieldName the name of the input field
322  * @param array $array array of values to check
323  * @return true if value is not empty, otherwise false
324  */
325  public function validateRequired( $fieldName , $array )
326  {
327  return ( @strlen( $array[ $fieldName ] ) > 0 ) ? true : false;
328  }
329  /**
330  * Checks if value is valid email
331  * @param string $fieldName the name of the input field
332  * @param array $array array of values to check
333  * @return true if value is a correct email, otherwise false
334  */
335  public function validateEmail( $fieldName , $array )
336  {
337  if ( $this->validateRequired( $fieldName , $array ) )
338  {
339  // invalid email regex
340  $pattern = "^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$";
341  return @eregi( $pattern , @$array[ $fieldName ] ) ? true : false;
342  }
343  else{ return true; }
344  }
345  /**
346  * Checks if value is numeric
347  * @param string $fieldName the name of the input field
348  * @param array $array array of values to check
349  * @return true if value is numeric, otherwise false
350  */
351  public function validateNumber( $fieldName , $array )
352  {
353  if ( $this->validateRequired( $fieldName , $array ) )
354  {
355  return ( @is_numeric( @$array[ $fieldName ] ) ) ? true : false;
356  }
357  else{ return true; }
358  }
359  /**
360  * Checks if value matches other field value
361  * @param string $fieldName the name of the input field
362  * @param string $matchField the name of the input field to match
363  * @param array $array array of values to check
364  * @return true if value is equal to other given value, otherwise false
365  */
366  public function validateEqualTo( $fieldName , $matchField , $array )
367  {
368  if ( $this->validateRequired( $fieldName , $array ) )
369  {
370  return ( @$array[ $fieldName ] == @$array[ $matchField ] ) ? true : false;
371  }
372  else{ return true; }
373  }
374  /**
375  * Checks if given regex pattern is matched
376  * @param string $fieldName the name of the input field
377  * @param string $pattern the pattern to match(regex)
378  * @param array $array array of values to check
379  * @return true if value matches the given pattern, otherwise false
380  */
381  public function validatePattern( $fieldName , $pattern , $array )
382  {
383  if ( $this->validateRequired( $fieldName , $array ) )
384  {
385  return @eregi( $pattern , @$array[ $fieldName ] ) ? true : false;
386  }
387  else{ return true; }
388  }
389  /**
390  * Alias of PtcForm::customTpls( )
391  */
392  public function customTpl($templates){ $this->customTpls($templates); }
393  /**
394  * Manipulates html templates for all elements. See @ref manipulate_templates
395  * @param array $templates array of html templates
396  */
397  public function customTpls( $templates )
398  {
399  $this->_htmlTpls=array_merge( $this->_htmlTpls , $templates );
400  }
401  /**
402  * Alias of PtcForm::addElAttributes( )
403  */
404  public function addElAttribute( $attributes ) { $this->addElAttributes( $attributes ); }
405  /**
406  * Adds attributes to array of attributes for html elements. See @ref adding_html_attributes
407  * @param array|string $attributes array or string to add as attribute/s
408  */
409  public function addElAttributes( $attributes )
410  {
411  $attributes = ( @is_array( $attributes ) ) ? $attributes : array( $attributes );
412  $this->_elAttributes = array_merge( $this->_elAttributes , $attributes );
413  }
414  /**
415  * Changes label containers default styles. See @ref change_style_labels
416  * @param array $labelStyle ex: "array('float'=>'left','margin'=>'2px 3px 0 0');"
417  * @param int $num options(1,2,3)
418  * @param string $type "left","right","top"
419  */
420  public function setLabelStyle( $labelStyle , $num , $type = null )
421  {
422  if ( !$type ) { $type = $this->_options[ 'labels_align' ]; }
423  $this->_labelStyles[ $num ][ $type ] = array_merge(
424  $this->_labelStyles[ $num ][ $type ] , $labelStyle );
425  foreach ( $this->_labelStyles[ $num ][ $type ] as $k => $v )
426  {
427  if ( false === $v ){ unset($this->_labelStyles[ $num ][ $type ][ $k ]); }
428  }
429  }
430  /**
431  * Changes default input fields style. See @ref change_style_fields
432  * @param array $style ex: "array('border'=>'2px inset','padding'=>'5px');"
433  * @param string $type "input", "radio" or "button"
434  */
435  public function setInputStyle( $style , $type )
436  {
437  $this->_inputStyles[ $type ] = array_merge( $this->_inputStyles[ $type ] , $style );
438  foreach ( $this->_inputStyles[ $type ] as $k => $v )
439  {
440  if ( false === $v ){ unset($this->_inputStyles[ $type ][ $k ]); }
441  }
442  }
443  /**
444  * Adds observers to manage form events with the PtcEvent component. See @ref using_observer
445  * @param string $class the name of the observer class
446  */
447  public function observe( $class = null )
448  {
449  if ( !class_exists( $event_class = $this->_getEventClass( ) ) )
450  {
451  trigger_error( $event_class . ' NOT FOUND!' , E_USER_ERROR );
452  return false;
453  }
454  $class = ( $class ) ? $class : get_called_class( );
455  $methods = get_class_methods( $class );
456  foreach ( $this->_events as $event )
457  {
458  if ( in_array( $event , $methods ) )
459  {
460  $cls = strtolower( $class );
461  $event_class::listen( $cls . '.' . $event , $class . '::' . $event );
462  $this->_observers[ $cls . '.' . $event ] = $event;
463  }
464  }
465  }
466  /**
467  * Returns options defined for the form
468  * @param string $name the option name
469  */
470  public function getOptions( $name = null )
471  {
472  return ( $name ) ? $this->_options[ $name ] : $this->_options;
473  }
474  /**
475  * Adds html before the form container, doesn't have to be an error msg
476  * @param string $msg some text
477  */
478  public function setErrorMsg( $msg ){ $this->_errMsg = $msg; }
479  /**
480  * Property for the message to show on top of the form
481  */
482  protected $_errMsg = null;
483  /**
484  * Class options property, to be merged with PtcForm::$_defaultOptions property
485  */
486  protected $_options = array( );
487  /**
488  * Default options for the class
489  */
490  protected $_defaultOptions = array
491  (
492  'form_method' => 'post' , // the form method to use
493  'form_action' => '#' , // the form action url
494  'form_width' => '500px' , // the width for the main container
495  'add_class_validator' => false , // add validator classes to fields for use with jquery
496  'labels_align' => 'left' , // align labels globally(left,top,right,none)
497  'labels_width' => '40%' , // the width for labels as a percentage
498  'style_elements' => true , // add default style to input elements to align properly
499  'style_labels' => true , // add default style to label elements to align properly
500  'style_tables' => true , // add default style to table elements to align properly
501  'spacer_height' => '3px' , // height for the spacer between fields
502  'keep_values' => true , // repopulate filled fields on form submission
503  'print_form' => true , // print form to screen or return html only
504  'start_tab' => "\t" , // format html code with tabs
505  'err_msg_level' => E_USER_WARNING , // error messages level
506  'debug_category' => 'PtcForm' , // default category for the PtcDebug class
507  'event_class' => '\PtcEvent' // event class parameter
508  );
509  /**
510  * Html templates property for all elements
511  */
512  protected $_htmlTpls = array
513  (
514  'select_option' => '<option {attributes}>{label}</option>',
515  'select' => '<select name="{name}" {attributes}>{options}</select>',
516  'textarea' => '<textarea name="{name}" {attributes}>{value}</textarea>',
517  'input' => '<input type="{type}" name="{name}" {attributes}>',
518  'fieldset' => '<fieldset {attributes}>{label}{data} {start_tab}</fieldset>',
519  'form' => '<form {method} {action} {attributes}>{fields}</form>',
520  'spacer' => '<div style="clear:both;height:{spacerVal}" {id}><!-- --></div>',
521  'label' => '<label {for} {attributes}>{label}</label>',
522  'legend' => '<legend {attributes}>{label}</legend>',
523  'span' => '<span {attributes}>{label}</span>',
524  'td' => '<td align="left" valign="top">',
525  'table' => '<table {attributes}>',
526  'field_container' => '<div {attributes}>{label}{field} {start_tab}</div>',
527  'main_container' => '<div style="width:{form_width}">{form}</div>'
528  );
529  /**
530  * Default label styles property
531  */
532  protected $_labelStyles = array
533  (
534  /* Styles for input, select and textarea */
535  1 => array( 'left' => array( 'float' => 'left' , 'margin' => '2px 0 0 0' , 'width' => '{label_width}%' ) ,
536  'right' => array( 'float' => 'left' , 'margin' => '1px 3px 0 0' , //margin:0 3px 0 0;
537  'text-align' => 'right' , 'width' => '{label_width}%' ) ,
538  'top' => array( ) ) ,
539  /* Styles for checkbox and radio buttons */
540  2 => array( 'left' => array( 'vertical-align' => 'middle' , 'border' => 'none' ) ,
541  'right' => array( 'vertical-align' => 'middle' , 'border' => 'none' ) ,
542  'top' => array( ) ) ,
543  /* Styles for radio/checkbox group and composite fields */
544  3 => array( 'left' => array( 'float' => 'left' , 'margin' => '3px 0 0 0' , 'width' => '{label_width}%' ) ,
545  'right' => array( 'float' => 'left' , 'margin' => '1px 3px 0 0' ,
546  'text-align' => 'right' , 'width' => '{label_width}%' ) ,
547  'top' => array( ) )
548  );
549  /**
550  * Default input styles options property
551  */
552  protected $_inputStyles=array
553  (
554  'radio' => array( 'padding' => '0px' , 'margin' => '0px' , 'vertical-align' => 'middle' , 'width' => '14px' ) ,
555  'input' => array( 'margin' => '0px' , 'padding' => '3px' , 'border' => '1px inset' ) , //padding:2px;
556  'button' => array( 'margin' => '0px' )
557  );
558  /**
559  * Html attributes for all elements
560  */
561  protected $_elAttributes = array
562  (
563  'class' , 'id' , 'style' , 'value' , 'maxlength' , 'minlength' ,'cellpadding' ,
564  'cellspacing' , 'size' , 'disabled' , 'checked' , 'target' , 'align' , 'events' ,
565  'title' , 'selected' , 'cols' , 'rows' , 'equalTo' , 'border' , 'pattern'
566  );
567  /**
568  * Possible options in fields storage
569  */
570  protected $_storageKeys = array( 'events' , 'attributes' , 'validate' , 'label' , 'labelOptions' , 'parentEl' );
571  /**
572  * Fields storage property
573  */
574  protected $_fields = array( );
575  /**
576  * Auto generated hidden fields storage
577  */
578  protected $_hiddenValues = array( );
579  /**
580  * Build hidden values property
581  */
582  protected $_buildHidden = true;
583  /**
584  * Array of fields to validate with the validator engine
585  */
586  protected $_validate = array( );
587  /**
588  * Observers property
589  */
590  protected $_observers = array( );
591  /**
592  * Class events property
593  */
594  protected $_events = array
595  (
596  'rendering' , 'rendered' , 'validating' , 'validated' ,
597  'building' , 'built' , 'error' , 'valid' , 'submit' , 'adding' , 'added'
598  );
599  /**
600  * Property that holds the submit buttons names
601  */
602  protected $_submit = array( );
603  /**
604  * Adds values to fields
605  * @param string $fieldName the name of the field
606  * @param array|string $options value/s to add
607  */
608  protected function _addFieldValues( $fieldName , $options )
609  {
610  if ( $err = $this->_checkErrors( $fieldName , 1 , __FUNCTION__ . "( )" ) ){ return; }
611  if ( !is_array( $options ) )
612  {
613  $options = array( 'value' => $options );
614  $this->_addFieldParams( $fieldName , 'attributes' , $options );
615  return;
616  }
617  switch( $this->_fields[ $fieldName ][ 'type' ] )
618  {
619  case 'select' :
620  case 'radiogroup' :
621  case 'checkboxgroup' :
622  foreach ( $options as $k => $v )
623  {
624  if ( !is_array( $v ) )
625  {
626  $field_type = str_replace( 'group' , '' , $this->_fields[ $fieldName ][ 'type' ] );
627  $v = array( 'type' => $field_type , 'label' => array( $v ) ,
628  'attributes' => array( 'value' => $k ) );
629  }
630  $this->_fields[ $fieldName ][ 'values' ][ $k ] = $this->_addDefaultValues( $v );
631  }
632  break;
633  case 'composite' :
634  case 'fieldset' :
635  $this->_fields[ $fieldName ][ 'values' ] = $options;
636  $this->_addCompositeField( $fieldName , $options );
637  break;
638  default:$this->_addFieldParams( $fieldName , 'values' , $options );
639  break;
640  }
641  }
642  /**
643  * Removes a field from the object
644  * @param string $fieldName the name of the field to be removed
645  */
646  protected function _removeField( $fieldName ){ unset( $this->_fields[ $fieldName ] ); }
647  /**
648  * Builds the container for the field
649  * @param string $fieldName the name of the field
650  * @param string $fieldHtml the html field element
651  * @param string $labelHtml the html label element
652  * @param bool $switch reverse html label position(for radio/checkbox)
653  */
654  protected function _buildContainer( $fieldName , $fieldHtml , $labelHtml = '' , $switch = false )
655  {
656  $main_container = '';
657  /* build container <div> attributes */
658  $main_container .= $this->_options[ 'start_tab' ] . $this->_htmlTpls[ 'field_container' ] . "\n";
659  $main_container = $this->_buildAttributes( $fieldName , $main_container , 'parentEl' );
660  /* build container <div> events */
661  //$mainContainer=$this->_buildAttributes($fieldName,$mainContainer,"events");
662  $main_container = str_replace( ' {start_tab}' , $this->_options[ 'start_tab' ] , $main_container );
663  /* for checkbox or radio only switch field with label */
664  if ( $switch ){ $container = str_replace( '{label}{field}' , $fieldHtml . $labelHtml , $main_container ); }
665  else{ $container = str_replace( '{label}{field}' , $labelHtml . $fieldHtml , $main_container ); }
666  return $container;
667  }
668  /**
669  * Add composite for multiple layouts with html table
670  * @param string $fieldName the name of the input field
671  * @param array $values array of fields
672  */
673  protected function _addCompositeField( $fieldName , $values )
674  {
675  foreach ( $this->_fields[ $fieldName ][ 'values' ] as $k => $v )
676  {
677  if ( $err = $this->_checkErrors( $v , 4 , __FUNCTION__ . '( )' ) )
678  {
679  unset( $this->_fields[ $fieldName ][ 'values' ][ $k ] );
680  continue;
681  }
682  $this->_fields[ $fieldName ][ 'values' ][ $v ] = $this->_fields[ $v ];
683  $this->_removeField( $v );
684  unset( $this->_fields[ $fieldName ][ 'values' ][ $k ] );
685  }
686  }
687  /**
688  * Adds empty default values when addElement() is called
689  * @param string $array tne field array to add default values to
690  */
691  protected function _addDefaultValues( $array )
692  {
693  foreach ( $this->_storageKeys as $k => $v )
694  {
695  if ( !@array_key_exists( $v , $array ) ){ $array[ $v ] = 0; }
696  }
697  return $array;
698  }
699  /**
700  * Switches between span and label elements according to field type
701  * @param string $fieldName the name of the input field
702  * @param string $labelText the text for the label
703  */
704  protected function _switchLabelEl( $fieldName , $labelText )
705  {
706  switch ( $this->_fields[ $fieldName ][ 'type' ] )
707  {
708  case 'radiogroup' :
709  case 'checkboxgroup' :
710  case 'composite' :
711  $label_container = $this->_htmlTpls[ 'span' ];
712  break;
713  case 'fieldset' :
714  $label_container = "\n" . $this->_options[ 'start_tab' ] . "\t" .
715  $this->_htmlTpls[ 'legend' ];
716  break;
717  default:$label_container = $this->_htmlTpls[ 'label' ];
718  break;
719  }
720  $label_html = str_replace( '{label}' , $labelText , $label_container );
721  return $label_html = $this->_buildAttributes( $fieldName , $label_html , 'labelOptions' );
722  }
723  /**
724  * Builds the fields
725  * @param string $fieldName the name of the field
726  */
727  protected function _buildField($fieldName)
728  {
729  if($err=$this->_checkErrors($fieldName,1,__FUNCTION__."()")){ return; }
730  $label_html='';
731  $align_label='none';
732  $label_width='';
733  $dyn_style='';
734  if(@$this->_fields[$fieldName]['label'][0])
735  {
736  $align_label=@array_key_exists('align',$this->_fields[$fieldName]['labelOptions']) ?
737  $this->_fields[$fieldName]['labelOptions']['align'] : $this->_options['labels_align'];
738  $label_width=@array_key_exists('width',$this->_fields[$fieldName]['labelOptions']) ?
739  $this->_fields[$fieldName]['labelOptions']['width'] : $this->_options['labels_width'];
740  $label_html=$this->_switchLabelEl($fieldName,$this->_fields[$fieldName]['label'][0]);
741  }
742  $spacer_height=@array_key_exists('spacer_height',$this->_fields[$fieldName]['attributes']) ?
743  $this->_fields[$fieldName]['attributes']['spacer_height'] : $this->_options['spacer_height'];
744  switch($field_type=$this->_fields[$fieldName]['type'])
745  {
746  case 'checkbox':
747  case 'radio':
748  /* add default style to inputs if not set */
749  foreach( $this->_inputStyles[ 'radio' ] as $k => $v ){ $dyn_style .= $k . ':' . $v . ';'; }
750  $this->_addInputStyle( $fieldName , $dyn_style );
751  $label=$this->_buildLabel(2,$align_label,$label_width,$label_html);
752  /* add default style to label containers */
753  $label_container=$this->_addLabelStyle($fieldName,$label['container'],$label['style']);
754  $field=str_replace('{inputField}',$this->_buildHtml($fieldName),$label['input_container']);
755  $container=$this->_buildContainer($fieldName,$field,$label_container,$label['switch']);
756  break;
757  case 'custom':
758  $container=$this->_options['start_tab'].$this->_fields[$fieldName]['attributes']['value']."\n";
759  break;
760  case 'submit':
761  $label_style='';
762  $input_container="\n".$this->_options['start_tab']."\t".'{inputField}'."\n";
763  /* add default style to inputs if not set */
764  foreach($this->_inputStyles['button'] as $k=>$v){ $dyn_style.=$k.":".$v.";"; }
765  $this->_addInputStyle($fieldName,$dyn_style);
766  $label_container='';
767  $field=str_replace('{inputField}',$this->_buildHtml($fieldName),$input_container);
768  $container=$this->_buildContainer($fieldName,$field,$label_container);
769  break;
770  case 'fieldset':
771  $data="\n";
772  $ori_tab=$this->_options['start_tab'];
773  $this->_options['start_tab']=$this->_options['start_tab']."\t";
774  foreach($this->_fields[$fieldName]['values'] as $k=>$arr)
775  {
776  $this->_fields[$k]=$arr;
777  $data.=$this->_buildField($k);
778  unset($this->_fields[$k]);
779  }
780  $this->_options['start_tab']=$ori_tab;
781  $container=str_replace('{data}',$data,$this->_options['start_tab'].$this->_htmlTpls['fieldset']);
782  $container=$this->_buildAttributes($fieldName,$container,'attributes');
783  $container=str_replace('{label}',$label_html,$container);
784  $container=str_replace(' {start_tab}',$this->_options['start_tab'],$container."\n");
785  break;
786  case 'checkboxgroup':
787  case 'radiogroup':
788  $cols=!@array_key_exists('cols',$this->_fields[$fieldName]['attributes']) ? 1 :
789  $this->_fields[$fieldName]['attributes']['cols'];
790  /* force </tr> in any case */
791  $cols=$cols>sizeof(@$this->_fields[$fieldName]['values']) ?
792  sizeof(@$this->_fields[$fieldName]['values']) : $cols;
793  $this->_fields['group_now']=$this->_fields[$fieldName];
794  unset($this->_fields[$fieldName]);
795  $label=$this->_buildLabel(3,$align_label,$label_width,$label_html);
796  $this->_addTableStyle('group_now',$label['table_style']);
797  $table_container=$this->_buildAttributes('group_now',$this->_htmlTpls['table'],"attributes");
798  $table=$this->_buildTableData($cols,1,$fieldName,
799  @$this->_fields['group_now']['values'],$table_container);
800  $this->_fields[$fieldName]=$this->_fields['group_now'];
801  unset($this->_fields['group_now']);
802  $field="\n".$this->_options['start_tab']."\t<div>{table}\t".$this->_options['start_tab']."</div>\n";
803  $field=str_replace('{table}',$table,$field);
804  /* add default style to label containers */
805  $label_container=$this->_addLabelStyle($fieldName,$label['container'],$label['style']);
806  $container=$this->_buildContainer($fieldName,$field,$label_container);
807  break;
808  case 'composite':
809  $field_attributes=$this->_fields[$fieldName]['attributes'];
810  $cols=!@array_key_exists('cols',$field_attributes) ?
811  sizeof($this->_fields[$fieldName]['values']) : $field_attributes['cols'];
812  /* force </tr> in any case */
813  $cols=$cols>sizeof($this->_fields[$fieldName]['values']) ?
814  sizeof($this->_fields[$fieldName]['values']) : $cols;
815  $label=$this->_buildLabel(3,$align_label,$label_width,$label_html);
816  $this->_addTableStyle($fieldName,$label['table_style']);
817  $table_container=$this->_buildAttributes($fieldName,$this->_htmlTpls['table'],'attributes');
818  $table=$this->_buildTableData($cols,2,$fieldName,
819  $this->_fields[$fieldName]['values'],$table_container);
820  $field="\n".$this->_options['start_tab']."\t<div>{table}\t".
821  $this->_options['start_tab']."</div>\n";
822  $field=str_replace('{table}',$table,$field);
823  /* add default style to label containers */
824  $label_container=$this->_addLabelStyle($fieldName,$label['container'],$label['style']);
825  $container=$this->_buildContainer($fieldName,$field,$label_container);
826  //$spacer_height="0px";
827  break;
828  default:
829  /* add default style to inputs if not set */
830  foreach($this->_inputStyles['input'] as $k=>$v){ $dyn_style.=$k.":".$v.";"; }
831  $this->_addInputStyle($fieldName,$dyn_style);//padding:2px;
832  $label=$this->_buildLabel(1,$align_label,$label_width,$label_html);
833  /* add default style to label containers */
834  $label_container=$this->_addLabelStyle($fieldName,$label['container'],$label['style']);
835  $field=str_replace('{inputField}',$this->_buildHtml($fieldName),$label['input_container']);
836  /* fix \t in the </div> of select field */
837  if($this->_fields[$fieldName]['type']=='select')
838  {
839  $field=str_replace('</div>',$this->_options['start_tab']."\t</div>",$field);
840  }
841  $container=$this->_buildContainer($fieldName,$field,$label_container);
842  break;
843  }
844  $container.=str_replace('{spacerVal}',$spacer_height,$this->addSpacer());
845  $container=preg_replace('# {.*?}|{.*?}#i','',$container); // clean up
846  return $container;
847  }
848  /**
849  * Builds a dynamic table for multiple layouts
850  * @param string $cols number of columns
851  * @param string $type the table type(1,2)
852  * @param string $fieldName the name of the field
853  * @param array $data the values for the table
854  * @param string $container the html table template
855  */
856  protected function _buildTableData($cols,$type,$fieldName,$data,$container)
857  {
858  if(!$data){ return; } // if no values are set for the field
859  $table="\n".$this->_options['start_tab']."\t\t".$container."\n";
860  $table.=$this->_options['start_tab']."\t\t\t".'<tr>';
861  $a=1;
862  $b=1;
863  $ori_build_hidden=$this->_buildHidden;
864  foreach($data as $k=>$arr)
865  {
866  $opts_spacer_height=$this->_options['spacer_height'];
867  $key=($type==1) ? $fieldName : $k;
868  $this->_fields[$key]=$arr;
869  $opts_start_tab=$this->_options['start_tab'];
870  $this->_options['start_tab']=$this->_options['start_tab']."\t\t\t\t\t";
871  $this->_options['spacer_height']='0px';
872  if($b>1 && $type==1){ $this->_buildHidden=false; }
873  $method=$this->_getFormValues();
874  if($this->_options['keep_values'] && !empty($this->_hiddenValues) && !empty($method))
875  {
876  if(isset($method[str_replace('[]','',$fieldName)]))
877  {
878  $keep_vals=$method[str_replace('[]','',$fieldName)];
879  if(@is_array($keep_vals))
880  {
881  foreach($keep_vals as $k=>$v)
882  {
883  if(@$arr['attributes']['value']==$v)
884  {
885  $this->_addFieldParams($key,'attributes',array('checked'=>1));
886  $new_method_arr=$method;
887  $new_method_arr[str_replace('[]','',$fieldName)]=$v;
888  $this->_editFormValues($new_method_arr);
889  }
890  }
891  }
892  $keep_vals='';
893  }
894  }
895  $td_content=$this->_buildField($key);
896  $this->_editFormValues($method);
897  $this->_options['start_tab']=$opts_start_tab;
898  $this->_options['spacer_height']=$opts_spacer_height;
899  $table.="\n".$this->_options['start_tab']."\t\t\t\t".$this->_htmlTpls['td'];
900  $table.="\n".$td_content.$this->_options['start_tab']."\t\t\t\t".'</td>';
901  if($a==$cols)
902  {
903  $table.="\n".$this->_options['start_tab']."\t\t\t".'</tr>';
904  $a=1;
905  if($b<sizeof($data)){ $table.="\n".$this->_options['start_tab']."\t\t\t".'<tr>'; }
906  }
907  else{ $a++; }
908  $b++;
909  unset($this->_fields[$key]);
910  }
911  $final=($cols*ceil(sizeof($data)/$cols));
912  if(sizeof($data)<$final)
913  {
914  for($z=sizeof($data);$z<$final;$z++)
915  {
916  $table.="\n".$this->_options['start_tab']."\t\t\t\t".$this->_htmlTpls['td']."&nbsp;</td>";
917  }
918  $table.="\n".$this->_options['start_tab']."\t\t\t".'</tr>';
919  }
920  $this->_buildHidden=$ori_build_hidden;
921  return $table.="\n".$this->_options['start_tab']."\t\t".'</table>'."\n";
922  }
923  /**
924  * Adds parameters to the fields
925  * @param string $fieldName the name of the field
926  * @param array $type ('events','attributes','validate','label','labelOptions','parentEl','value/s')
927  * @param array|string $options the options to pass
928  */
929  protected function _addFieldParams($fieldName,$type,$options)
930  {
931  $options=is_array($options) ? $options : array($options);
932  $name=explode('=>',$fieldName);
933  $a=sizeof($name);
934  $exclude_types=array('composite','fieldset'); // exclude from validate
935  switch($a)
936  {
937  case 1:
938  if(!@$this->_fields[$fieldName])
939  {
940  trigger_error(__CLASS__.'::'.__FUNCTION__.' could not add '.$type.
941  ' for field '.$fieldName,E_USER_WARNING); return;
942  }
943  if($type=='validate')
944  {
945  if(in_array(@$this->_fields[$name[0]]['type'],$exclude_types))
946  {
947  trigger_error(__CLASS__.'::'.__FUNCTION__.' could not add validator to field '.$fieldName.
948  ', '.@$this->_fields[$name[0]]['type'].' type not supported!',E_USER_WARNING);
949  return;
950  }
951  $this->_addValidator($name[0],$options);
952  $this->_addClassValidator($name[0],$options,@$this->_fields[$name[0]]['type']);
953  }
954  if ( @array_key_exists( 'class' , $this->_fields[ $name[ 0 ] ][ 'attributes' ] ) &&
955  $type == 'attributes' && @array_key_exists( 'class' , $options ) )
956  {
957  $class=$this->_fields[$name[0]]['attributes']['class'].' '.$options['class'];
958  $this->_fields[$name[0]]['attributes']['class']=trim($class);
959  return;
960  }
961  if(!@is_array($this->_fields[$fieldName][$type])){ $this->_fields[$fieldName][$type]=$options; }
962  else{ $this->_fields[$fieldName][$type]=array_merge($this->_fields[$fieldName][$type],$options); }
963  break;
964  case 2:
965  if ( !@array_key_exists( $name[ 1 ] , $this->_fields[ $name[ 0 ] ][ 'values' ] ) )
966  {
967  trigger_error(__CLASS__.'::'.__FUNCTION__.' could not find fieldname '.$fieldName,
968  E_USER_WARNING);
969  return;
970  }
971  if ( $type == 'validate' )
972  {
973  if ( in_array( @$this->_fields[ $name[ 0 ] ][ 'values' ][ $name[ 1 ] ][ 'type' ] , $exclude_types ) )
974  {
975  trigger_error(__CLASS__.'::'.__FUNCTION__.' could not add validator to field '.$fieldName.
976  ', field type '.@$this->_fields[$name[0]]['values'][$name[1]]['type'].' not supported!',
977  E_USER_WARNING);
978  return;
979  }
980 
981 
982  if ( $this->_fields[$name[0]][ 'type' ] == 'checkboxgroup' ||
983  $this->_fields[$name[0]][ 'type' ] == 'radiogroup' /*&&
984  !array_key_exists( $name[ 0 ] , $this->_validate*/ )
985  {
986  $this->_addValidator( $name[ 0 ] , $options );
987  }
988  else{ $this->_addValidator( $name[ 1 ] , $options ); }
989 
990 
991  if ( $this->_fields[$name[0]]['values'][$name[1]]['type']=='radiogroup' ||
992  $this->_fields[$name[0]]['values'][$name[1]]['type']=='checkboxgroup')
993  {
994  foreach($this->_fields[$name[0]]['values'][$name[1]]['values'] as $k=> $v)
995  {
996  $this->_fields[$k.'_temp']=$v;
997  $this->_addClassValidator($k.'_temp',$options);
998  $this->_fields[$name[0]]['values'][$name[1]]['values'][$k]=$this->_fields[$k.'_temp'];
999  unset($this->_fields[$k.'_temp']);
1000  }
1001  }
1002  else{ $this->_addClassValidator($fieldName,$options); }
1003  }
1004  if(@array_key_exists('class',$this->_fields[$name[0]]['values'][$name[1]]['attributes']) &&
1005  $type=='attributes' && @array_key_exists('class',$options))
1006  {
1007  $class=$this->_fields[$name[0]]['values'][$name[1]]['attributes']['class'].' '.$options['class'];
1008  $this->_fields[$name[0]]['values'][$name[1]]['attributes']['class']=trim($class);
1009  return;
1010  }
1011  if(!@is_array($this->_fields[$name[0]]['values'][$name[1]][$type]))
1012  {
1013  $this->_fields[$name[0]]['values'][$name[1]][$type]=$options;
1014  }
1015  else
1016  {
1017  $options=array_merge($this->_fields[$name[0]]['values'][$name[1]][$type],$options);
1018  $this->_fields[$name[0]]['values'][$name[1]][$type]=$options;
1019  }
1020  break;
1021  }
1022  return $this->_fields[$name[0]];
1023  }
1024  /**
1025  * Adds validation to the input field
1026  * @param string $fieldName the name of the field
1027  * @param array|string $options the options to pass
1028  */
1029  protected function _addValidator( $fieldName , $options )
1030  {
1031  $options = is_array( $options ) ? $options : array( $options );
1032  foreach ( $options as $k => $v )
1033  {
1034  if ( is_numeric( $k ) )
1035  {
1036  $options[ $v ] = $v;
1037  unset( $options[ $k ] );
1038  }
1039  }
1040  if ( !@array_key_exists( $fieldName , $this->_validate ) )
1041  {
1042  $this->_validate[ str_replace( '[]' , '' , $fieldName ) ] = $options;
1043  }
1044  else{ array_merge( $this->_validate[ $fieldName ] , $options ); }
1045  }
1046  /**
1047  * Adds validator classes to the fields for js validation
1048  * @param string $fieldName the name of the field
1049  * @param array|string $options the options to pass
1050  * @param string $fieldType used by checkbox and radio groups only
1051  */
1052  protected function _addClassValidator( $fieldName , $options , $fieldType = 'default' )
1053  {
1054  if($this->_options['add_class_validator'])
1055  {
1056  foreach($options as $k=>$v)
1057  {
1058  /* equalTo[matchFieldName] */
1059  if($k==='equalTo')
1060  {
1061  $this->_addFieldParams($fieldName,'attributes',array($k=>$v));
1062  $v=$k.'['.$v.']';
1063  }
1064  if($fieldType=='radiogroup' || $fieldType=='checkboxgroup')
1065  {
1066  if(!@$this->_fields[$fieldName]['values']){ break; }
1067  foreach($this->_fields[$fieldName]['values'] as $key=>$arr)
1068  {
1069  $this->_addFieldParams($fieldName.'=>'.$key,'attributes',array('class'=>$v));
1070  }
1071  }
1072  else{ $this->_addFieldParams( $fieldName , 'attributes' , array( 'class' => $v ) ); }
1073  }
1074  }
1075  }
1076  /**
1077  * Adds default styles to fields to align properly
1078  * @param string $fieldName the name of the field
1079  * @param string $fieldStyle the style property
1080  */
1081  protected function _addInputStyle( $fieldName , $fieldStyle )
1082  {
1083  if ( $this->_options[ 'style_elements' ] )
1084  {
1085  if ( @array_key_exists( 'style' , $this->_fields[ $fieldName ][ 'attributes' ] ) ){ return; }
1086  if ( !is_array( $this->_fields[ $fieldName ][ 'attributes' ] ) )
1087  {
1088  $this->_fields[ $fieldName ][ 'attributes' ] = array( );
1089  }
1090  $this->_fields[ $fieldName ][ 'attributes' ][ 'style' ] = $fieldStyle;
1091  }
1092  }
1093  /**
1094  * Adds default style to the label container to align properly
1095  * @param string $fieldName the name of the field
1096  * @param string $labelContainer the html template for label element
1097  * @param string $style the style property
1098  */
1099  protected function _addLabelStyle( $fieldName , $labelContainer , $style )
1100  {
1101  $label_style = '';
1102  if ( !@array_key_exists( 'style' , $this->_fields[ $fieldName ][ 'labelOptions' ] ) &&
1103  $this->_options[ 'style_labels' ] )
1104  {
1105  $label_style = $style;
1106  }
1107  $label_html = str_replace( ' {label_style}' , $label_style .
1108  ' id="ptc-gen' . $this->_randomId( ) .'"' , $labelContainer );
1109  $this->_addElementId( $fieldName , 'attributes' );
1110  return $label_html = str_replace( '{for}' , 'for="' .
1111  $this->_fields[ $fieldName ][ 'attributes' ][ 'id' ] . '"' , $label_html );
1112  }
1113  /**
1114  * Adds default style to the table to align properly
1115  * @param string $fieldName the name of the field
1116  * @param string $tableStyle the style property
1117  */
1118  protected function _addTableStyle( $fieldName , $tableStyle )
1119  {
1120  if ( $this->_options[ 'style_tables' ] )
1121  {
1122  if ( !@is_array( $this->_fields[ $fieldName ][ 'attributes' ] ) )
1123  {
1124  $this->_fields[ $fieldName ][ 'attributes' ] = array( );
1125  }
1126  if ( !@array_key_exists( 'style' , $this->_fields[ $fieldName ][ 'attributes' ] ) )
1127  {
1128  $this->_fields[ $fieldName ][ 'attributes' ][ 'style' ] = $tableStyle;
1129  }
1130  if ( !@array_key_exists( 'cellpadding' , $this->_fields[ $fieldName ][ 'attributes' ] ) )
1131  {
1132  $this->_fields[ $fieldName ][ 'attributes' ][ 'cellpadding' ] = 0;
1133  }
1134  if ( !@array_key_exists( 'cellspacing' , $this->_fields[ $fieldName ][ 'attributes' ] ) )
1135  {
1136  $this->_fields[ $fieldName ][ 'attributes' ][ 'cellspacing' ] = 0;
1137  }
1138  }
1139  }
1140  /**
1141  * Compiles PtcForm::_elAttributes with the template {attributes}
1142  * @param string $container the html element
1143  */
1144  protected function _buildElAttributes( $container )
1145  {
1146  $chain_attributes = '';
1147  foreach ( $this->_elAttributes as $k => $v ){ $chain_attributes .= '{' . $v . '} '; }
1148  return $container = str_replace( '{attributes}' ,
1149  substr( $chain_attributes , 0 , -1 ) , $container );
1150  }
1151  /**
1152  * Builds attributes for html elements
1153  * @param string $fieldName the name of the field
1154  * @param string $container the html template for container
1155  * @param string $arrKey (events,attributes,validate,label,labelOptions,parentEl)
1156  */
1157  protected function _buildAttributes( $fieldName , $container , $arrKey )
1158  {
1159  $container = $this->_buildElAttributes( $container );
1160  if ( $arrKey == 'parentEl' || $arrKey == 'attributes')
1161  {
1162  $this->_addElementId( $fieldName , $arrKey );
1163  }
1164  if ( @is_array( $this->_fields[ $fieldName ][ $arrKey ] ) )
1165  {
1166  if ( $arrKey == 'events' )
1167  {
1168  $events = '';
1169  foreach ( $this->_fields[ $fieldName ][ $arrKey ] as $k => $v )
1170  {
1171  $events .= $k . '="' . $v . '" ';
1172  }
1173  $events = substr( $events , 0 , -1 ); // remove last space from events chain
1174  return $container = str_replace( '{events}' , $events , $container );
1175  }
1176  $this->_fields[ $fieldName ][ $arrKey ] =
1177  array_filter( $this->_fields[ $fieldName ][ $arrKey ] , 'strlen' );
1178  foreach ( $this->_fields[ $fieldName ][ $arrKey ] as $k => $v )
1179  {
1180  $container = str_replace( '{' . $k . '}' , $k . '="' . $v . '"' , $container );
1181  }
1182  }
1183  return $container;
1184  }
1185  /**
1186  * Adds an id to all html elements
1187  * @param string $fieldName the name of element
1188  * @param string $arrKey the key inside the PtcForm::$_fields array
1189  */
1190  protected function _addElementId( $fieldName , $arrKey )
1191  {
1192  if ( !@array_key_exists( 'id' , $this->_fields[ $fieldName ][ $arrKey ] ) )
1193  {
1194  if ( !@is_array( $this->_fields[ $fieldName ][ $arrKey ] ) )
1195  {
1196  $this->_fields[ $fieldName ][ $arrKey ] = array( );
1197  }
1198  $this->_fields[ $fieldName ][ $arrKey ][ 'id' ] = 'ptc-gen' . $this->_randomId( );
1199  }
1200  }
1201  /**
1202  * Builds html select options
1203  * @param string $fieldName the name of the select field
1204  */
1205  protected function _buildList( $fieldName )
1206  {
1207 
1208  if ( @$this->_fields[ $fieldName ][ 'values' ] )
1209  {
1210  $options = '';
1211  foreach ( @$this->_fields[ $fieldName ][ 'values' ] as $k => $arrV )
1212  {
1213  $option = $this->_buildElAttributes( $this->_htmlTpls[ 'select_option' ] );
1214  if ( @$arrV[ 'label' ][ 0 ] )
1215  {
1216  $option = str_replace( '{label}' , $arrV[ 'label' ][ 0 ] , $option );
1217  }
1218  foreach ( @$arrV[ 'attributes' ] as $k => $v )
1219  {
1220  $option = str_replace( '{' . $k . '}' , $k . '="' . $v . '"' , $option );
1221  }
1222  $options .= preg_replace( '# {.*?}|{.*?}#i' , '' , "\n" .
1223  $this->_options[ 'start_tab' ] . "\t\t\t" . $option );// clean up
1224  }
1225  $select_field = str_replace( '{options}' , $options . "\n" , $this->_htmlTpls[ 'select' ] );
1226  $select_field = "\n" . $this->_options[ 'start_tab' ] . "\t\t" . $select_field . "\n";
1227  return $select_field = str_replace( '</select>' ,
1228  $this->_options[ 'start_tab' ] . "\t\t</select>" , $select_field );
1229  }
1230  }
1231  /**
1232  * Builds container for the field
1233  * @param string $fieldName the name of the field
1234  */
1235  protected function _buildHtml( $fieldName )
1236  {
1237  $this->_rebuildValues( $fieldName ); // rebuild field values if form has been sent already
1238  switch( $this->_fields[ $fieldName ][ 'type' ] )
1239  {
1240  case 'textarea':
1241  $html_field = $this->_htmlTpls[ 'textarea' ];
1242  if ( isset( $this->_fields[ $fieldName ][ 'attributes' ][ 'value' ] ) )
1243  {
1244  $html_field = str_replace( '{value}' ,
1245  $this->_fields[ $fieldName ][ 'attributes' ][ 'value' ] , $html_field );
1246  }
1247  break;
1248  case 'select':$html_field = $this->_buildList( $fieldName );
1249  break;
1250  case 'checkbox':
1251  case 'radio':
1252  if ( $this->_options[ 'keep_values' ] && $this->_buildHidden )
1253  {
1254  $hidden_field = str_replace( '{type}' , 'hidden' , $this->_htmlTpls[ 'input' ] ) . "\n" .
1255  $this->_options[ 'start_tab' ] . "\t";
1256  $html_field = str_replace( '{name}' , str_replace( '[]' , '' , $fieldName ) . '_' .
1257  mt_rand( 1000 , 9999 ) . '_ptcgen' , $hidden_field );
1258  $html_field = preg_replace( '# {.*?}|{.*?}#i' , '' , $html_field ); // clean up
1259  }
1260  @$html_field .= str_replace( '{type}' ,
1261  $this->_fields[ $fieldName ][ 'type' ] , $this->_htmlTpls[ 'input' ] );
1262  break;
1263  default:$html_field = str_replace( '{type}' ,
1264  $this->_fields[ $fieldName ][ 'type' ] , $this->_htmlTpls[ 'input' ] );
1265  break;
1266  }
1267  $html_field = str_replace( '{name}' , $fieldName , $html_field );
1268  $html_field = $this->_buildAttributes( $fieldName , $html_field , 'attributes' );
1269  return $html_field = $this->_buildAttributes( $fieldName , $html_field , 'events' );
1270  }
1271  /**
1272  * Rebuilds values for the fields if form has been sent
1273  * @param string $fieldName the name of the field
1274  */
1275  protected function _rebuildValues( $fieldName )
1276  {
1277  if ( !@array_key_exists( 'noAutoValue' , @$this->_fields[ $fieldName ][ 'attributes' ] ) )
1278  {
1279  $method = $this->_getFormValues( );
1280  if ( $this->_options[ 'keep_values' ] && !empty( $method ) )
1281  {
1282  switch ( $this->_fields[ $fieldName ][ 'type' ] )
1283  {
1284  case 'checkbox' :
1285  case 'radio' :
1286  if ( !@$this->_fields[ $fieldName ][ 'attributes' ][ 'value' ] )
1287  {
1288  $this->_fields[ $fieldName ][ 'attributes' ][ 'value' ] = 'on';
1289  }
1290  unset( $this->_fields[ $fieldName ][ 'attributes' ][ 'checked' ] );
1291  foreach ( $this->_hiddenValues as $k => $v )
1292  {
1293  if ( @array_key_exists( $k , $method ) &&
1294  $k == str_replace( '[]' , '' , $fieldName ) &&
1295  @$method[ $k ] == $this->_fields[ $fieldName ][ 'attributes' ][ 'value' ] )
1296  {
1297  $this->_addFieldParams( $fieldName , 'attributes' , array( 'checked' => 1 ) );
1298  }
1299  }
1300  break;
1301  case 'textarea' :
1302  unset( $this->_fields[ $fieldName ][ 'attributes' ][ 'value' ] );
1303  if ( @strlen( $method[ $fieldName ] ) > 0 )
1304  {
1305  $this->_addFieldValues( $fieldName , @$method[ $fieldName ] );
1306  }
1307  break;
1308  case 'select' :
1309  foreach ( @$this->_fields[ $fieldName ][ 'values' ] as $k => $arrV )
1310  {
1311  unset( $this->_fields[ $fieldName ][ 'values' ][ $k ][ 'attributes' ][ 'selected' ] );
1312  if ( @$method[ $fieldName ] == $k )
1313  {
1314  $this->_addFieldParams( $fieldName . '=>' . $k ,
1315  'attributes' , array( 'selected' => 1 ) );
1316  }
1317  }
1318  break;
1319  default :
1320  unset( $this->_fields[ $fieldName ][ 'attributes' ][ 'value' ] );
1321  if ( @strlen( $method[ $fieldName ] ) > 0 )
1322  {
1323  $this->_addFieldValues( $fieldName , @$method[ $fieldName ] );
1324  }
1325  }
1326  }
1327  }
1328  }
1329  /**
1330  * Builds the label for field
1331  * @param string $case (1,2,3)
1332  * @param string $alignLabel ("left","top","right","none")
1333  * @param string $labelWidth the width of the label as a percentage
1334  * @param string $labelHtml the html template
1335  */
1336  protected function _buildLabel( $case , $alignLabel , $labelWidth , $labelHtml )
1337  {
1338  $label_width = str_replace( '%' , '' , $labelWidth );
1339  $table_width = ( 99 - $label_width );
1340  $label = array( );
1341  if ( $case == 1 ) // build label input,select and textarea fields
1342  {
1343  switch ( $alignLabel )
1344  {
1345  case 'left' :
1346  $label[ 'container' ] = "\n" . $this->_options[ 'start_tab' ] . "\t" .
1347  '<div {label_style}>' . $labelHtml . '</div>';
1348  $label[ 'input_container' ] = "\n" . $this->_options[ 'start_tab' ] . "\t" .
1349  '<div {id}>{inputField}</div>' . "\n";
1350  break;
1351  case 'top' :
1352  $label[ 'container' ] = "\n" . $this->_options[ 'start_tab' ] . "\t" .
1353  '<div {label_style}>' . $labelHtml . '</div>';
1354  $label[ 'input_container' ] = "\n" . $this->_options[ 'start_tab' ] . "\t" .
1355  '<div {id}>{inputField}</div>'."\n";
1356  break;
1357  case 'right' :
1358  $label[ 'container' ] = "\n" . $this->_options[ 'start_tab' ] . "\t" .
1359  '<div {label_style}>' . $labelHtml . '</div>';
1360  $label[ 'input_container' ] = "\n" . $this->_options[ 'start_tab' ] . "\t" .
1361  '<div {id}>{inputField}</div>'."\n";
1362  break;
1363  case 'none' :
1364  $label[ 'container' ] = '';
1365  $label[ 'input_container' ] = "\n" . $this->_options[ 'start_tab' ] . "\t" .
1366  '<div {id}>{inputField}</div>' . "\n";
1367  break;
1368  }
1369  $label[ 'input_container' ] = str_replace( "{id}" , 'id="ptc-gen' .
1370  $this->_randomId( ) . '"' , $label[ 'input_container' ] );
1371  }
1372  else if ( $case == 2 ) // build label for checkbox and radio buttons
1373  {
1374  switch ( $alignLabel )
1375  {
1376  case 'left' :
1377  $label[ 'container' ] = "\n" . $this->_options[ 'start_tab' ] . "\t" .
1378  '<span {label_style}>' . $labelHtml . '</span>';
1379  $label[ 'input_container' ] = "\n" . $this->_options[ 'start_tab' ] .
1380  "\t" . '{inputField}' . "\n";
1381  $label[ 'switch' ] = false;
1382  break;
1383  case 'top' :
1384  $label[ 'container' ] = "\n" . $this->_options[ 'start_tab' ] . "\t" .
1385  '<div {label_style}>' . $labelHtml . '</div>';
1386  $label[ 'input_container' ] = "\n" . $this->_options[ 'start_tab' ] . "\t" .
1387  '<div {id}>{inputField}</div>' . "\n";
1388  $label[ 'switch' ] = false;
1389  break;
1390  case 'right' :
1391  $label[ 'container' ] = "\n" . $this->_options[ 'start_tab' ] . "\t" .
1392  '<span {label_style}>' . $labelHtml . '</span>' . "\n";
1393  $label[ 'input_container' ] = "\n" . $this->_options[ 'start_tab' ] . "\t" . '{inputField}';
1394  $label[ 'switch' ] = true;
1395  break;
1396  case 'none' :
1397  $label[ 'container' ] = '';
1398  $label[ 'input_container' ] = "\n" . $this->_options[ 'start_tab' ] .
1399  "\t" . '{inputField}' . "\n";
1400  $label[ 'switch' ] = false;
1401  break;
1402  }
1403  $label[ 'input_container' ] = str_replace( "{id}" , 'id="ptc-gen' .
1404  $this->_randomId( ) . '"' , $label[ 'input_container' ] );
1405  }
1406  else if ( $case == 3 ) // build label for radio/checkbox group and composite fields
1407  {
1408  switch ( $alignLabel )
1409  {
1410  case 'left' :
1411  $label[ 'container' ] = "\n" . $this->_options[ 'start_tab' ] . "\t" .
1412  '<div {label_style}>' . $labelHtml . '</div>';
1413  $label[ 'table_style' ] = "width:" . $table_width . "%;";
1414  break;
1415  case 'top' :
1416  $label[ 'container' ] = "\n" . $this->_options[ 'start_tab' ] . "\t" .
1417  '<div {label_style}>' . $labelHtml . '</div>';
1418  $label[ 'table_style' ] = "width:100%;";
1419  break;
1420  case 'right' :
1421  $label[ 'container' ] = "\n" . $this->_options[ 'start_tab' ] . "\t" .
1422  '<div {label_style}>' . $labelHtml . '</div>';
1423  $label[ 'table_style' ] = "width:" . ( $table_width - 1 ) . "%;";
1424  break;
1425  case 'none' :
1426  $label[ 'container' ] = '';
1427  $label[ 'table_style' ] = "width:100%;";
1428  break;
1429  }
1430  }
1431  if ( !@array_key_exists( 'style' , $label ) && @$this->_labelStyles[ $case ][ $alignLabel ] )
1432  {
1433  $label[ 'style' ] = ' style="';
1434  foreach ( $this->_labelStyles[ $case ][ $alignLabel ] as $k => $v )
1435  {
1436  $label[ 'style' ] .= $k . ':' . $v . ';';
1437  }
1438  $label[ 'style' ] = str_replace( '{label_width}' , $label_width , $label[ 'style' ] ) . '"';
1439  }
1440  else{ $label[ 'style' ] = ''; }
1441  return $label;
1442  }
1443  /**
1444  * Checks for errors while building and rendering the form
1445  * @param string $fieldName the name of the field
1446  * @param string $type the check type (1,2,3,4,5)
1447  * @param string $function which function called this process
1448  * @param string $errType the error type
1449  */
1450  protected function _checkErrors( $fieldName , $type , $function = null , $errType = null )
1451  {
1452  $errType = ( !$errType ) ? $this->_options[ 'err_msg_level' ] : $errType;
1453  $signature = ( !$function ) ? __CLASS__ . ' ' : __CLASS__ . '::' . $function . ' ';
1454  $debug_msg = '';
1455  switch( $type )
1456  {
1457  case 1 : // test fieldname
1458  if ( !@$this->_fields[ $fieldName ] )
1459  {
1460  $debug_msg = 'could not find field ' . $fieldName;
1461  trigger_error( $signature . $debug_msg , $errType );
1462  return true;
1463  }
1464  break;
1465  case 2 : // test field values
1466  if ( !@$this->_fields[ $fieldName ][ 'values' ] )
1467  {
1468  $debug_msg = 'could not find values for ' . $fieldName;
1469  trigger_error( $signature . $debug_msg , $errType );
1470  return true;
1471  }
1472  break;
1473  case 3 : // test storage keys
1474  if ( !in_array( $fieldName , $this->_storageKeys ) )
1475  {
1476  $debug_msg = '"' . $fieldName . '" is not a valid type';
1477  trigger_error( $signature . $debug_msg , $errType );
1478  return true;
1479  }
1480  break;
1481  case 4 : // test composite value
1482  if ( !@$this->_fields[ $fieldName ] )
1483  {
1484  $debug_msg = 'could not find field ' . $fieldName . ' to add to composite';
1485  trigger_error( $signature . $debug_msg , $errType );
1486  return true;
1487  }
1488  break;
1489  case 5 : // no fields in $_fields parameter
1490  if ( empty( $this->_fields ) )
1491  {
1492  $debug_msg = 'no fields defined, quitting now!';
1493  trigger_error( $signature . $debug_msg , $errType );
1494  return true;
1495  }
1496  break;
1497  }
1498  return false;
1499  }
1500  /**
1501  * Retrieves form values from POST or GET
1502  */
1503  protected function _getFormValues( )
1504  {
1505  switch ( strtolower( $this->_options[ 'form_method' ] ) )
1506  {
1507  case 'get': return $_GET; break;
1508  default: return $_POST; break;
1509  }
1510  }
1511  /**
1512  * Manipulates form values from POST or GET
1513  * @param array $array array of values(POST or GET)
1514  */
1515  protected function _editFormValues( $array )
1516  {
1517  switch ( strtolower( $this->_options[ 'form_method' ] ) )
1518  {
1519  case 'get' : $_GET = $array; break;
1520  default: $_POST = $array; break;
1521  }
1522  }
1523  /**
1524  * Increases number of random generated id for elements
1525  */
1526  protected function _randomId( )
1527  {
1528  return @$_SESSION[ '_PTC_RANDID_' ] = ( @$_SESSION[ '_PTC_RANDID_' ] + 1 );
1529  }
1530  /**
1531  * Fires events with the event class
1532  * @param string $event the name of the event
1533  * @param mixed $data the data to push
1534  */
1535  protected function _fireEvent( $event , $data )
1536  {
1537  $event = ( is_array( $event ) ) ? $event : array( $event );
1538  $event_class = $this->_getEventClass( );
1539  if ( !empty( $this->_observers ) )
1540  {
1541  foreach ( $this->_observers as $k => $v )
1542  {
1543  foreach ( $event as $ev )
1544  {
1545  if ( $v === $ev ){ $event_class::fire( $k , $data ); }
1546  }
1547  }
1548  }
1549  }
1550  /**
1551  * Returns the event component name
1552  */
1553  protected function _getEventClass( )
1554  {
1555  return __NAMESPACE__ . $this->_options[ 'event_class' ];
1556  }
1557  /**
1558  * Send messsages to the PtcDebug class if present
1559  * @param mixed $string the string to pass
1560  * @param mixed $statement some statement if required
1561  * @param string $category a category for the messages panel
1562  */
1563  protected static function _debug( $string , $statement = null , $category = null )
1564  {
1565  if ( !defined( '_PTCDEBUG_NAMESPACE_' ) ) { return false; }
1566  return @call_user_func_array( array( '\\' . _PTCDEBUG_NAMESPACE_ , 'bufferLog' ) ,
1567  array( $string , $statement , $category ) );
1568  }
1569  }
validateRequired($fieldName, $array)
Checks if value is empty.
Definition: PtcForm.php:325
$_errMsg
Property for the message to show on top of the form.
Definition: PtcForm.php:482
observe($class=null)
Adds observers to manage form events with the PtcEvent component.
Definition: PtcForm.php:447
_addInputStyle($fieldName, $fieldStyle)
Adds default styles to fields to align properly.
Definition: PtcForm.php:1081
$_buildHidden
Build hidden values property.
Definition: PtcForm.php:582
setInputStyle($style, $type)
Changes default input fields style.
Definition: PtcForm.php:435
addSpacer($spacerVal=null)
Adds a spacer div.
Definition: PtcForm.php:164
validate()
Validates form fields defined with the &quot;validate&quot; parameter.
Definition: PtcForm.php:233
_buildField($fieldName)
Builds the fields.
Definition: PtcForm.php:727
_buildList($fieldName)
Builds html select options.
Definition: PtcForm.php:1205
$_htmlTpls
Html templates property for all elements.
Definition: PtcForm.php:512
_buildLabel($case, $alignLabel, $labelWidth, $labelHtml)
Builds the label for field.
Definition: PtcForm.php:1336
$_elAttributes
Html attributes for all elements.
Definition: PtcForm.php:561
_addLabelStyle($fieldName, $labelContainer, $style)
Adds default style to the label container to align properly.
Definition: PtcForm.php:1099
customTpls($templates)
Manipulates html templates for all elements.
Definition: PtcForm.php:397
$_validate
Array of fields to validate with the validator engine.
Definition: PtcForm.php:586
_addClassValidator($fieldName, $options, $fieldType= 'default')
Adds validator classes to the fields for js validation.
Definition: PtcForm.php:1052
validateNumber($fieldName, $array)
Checks if value is numeric.
Definition: PtcForm.php:351
addElAttribute($attributes)
Alias of PtcForm::addElAttributes( )
Definition: PtcForm.php:404
_addFieldValues($fieldName, $options)
Adds values to fields.
Definition: PtcForm.php:608
$_storageKeys
Possible options in fields storage.
Definition: PtcForm.php:570
$_options
Class options property, to be merged with PtcForm::$_defaultOptions property.
Definition: PtcForm.php:486
__construct($options=array())
Sets form method(POST/GET) and retrieves sent values.
Definition: PtcForm.php:24
_addDefaultValues($array)
Adds empty default values when addElement() is called.
Definition: PtcForm.php:691
_addTableStyle($fieldName, $tableStyle)
Adds default style to the table to align properly.
Definition: PtcForm.php:1118
addElAttributes($attributes)
Adds attributes to array of attributes for html elements.
Definition: PtcForm.php:409
getOption($name=null)
Alias of PtcForm::getOptions( )
Definition: PtcForm.php:19
$_hiddenValues
Auto generated hidden fields storage.
Definition: PtcForm.php:578
_addCompositeField($fieldName, $values)
Add composite for multiple layouts with html table.
Definition: PtcForm.php:673
_rebuildValues($fieldName)
Rebuilds values for the fields if form has been sent.
Definition: PtcForm.php:1275
_buildContainer($fieldName, $fieldHtml, $labelHtml= '', $switch=false)
Builds the container for the field.
Definition: PtcForm.php:654
_getFormValues()
Retrieves form values from POST or GET.
Definition: PtcForm.php:1503
_buildElAttributes($container)
Compiles PtcForm::_elAttributes with the template {attributes}.
Definition: PtcForm.php:1144
$_fields
Fields storage property.
Definition: PtcForm.php:574
validatePattern($fieldName, $pattern, $array)
Checks if given regex pattern is matched.
Definition: PtcForm.php:381
$_labelStyles
Default label styles property.
Definition: PtcForm.php:532
_getEventClass()
Returns the event component name.
Definition: PtcForm.php:1553
_switchLabelEl($fieldName, $labelText)
Switches between span and label elements according to field type.
Definition: PtcForm.php:704
_randomId()
Increases number of random generated id for elements.
Definition: PtcForm.php:1526
addElement($params)
Adds elemtns to the form object.
Definition: PtcForm.php:74
_buildTableData($cols, $type, $fieldName, $data, $container)
Builds a dynamic table for multiple layouts.
Definition: PtcForm.php:856
$_defaultOptions
Default options for the class.
Definition: PtcForm.php:490
_buildAttributes($fieldName, $container, $arrKey)
Builds attributes for html elements.
Definition: PtcForm.php:1157
_buildHtml($fieldName)
Builds container for the field.
Definition: PtcForm.php:1235
getOptions($name=null)
Returns options defined for the form.
Definition: PtcForm.php:470
_addFieldParams($fieldName, $type, $options)
Adds parameters to the fields.
Definition: PtcForm.php:929
_editFormValues($array)
Manipulates form values from POST or GET.
Definition: PtcForm.php:1515
customTpl($templates)
Alias of PtcForm::customTpls( )
Definition: PtcForm.php:392
_addElementId($fieldName, $arrKey)
Adds an id to all html elements.
Definition: PtcForm.php:1190
setLabelStyle($labelStyle, $num, $type=null)
Changes label containers default styles.
Definition: PtcForm.php:420
_checkErrors($fieldName, $type, $function=null, $errType=null)
Checks for errors while building and rendering the form.
Definition: PtcForm.php:1450
_removeField($fieldName)
Removes a field from the object.
Definition: PtcForm.php:646
static _debug($string, $statement=null, $category=null)
Send messsages to the PtcDebug class if present.
Definition: PtcForm.php:1563
_addValidator($fieldName, $options)
Adds validation to the input field.
Definition: PtcForm.php:1029
setErrorMsg($msg)
Adds html before the form container, doesn&#39;t have to be an error msg.
Definition: PtcForm.php:478
$_events
Class events property.
Definition: PtcForm.php:594
validateEqualTo($fieldName, $matchField, $array)
Checks if value matches other field value.
Definition: PtcForm.php:366
_fireEvent($event, $data)
Fires events with the event class.
Definition: PtcForm.php:1535
$_observers
Observers property.
Definition: PtcForm.php:590
$_inputStyles
Default input styles options property.
Definition: PtcForm.php:552
render($attributes=array(), $events=array())
Renders the form.
Definition: PtcForm.php:176
$_submit
Property that holds the submit buttons names.
Definition: PtcForm.php:602
validateEmail($fieldName, $array)
Checks if value is valid email.
Definition: PtcForm.php:335