Advertisement
petschko

Form Class

Dec 2nd, 2015
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.34 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Author: Peter Dragicevic [peter-91@hotmail.de]
  4.  * Authors-Website: http://petschko.org/
  5.  * Date: 25.11.2015
  6.  * Time: 12:45
  7.  * Update: 09.04.2016
  8.  * Version: 1.1.4 (Changed Website)
  9.  * 1.1.3 (Reformat Code)
  10.  *
  11.  * Notes: Create Forms with this class
  12.  */
  13.  
  14. /**
  15.  * Class Form
  16.  */
  17. class Form {
  18.     private $fields = array();
  19.     private $name;
  20.     private $returnMsg;
  21.     private $isPost;
  22.     private $upload;
  23.  
  24.     /**
  25.      * Create a new instance of forms
  26.      *
  27.      * @param array $fields - object array of form objects
  28.      * @param string $name - name of the form
  29.      * @param bool|string $returnMsg - return message for the user - false means no message
  30.      * @param bool $isPost - set the method true = post false = get
  31.      * @param bool $uploadForm - set this to true if you uploading some with this form
  32.      */
  33.     public function __construct($fields, $name, $returnMsg = false, $isPost = true, $uploadForm = false) {
  34.         $this->setFields($fields);
  35.         $this->setName($name);
  36.         $this->setReturnMsg($returnMsg);
  37.         $this->setIsPost($isPost);
  38.         $this->setUpload($uploadForm);
  39.         $this->setField('formName', FormField::createNewField('formName', 'hidden', $name, $isPost));
  40.     }
  41.  
  42.     /**
  43.      * Clears Memory
  44.      */
  45.     public function __destruct() {
  46.         unset($this->fields);
  47.         unset($this->name);
  48.         unset($this->returnMsg);
  49.         unset($this->isPost);
  50.         unset($this->upload);
  51.     }
  52.  
  53.     /**
  54.      * Returns the Fields array
  55.      *
  56.      * @return array - get the object array of the form
  57.      */
  58.     private function getFields() {
  59.         return $this->fields;
  60.     }
  61.  
  62.     /**
  63.      * Set the Field array
  64.      *
  65.      * @param array $fields - set the object array of the form
  66.      */
  67.     private function setFields($fields) {
  68.         $this->fields = $fields;
  69.     }
  70.  
  71.     /**
  72.      * Get the Name
  73.      *
  74.      * @return string - the name of the form
  75.      */
  76.     public function getName() {
  77.         return $this->name;
  78.     }
  79.  
  80.     /**
  81.      * Set the Name
  82.      *
  83.      * @param string $name - set the name of the form
  84.      */
  85.     private function setName($name) {
  86.         $this->name = $name;
  87.     }
  88.  
  89.     /**
  90.      * Get the current message of the form object
  91.      *
  92.      * @return string|bool - the (error/success) message of the form - false means no message
  93.      */
  94.     public function getReturnMsg() {
  95.         return $this->returnMsg;
  96.     }
  97.  
  98.     /**
  99.      * Setup an error/success message
  100.      *
  101.      * @param string|bool $returnMsg - set the return message for the user - false means no message
  102.      */
  103.     public function setReturnMsg($returnMsg) {
  104.         $this->returnMsg = $returnMsg;
  105.     }
  106.  
  107.     /**
  108.      * Get an field object
  109.      *
  110.      * @param string $fieldName - name of the field
  111.      * @return FormField - field object
  112.      */
  113.     public function getField($fieldName) {
  114.         return $this->fields[$fieldName];
  115.     }
  116.  
  117.     /**
  118.      * Set a new field object or overwrite it
  119.      *
  120.      * @param string $fieldName - name of the field object
  121.      * @param FormField $object - the field object
  122.      */
  123.     public function setField($fieldName, $object) {
  124.         $this->fields[$fieldName] = $object;
  125.     }
  126.  
  127.     /**
  128.      * Get isPost (Method type of this class)
  129.      *
  130.      * @return bool - the method of the form get = false | post = true
  131.      */
  132.     public function getIsPost() {
  133.         return $this->isPost;
  134.     }
  135.  
  136.     /**
  137.      * Set isPost (Method type of this class)
  138.      *
  139.      * @param bool $isPost - the method of the form get|post
  140.      */
  141.     private function setIsPost($isPost) {
  142.         $this->isPost = $isPost;
  143.     }
  144.  
  145.     /**
  146.      * Get is Upload
  147.      *
  148.      * @return bool - is this an upload form = true | not = false
  149.      */
  150.     public function isUpload() {
  151.         return $this->upload;
  152.     }
  153.  
  154.     /**
  155.      * Set isUpload
  156.      *
  157.      * @param bool $upload - is this an upload form = true | no = false
  158.      */
  159.     private function setUpload($upload) {
  160.         $this->upload = $upload;
  161.     }
  162.  
  163.     /**
  164.      * Check if the Form was submitted
  165.      *
  166.      * @return bool - is this form send by post/get = true | false = not send
  167.      */
  168.     public function isSend() {
  169.         if($this->getIsPost()) {
  170.             if(! isset($_POST['formName']))
  171.                 $_POST['formName'] = false;
  172.  
  173.             // Check if form is send
  174.             if($_POST['formName'] == $this->getName())
  175.                 return true;
  176.             return false;
  177.         } else {
  178.             if(! isset($_GET['formName']))
  179.                 $_GET['formName'] = false;
  180.  
  181.             // Check if form is send
  182.             if($_GET['formName'] == $this->getName())
  183.                 return true;
  184.             return false;
  185.         }
  186.     }
  187.  
  188.     /**
  189.      * Checks all fields in this Form, if they're filled out correctly
  190.      *
  191.      * @return array|bool - error array (errorFieldName, errorReason[, optional info like maxLen etc]) or true on success
  192.      */
  193.     public function checkValues() {
  194.  
  195.         // Check all vars if they are filled out correctly
  196.         foreach($this->getFields() as $field) {
  197.  
  198.             // Trim data
  199.             $field->setValue(trim($field->getValue()));
  200.  
  201.             // Check if var is filled out, if required
  202.             if($field->isRequired() && ! $field->getValue())
  203.                 return array($field->getName(), 'required');
  204.  
  205.             // Check if text is to long
  206.             if($field->getMaxLen() < $field->getCurrentLen() && $field->getMaxLen() != 0)
  207.                 return array($field->getName(), 'maxlen', $field->getMaxLen());
  208.  
  209.             // Check if field is to short (only on req fields)
  210.             if($field->getMinLen() > $field->getCurrentLen() && ($field->isRequired() || $field->getCurrentLen() > 0) && $field->getMinLen() != 0)
  211.                 return array($field->getName(), 'minlen', $field->getMinLen());
  212.  
  213.             // Check if data type is correct
  214.             if(! $field->checkDataType() && $field->getCurrentLen() > 0)
  215.                 return array($field->getName(), 'datatype', $field->getDataType());
  216.         }
  217.  
  218.         return true;
  219.     }
  220. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement