oop - PHP OO: Best strategy for dealing with form posts? -


we have built custom forms , @ stage built lot of thought. each class has own responsibility , far basic principle of oop concepts have been used i.e. inheritance, polymorphism, encapsulation, etc. each element of form object , objects collected , initiated on fly.

now weve got processing part of forms @ loss strategy deal , ask if has pointers please?

is there alternative, instance creation of class responsible checking if form has been submit , methods gather post data, validate, etc or people still old way quick conditional in client check if form submit:

if(isset($_post["var"]) { //process form } else { //show form } 

and best use separate action page process?

bascially dont want have write awful code thats not reusable or make use of oop concepts. achieve without using frameworks.

i try go structure :

// public function __construct( validatorinterface $validator ) $form = new form( new validator );   // public function add_field( $name , array $rules = array() ) $form->add_field(     'name',       // $_post['name'] , , in template file <?php echo $name ?>     array(        'required'  => 'full name required'         // validator execute is_required()     )); $form->add_field(     'email' ,     array(         'required'  => 'email address required',         'email'     => 'a valid email address required'         // validator execute is_email()     )); $form->add_field( 'country' );  //name of submit button $for->add_submitter( 'submit' ); // etc   $page = new template(); $page->use_file( 'contact.php' );  if( $form->is_submitted() ) {    // gathers $_post's registered fields    $form->collect();     if ($form->is_valid() )    {       $page->use_file( 'done.html' );       // stuff     }    else    {                   // public function populate( templateinterface $template )       // assigns field values , error messages template       $form->populate( $page );             }  }  echo $page->render();    

and template class based upon code : http://codeangel.org/articles/simple-php-template-engine.html


update

implementation method registers new fields

public function add_field( $name , array $rules = array() ) {           if ( !array_key_exists( $name , $this->_fields ))    {       $this->_fields[ $name ] = array();    }     $this->_fields[ $name ]['rules'] = $rules;  } 

implementation method finds values of registered fields

public function collect() {               $field_names = array_keys( $this->_fields );     foreach ( $field_names $name )    {                       $this->_fields[ $name ]['value'] = $this->_collect_value( $name );                    }  }  protected function _collect_value($name) {     $value = null;     if ( isset( $_post[ $name ] ) )    {       $value = $_post[$name];           }     $value = trim( $value );     if ( empty( $value ) ){       $value = null;        }     return $value;  }    

the data collection pretty simple process.

and on is_valid() method call if validator instance local variable , performs actions on each registered form field ( helpfule php filter_var() function.


Comments

Popular posts from this blog

objective c - Change font of selected text in UITextView -

php - Accessing POST data in Facebook cavas app -

c# - Getting control value when switching a view as part of a multiview -