title: PHP OOP - How to validate input fields correctly
body: <p>This is my Form - This an Include File.</p>
  <p><strong>form_file.php</strong></p>
  <pre><code>&lt;section class=&quot;container-sm&quot;&gt;
     &lt;form method=&quot;post&quot; class=&quot;par-form&quot;&gt;
          &lt;h3 class=&quot;par-h2&quot;&gt;John Doe Form&lt;/h3&gt;
  
           &lt;p&gt;&lt;label class=&quot;single-label&quot; for='user_firstname'&gt;Name: &lt;a id='user_firstname_label'&gt;&lt;/a&gt;&lt;/label&gt;
                  
           &lt;input class=&quot;par-input form_login&quot; maxlength=&quot;15&quot; name=&quot;user_firstname&quot; required value=&quot;&lt;?=$obVaga-&gt;user_firstname?&gt;&quot; &gt;&lt;/input&gt;&lt;span class=&quot;error&quot;&gt;*&lt;?php echo $nameErr;?&gt;&lt;/span&gt;&lt;/p&gt;
  
           &lt;p&gt;&lt;label class=&quot;single-label&quot; for='user_secondname'&gt;Surname: &lt;a id='user_secondname_label'&gt;&lt;/a&gt;&lt;/label&gt;
                
           &lt;input class=&quot;par-input form_login&quot; maxlength=&quot;40&quot; name=&quot;user_secondname&quot; required value=&quot;&lt;?=$obVaga-&gt;user_secondname?&gt;&quot; &gt;&lt;/input&gt;&lt;/p&gt;
                
           &lt;p&gt;&lt;label  class=&quot;single-label&quot; for='user_email'&gt;E-mail: &lt;a id='user_email_label'&gt;&lt;/a&gt;&lt;/label&gt;
                
           &lt;input class=&quot;par-input form_login&quot; maxlength=&quot;40&quot; name=&quot;user_email&quot; required value=&quot;&lt;?=$obVaga-&gt;user_email?&gt;&quot; &gt;&lt;/input&gt;&lt;/p&gt;
  
           &lt;button type=&quot;button&quot; onclick=&quot;sendData()&quot; class=&quot;par-button&quot; id='user_button_sendData'&gt;SEND&lt;/button&gt;
          &lt;/form&gt;
  &lt;/section&gt;    
  </code></pre>
  <p>This is the template page that receives the Form file</p>
  <p><strong>single-add.php</strong></p>
  <pre><code>&lt;?php 
  /**
   * Acess the composer library
   */
  require __DIR__.'/vendor/autoload.php';
  /**
   * Use a Classe VAGA
   */
  use \App\Entity\Vaga; 
  
  $obVaga = new Vaga;
  
      if (isset($_POST['user_firstname'],$_POST['user_secondname'],$_POST['user_email'])) {
  
      $obVaga-&gt;user_firstname     = $_POST['user_firstname'];
      $obVaga-&gt;user_secondname    = $_POST['user_secondname'];
      $obVaga-&gt;user_email         = $_POST['user_email'];
      $obVaga-&gt;user_cadastrar();
      } 
  
  ?&gt;
                  
          &lt;!-- Receive the contents of the form include form_file.php  --&gt;
          &lt;?php include __DIR__.'/appincludes/form_file.php' ?&gt;
  </code></pre>
  <p>This is the Class page that receives the data</p>
  <p><strong>Vaga.php</strong></p>
  <pre><code>&lt;?php
  namespace App\Entity;
  
  /**
   * Use a Classe Database
   */
  use \App\Db\Database;
  use PDO;
  
  
  class Vaga{
  
      // @var Integer
      public $user_id;
  
      // @var String
      public $user_firstname;
  
      // @var String
      public $user_secondname;
  
      // @var String 
      public $user_email;
  
      // @var String 
      public $nameErr;
  
      // @var Boolean
  
  if ($_SERVER[&quot;REQUEST_METHOD&quot;] == &quot;POST&quot;) {
      if (empty($_POST[&quot;user_firstname&quot;])) {
        $nameErr = &quot;Name is required&quot;;
      } else {
        $name = test_input($_POST[&quot;user_firstname&quot;]);
        // check if name only contains letters and whitespace
        if (!preg_match(&quot;/^[a-zA-Z-' ]*$/&quot;,$name)) {
          $nameErr = &quot;Only letters and white space allowed&quot;;
        }
      }
      
      if (empty($_POST[&quot;user_secondname&quot;])) {
          $nameErr = &quot;Name is required&quot;;
        } else {
          $name = test_input($_POST[&quot;user_secondname&quot;]);
          // check if name only contains letters and whitespace
          if (!preg_match(&quot;/^[a-zA-Z-' ]*$/&quot;,$name)) {
            $nameErr = &quot;Only letters and white space allowed&quot;;
          }
      }
  
      if (empty($_POST[&quot;user_email&quot;])) {
        $emailErr = &quot;Email is required&quot;;
      } else {
        $email = test_input($_POST[&quot;email&quot;]);
        // check if e-mail address is well-formed
        if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
          $emailErr = &quot;Invalid email format&quot;;
        }
      }
  }
  
  
      public function user_cadastrar(){
          // DEFINIR A DATA
          // INSERT STRINGs IN THE TABLE 
          $obDatabase = new Database('tb_partner'); 
  
          $this-&gt;id = $obDatabase-&gt;insert([
                                  'user_firstname' =&gt; $this-&gt;user_firstname,
                                  'user_secondname'=&gt; $this-&gt;user_secondname,
                                  'user_email'    =&gt; $this-&gt;user_email
                              ]);
      }
   
  }
  </code></pre>
  <p>I´m trying to use this script to validate the fields, but I am stuck. I`m trying to execute this script through the class file Vaga.php</p>
  <pre><code>public function par_add_validate(){
  if ($_SERVER[&quot;REQUEST_METHOD&quot;] == &quot;POST&quot;) {
      if (empty($_POST[&quot;user_firstname&quot;])) {
        $nameErr = &quot;Name is required&quot;;
      } else {
        $name = test_input($_POST[&quot;user_firstname&quot;]);
        // check if name only contains letters and whitespace
        if (!preg_match(&quot;/^[a-zA-Z-' ]*$/&quot;,$name)) {
          $nameErr = &quot;Only letters and white space allowed&quot;;
        }
      }
      
      if (empty($_POST[&quot;user_secondname&quot;])) {
          $nameErr = &quot;Name is required&quot;;
        } else {
          $name = test_input($_POST[&quot;user_secondname&quot;]);
          // check if name only contains letters and whitespace
          if (!preg_match(&quot;/^[a-zA-Z-' ]*$/&quot;,$name)) {
            $nameErr = &quot;Only letters and white space allowed&quot;;
          }
      }
  
      if (empty($_POST[&quot;user_email&quot;])) {
        $emailErr = &quot;Email is required&quot;;
      } else {
        $email = test_input($_POST[&quot;email&quot;]);
        // check if e-mail address is well-formed
        if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
          $emailErr = &quot;Invalid email format&quot;;
        }
      }
  };
  }
  </code></pre>
  <p>I putted the Var $nameErr in the form_file.php where it should display an echo string IF the field is empty or with not acceptable letters.</p>
  <p>This is the result in the debug.log file:</p>
  <blockquote>
  <p>[18-Aug-2021 16:44:07 UTC] PHP Notice:  Undefined variable: nameErr in C:\xampppserver2\htdocs\project\appincludes\form_file.php on line 15</p>
  </blockquote>
  <p>Line 15:</p>
  <pre><code>&lt;input name=&quot;user_firstname&quot; required value=&quot;&lt;?=$obVaga-&gt;user_firstname?&gt;&quot; &gt;&lt;/input&gt;&lt;span&gt;*&lt;?php echo $nameErr;?&gt;&lt;/span&gt;&lt;/p&gt;
  </code></pre>
  <p>What am I doing wrong or what is missing?</p>