Skip to content

Passing Closures As Parameters For PtcForm addElement

ifsale edited this page Nov 16, 2013 · 1 revision

Since version 0.9.1b, PtcForm class accepts closures as field parameters with the addElement( ) method.

Let me show a practical example to understand the convenience of using closures as field parameters:


$form->addElement( array
(
	'type'		=>	'select' ,
	'name'		=>	'usr_sel_day' ,
    'label'     =>  'Which day of the month were you born?' ,
	'values'	=>	function( ){									              									
                      $days = array( '' => null );
                      foreach ( range( 1 , 31 ) as $day ){
			            $day = ( strlen( $day ) === 1 ) ? '0' . $day : $day;
                        $days[ $day ] = $day; 
		               }
		             return $days;
                 }
) );	

As we can see this is a select input that uses a closure as values, the closure returns an array from 1 to 31.

Clone this wiki locally