ED php framework :: Tutorial #13
Validate input data
You use TDataValidator to check if the input data is correct. In a normal scenario there are a couple of data fields like textbox(es), dropdown(s), date/time field(s), etc. with the addition of one "SAVE" button. There are 2 scenarios to use the methods of this class. The first set of methods don't raise exception and store all the errors in an internal property. Using the 2nd set of methods in case of error the current method will raise an exception.

Scenario 1:
    public function btnSave_OnClick() {

        $validator = new TDataValidator();
        $validator->isNotNull($this->dataField->getValue(), "Value is null");
        $validator->isNotEmpty($this->dataField->getValue(), "Value is empty"); // either "" or NULL
        $validator->isInt($this->dataField->getValue(), "Value is not whole number");
        $validator->isDate($this->dataField->getValue(), "Value is not valid date");
        $validator->isTime($this->dataField->getValue(), "Value is not valid time");
        $validator->isPhone($this->txtPhone->getValue(), "Not a valid phone.");
        $validator->isDateTime($this->dataField->getValue(), "Value is not date/time");
        $validator->isEqual($password1, $password2, "Passwords are not equal");
        $validator->isCharRange($password, 5, 10, "Password length must be between 5 and 10 characters");
        $validator->isLessThanOrEqual($value, 99, "Age must be less than 99 years"); // integer(s)
        $validator->isLessThanOrEqual2($value, 24.95, "Price must be less than 24.95"); // numeric(s)
        $validator->isGreaterThanOrEqual($value, 10, "Age must be greater than 10 years"); // integer(s)
        $validator->isGreaterThanOrEqual2($value, 4.95, "Price must be greater than 4.95"); // numeric(s)
        $validator->isIntRange($age, 10, 99, "Age must be between 10 and 99 years");
        $validator->isDigits($age, "Age must be a whole number");
        $validator->isLettersAndDigits($username, "Username must contain only letters and digits");
        $validator->isIdentifier($username, "Username must contain only letters, digits or underscore");
        $validator->isMoney($price, "Price must have money format like 24.95");
        $validator->isMoneyRange($price, 4.95, 24.95, "Price must be between 4.95 and 24.95");
        $validator->isRoundMoney($price, "Price must end with .00 like 25.00");
        $validator->isSqlInjectionSafe($text, "The text is not sql safe.");

        if ($validator->hasErrors()) {
            $this->appendErrors($validator->getErrors()); // show the errors
            return;
        }

    }


Scenario 2: All methods are static.
    public function btnSave_OnClick() {

        TDataValidator::checkNull($this->dataField->getValue(), "Value is null");
        TDataValidator::checkEmpty($this->dataField->getValue(), "Value is empty"); // either "" or NULL
        TDataValidator::checkInt($this->dataField->getValue(), "Value is not whole number");
        TDataValidator::checkDate($this->dataField->getValue(), "Value is not valid date");
        TDataValidator::checkTime($this->dataField->getValue(), "Value is not valid time");
        TDataValidator::checkPhone($this->txtPhone->getValue(), "Not a valid phone.");
        TDataValidator::checkDateTime($this->dataField->getValue(), "Value is not date/time");
        TDataValidator::checkEqual($password1, $password2, "Passwords are not equal");
        TDataValidator::checkCharRange($password, 5, 10, "Password length must be between 5 and 10 characters");
        TDataValidator::checkLessThanOrEqual($value, 99, "Age must be less than 99 years"); // integer(s)
        TDataValidator::checkLessThanOrEqual2($value, 24.95, "Price must be less than 24.95"); // numeric(s)
        TDataValidator::checkGreaterThanOrEqual($value, 10, "Age must be greater than 10 years"); // integer(s)
        TDataValidator::checkGreaterThanOrEqual2($value, 4.95, "Price must be greater than 4.95"); // numeric(s)
        TDataValidator::checkIntRange($age, 10, 99, "Age must be between 10 and 99 years");
        TDataValidator::checkDigits($age, "Age must be a whole number");
        TDataValidator::checkLettersAndDigits($username, "Username must contain only letters and digits");
        TDataValidator::checkIdentifier($username, "Username must contain only letters, digits or underscore");
        TDataValidator::checkMoney($price, "Price must have money format like 24.95");
        TDataValidator::checkMoneyRange($price, 4.95, 24.95, "Price must be between 4.95 and 24.95");
        TDataValidator::checkRoundMoney($price, "Price must end with .00 like 25.00");
        TDataValidator::checkSqlInjectionSafe($text, "The text is not sql safe.");

    }


If a method raises exception then it will be automatically captured by the framework and it will be displayed in a safe manner, but you can always override how it will be handled by redefining the method TPage::OnPageException.
    public function OnPageException($ex) {
        if (is_class($ex, "TDataValidatorException")) {
            return $this->showError($ex->getMessage());
        }
        else if (is_class($ex, "TDatabaseException")) {
            $this->showError("Database error. Check error.log");
        }
        else if (is_class($ex, "Exception")) {
            $this->showError($ex->getMessage());
        }

        errorLog($ex->getMessage());
        errorLog($ex->getTraceAsString());
    }
By default the framework shows the error to the website visitor. Only for the database exceptions hides the real error as it might contain sensitive information. All exceptions except those from TDataValidator are logged in "protected/log/error.log"