title: Form Validation PHP cleanups
body: <p>Here sample code of my project. I have a personal portfolio that is mostly a static page, running this simple PHP script to validate a form and to send a Email to the admin.</p>
  
  <pre><code>if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  
      $flag = $_POST['FLAG'];
      $name = trim(stripcslashes($_POST['name']));
      $email = trim(stripcslashes($_POST['email']));
      $message = trim(stripcslashes($_POST['message']));
      $success = false;
  
      $mail = new Mail($flag, $name, $email, $message, $config);   
  
      $result = $mail-&gt;validateFields();
  
      if($result) {
          $errors = $result;
      } elseif ($result == false) {
          $mail-&gt;sendEmail();
          $success = true;
      }    
  }
  </code></pre>
  
  <p>Any Idea how to improve my code?<br>
  I am using swiftmailer and a 3rd party MTA (mailgun).<br>
  The class MAIL has 2 methods : Validate and Send.<br>
  validate method is used to validates the fields and return false if no errors are found, else return an array with error messages.<br>
  The send mail is using swiftmailer to send a email. Any Idea how to make everything better? Should I change something or is pretty much ok?</p>
  
  <pre><code>class Mail
  {
  public $flag = null;
  public $name = null;
  public $email = null;
  public $message = null;
  public $config = null;
  public $errors = [];
  
  function __construct($flag, $name, $email, $message, $config)
  {
      $this-&gt;flag = $flag;
      $this-&gt;name = $name;
      $this-&gt;email = $email;
      $this-&gt;message = $message;
      $this-&gt;config = $config;
  
  }
  
  public function validateFields()
  {   
      if (!empty($this-&gt;flag)) {
      $this-&gt;errors[] = 'SPAM FILTER ERROR =)';
      }
  
      if ( empty($this-&gt;name) | !preg_match('/^[a-zA-Z\s]+$/', $this-&gt;name)) {
          $this-&gt;errors[] = 'Enter valid name';
      }
  
      if ( empty($this-&gt;email) | !filter_var($this-&gt;email, FILTER_VALIDATE_EMAIL)) { 
          $this-&gt;errors[] = 'Enter valid email address';
      }
  
      if ( empty($this-&gt;message) ) {
          $this-&gt;errors[] = 'Please enter a message';
      }
  
      if( empty($this-&gt;errors) ) {
          return false;
  
      } else {
          return $this-&gt;errors;
      }
  }
  
  public function sendEmail()
  {
      $transport = (new Swift_SmtpTransport("{$this-&gt;config['SMTP']}", 25))
          -&gt;setUsername("{$this-&gt;config['USERNAME']}")
          -&gt;setPassword("{$this-&gt;config['PASSWORD']}");
  
      /// rest of email logic
  }
  }
  </code></pre>
  
  <p>Any Suggestions will be very appreciated! <br></p>
  
  <p>Thanks.</p>