0

I am developing a PHP MVC Framework. But when i insert post then i can see this error. How can I solve this error? I want to validate my input fields. prevent empty fields. Here is my error screenshot

Bellow My codes

class DForm{



    public $currentValue;
    public $values = array();
    public $errors = array();



    // Mehod Post
    public function post($key){

        $this->values[$key] =  trim(isset($_POST['$key']));
         $this->$currentValue = $key;

         return $this;
    }

    // Empty Method
    public function isEmpty(){
        if (empty($this->values[$this->$currentValue])) {
            $this->errors[$this->$currentValue]['empty'] = "Field must not be Empty!";

        }
        return $this;
    }
    // Empty Method for Only Category
    public function isCatEmpty(){
        if ($this->values[$this->$currentValue] == 0) {
            $this->errors[$this->$currentValue]['empty'] = "Field must not be Empty!";

        }
        return $this;
    }

    // Lenth method
    public function length($min= 0, $max){
        if (strlen($this->values[$this->$currentValue]) < $min OR $this->values[$this->$currentValue] > $max) {
            $this->errors[$this->$currentValue]['length'] = "Should min".$min." And Max ".$max." Characters!";
        }
        return $this;
    }


    // Submit Method
    public function submit(){
        if (empty($this->errors)) {
            return true;
        }else{
            return false;
        }
    }

}
Bilal Ahmed
  • 4,005
  • 3
  • 22
  • 42
  • 1
    Possible duplicate of ["Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset" using PHP](https://stackoverflow.com/questions/4261133/notice-undefined-variable-notice-undefined-index-and-notice-undefined) – miken32 Oct 06 '18 at 03:45
  • where is your constructor? – Bilal Ahmed Oct 06 '18 at 04:01

1 Answers1

-1

Error in this line $this->$currentValue = $key;. Because you are using $ sign before currentValue. and always call currentValue like $this->currentValue

replace above line with this

$this->currentValue = $key;//remove $ sign before currentValue

also correct these functions

  • isEmpty()
  • isCatEmpty()
  • length()

Also you should add Constructors and Destructors in class and initialized the parameters like

public $currentValue;
    function __construct() {        
        $this->currentValue = "";   //here you can assign value     
    }

if you want to dynamically assign value to currentValue then pass parameter in constructor

function __construct($value) {      
            $this->currentValue = $value;   
        }
Bilal Ahmed
  • 4,005
  • 3
  • 22
  • 42