defender::validate PHP Method

validate() public method

public validate ( )
    public function validate()
    {
        $locale = fusion_get_locale();
        // Are there situations were inputs could have leading
        // or trailing spaces? If not then uncomment line below
        //$this->field_value = trim($this->field_value);
        // Don't bother processing and validating empty inputs
        //if ($this->field_value == '') return $this->field_value;
        /**
         * Keep this include in the constructor
         * This solution was needed to load the defender.inc.php before
         * defining LOCALESET
         */
        include_once LOCALE . LOCALESET . "defender.php";
        // declare the validation rules and assign them
        // type of fields vs type of validator
        $validation_rules_assigned = array('color' => 'textbox', 'dropdown' => 'textbox', 'text' => 'textbox', 'textarea' => 'textbox', 'textbox' => 'textbox', 'checkbox' => 'checkbox', 'password' => 'password', 'date' => 'date', 'timestamp' => 'timestamp', 'number' => 'number', 'email' => 'email', 'address' => 'address', 'name' => 'name', 'url' => 'url', 'image' => 'image', 'file' => 'file', 'document' => 'document', "radio" => "textbox");
        // execute sanitisation rules at point blank precision using switch
        try {
            if (!empty($this->field_config['type'])) {
                if (empty($this->field_value) && $this->field_config['type'] !== "number") {
                    return $this->field_default;
                }
                switch ($validation_rules_assigned[$this->field_config['type']]) {
                    case 'textbox':
                        return $this->verify_text();
                        break;
                    case 'date':
                        return $this->verify_date();
                        break;
                    case 'timestamp':
                        return $this->verify_date();
                        break;
                    case 'password':
                        return $this->verify_password();
                        break;
                    case 'email':
                        return $this->verify_email();
                        break;
                    case 'number':
                        return $this->verify_number();
                        break;
                    case 'file':
                        return $this->verify_file_upload();
                        break;
                    case 'url':
                        return $this->verify_url();
                        break;
                    case 'checkbox':
                        return $this->verify_checkbox();
                        break;
                    case 'name':
                        $name = $this->field_name;
                        if ($this->field_config['required'] && !$_POST[$name][0]) {
                            $this->stop();
                            self::setInputError($name . '-firstname');
                        }
                        if ($this->field_config['required'] && !$_POST[$name][1]) {
                            $this->stop();
                            self::setInputError($name . '-lastname');
                        }
                        if ($this->safe()) {
                            $return_value = $this->verify_text();
                            return $return_value;
                        }
                        break;
                    case 'address':
                        $name = $this->field_name;
                        if ($this->field_config['required'] && !$_POST[$name][0]) {
                            $this->stop();
                            self::setInputError($name . '-street-1');
                        }
                        if ($this->field_config['required'] && !$_POST[$name][1]) {
                            $this->stop();
                            self::setInputError($name . '-street-2');
                        }
                        if ($this->field_config['required'] && !$_POST[$name][2]) {
                            $this->stop();
                            self::setInputError($name . '-country');
                        }
                        if ($this->field_config['required'] && !$_POST[$name][3]) {
                            $this->stop();
                            self::setInputError($name . '-region');
                        }
                        if ($this->field_config['required'] && !$_POST[$name][4]) {
                            $this->stop();
                            self::setInputError($name . '-city');
                        }
                        if ($this->field_config['required'] && !$_POST[$name][5]) {
                            $this->stop();
                            self::setInputError($name . '-postcode');
                        }
                        if ($this->safe()) {
                            $return_value = $this->verify_text();
                            return $return_value;
                        }
                        break;
                        // DEV: To be reviewed
                    // DEV: To be reviewed
                    case 'image':
                        return $this->verify_image_upload();
                        break;
                        // need to know what is this field.
                    // need to know what is this field.
                    case 'document':
                        $name = $this->field_name;
                        if ($this->field_config['required'] && !$_POST[$name][0]) {
                            $this->stop();
                            self::setInputError($name . '-doc-1');
                        }
                        if ($this->field_config['required'] && !$_POST[$name][1]) {
                            $this->stop();
                            self::setInputError($name . '-doc-2');
                        }
                        if ($this->field_config['required'] && !$_POST[$name][2]) {
                            $this->stop();
                            self::setInputError($name . '-doc-3');
                        }
                        if ($this->field_config['required'] && !$_POST[$name][3]) {
                            $this->stop();
                            self::setInputError($name . '-doc-4');
                        }
                        if ($this->field_config['required'] && !$_POST[$name][4]) {
                            $this->stop();
                            self::setInputError($name . '-doc-5');
                        }
                        if ($this->safe()) {
                            $return_value = $this->verify_text();
                            return $return_value;
                        }
                        break;
                    default:
                        $this->stop();
                        $locale['type_unknown'] = '%s: has an unknown type set';
                        // to be moved
                        addNotice('danger', $this->field_name . $locale['type_unknown']);
                }
            } else {
                $this->stop();
                $locale['type_unset'] = '%s: has no type set';
                // to be moved
                addNotice('danger', $this->field_name . $locale['type_unset']);
            }
        } catch (Exception $e) {
            $this->stop();
            addNotice('danger', $e->getMessage());
        }
    }