开发者

PHP OO: Best strategy for dealing with form posts?

We have built custom forms and at this stage are built very well with a lot of thought. Each class has its own responsibility and so far the basic principle of OOP concepts have been used i.e. inheritance, polymorphism, encapsulation, etc. Each element of the form is an object and all objects are collected and initiated on the fly.

Now weve got to the processing part of the forms we at a loss for a strategy to deal with this and would like to ask if anyone has any pointers please?

Is there an alternative, say for instance the creation of a class that could be responsible for checking if the form has been submit and methods to gather post data, validate, etc or do people still do the old way like a quick conditional in the client to check if form submit:

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

And would it be best to use a separate action page to process?

Bascially what I dont want to do is have to write some awful code thats not reusable or make use of OOP con开发者_Python百科cepts. We would like to achieve something WITHOUT USING ANY FRAMEWORKS.


I would try to go with structure like this :

// public function __construct( ValidatorInterface $validator )
$form = new Form( new Validator );


// public function add_field( $name , array $rules = array() )
$form->add_field( 
   'name',  
    // that would be $_POST['name'] , and in template file <?php echo $name ?>
    array(
       'required'  => 'Full Name is required' 
       // for validator to execute is_required()
    ));
$form->add_field( 
   'email' ,
    array( 
       'required'  => 'Email Address is required', 
       'email'     => 'A valid email address is required' 
       // for validator to 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 all the $_POST's from registered fields
   $form->collect();

   if ($form->is_valid() )
   {
      $page->use_file( 'done.html' );
      // do some stuff 
   }
   else
   {            
      // public function populate( TemplateInterface $template )
      // assigns field values and error messages to the template
      $form->populate( $page );         
   }

}

echo $page->render();   

And the Template class based upon this code : http://codeangel.org/articles/simple-php-template-engine.html


update

Implementation for method that 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 for method that finds values of all the registered fields

public function collect()
{           
   $field_names = array_keys( $this->_fields );

   foreach ( $field_names as $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 is pretty simple process.

And on the is_valid() method call if get the Validator instance from local variable and performs actions on each registered form field ( mostly with helpfule php filter_var() function.


You're all missing very important concept. You don't want to show form, you want to ask user for some input. You actually want something like this:

$rules = array(
  "name" => array("not_empty"),
  "email" => array("not_empty", "email")
);
$callback = array($this, "handle_valid_post_data");
$ui->request_input($rules, $callback);

Sadly, no framework does this so you have to implement it yourself.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜