Skip to main content
deleted 148 characters in body; edited tags
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

I hope this is the right place to put this. But I just made this real quick, wasI'm looking for any advice etc? Betterand ways of doing thing, I'm always looking to improve!on making it better.

https://github.com/Prashles/ValidationCode

EDIT: Here's the actual class itself.

I hope this is the right place to put this. But I just made this real quick, was looking for any advice etc? Better ways of doing thing, I'm always looking to improve!

https://github.com/Prashles/Validation

EDIT: Here's the actual class itself.

I'm looking for advice and ways on making it better.

Code

Rollback to Revision 2
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237
<?php

require_once dirname(__FILE__).'/validate/rules.php';
require_once dirname(__FILE__).'/validate/errors.php';

Class Validate {

    private $valErrors$_errors = array();

    public function rule__construct($value, $name,)
 $rules, $static = FALSE){
    {    require_once 'errors.php';
        $rules$this->errorText = explode$errorText;
    }

    public function rule('|'$value, $rules$name, $inputRules);
    {

        $response$inputRules = FALSE;
explode('|', $inputRules);
        foreach ($rules$inputRules as $rule$inputRule) {

            if (!strlen($value) && $rule$inputRule != 'required') {
                break;
            }

            if (preg_match('/\[(.*?)\]/', $rule$inputRule, $match)) {

                $rule = explode('[', $rule$inputRule);
                $rule = $rule[0];
                $param = $match[1];
                $call =if array($value!method_exists($this, $param$rule);) {
                    throw new Exception("Method {$rule} does not exist");
            else {       exit;
                }

                $call = array($value, $param);
            }
 
            $validateelse ={
 new Rules;
              if (!method_exists($validate$this, $rule$inputRule)) {
                    throw new Exception("Method {$rule$inputRule} does not found"exist");
                    exit;
                }
                $rule = $inputRule;
                $call = array($value);
            }

            $response = call_user_func_array(array($validate$this, $rule), $call);   

            if (!$response && !$static) {
                $error = $this->valErrors[]>errorText[$rule];
                $replace = array('rule' 
 => $rule, 'name'                 ':name' => $name, 
 'param'                   ':param' => (isset($param)) ? $param : nullNULL
                );
                $response = str_replace(array_keys($replace), array_values($replace), $error);

                $this->_errors[] = $response;
            } 

        }

    }

    public function exec()
    {
        return (empty($this->_errors)) ? false : $this->_errors;
    }

    /*
    * Rule functions
    */

    public function min_length($value, $param)
    {
        return $response;(strlen($value) < $param);
    }

    public function errorsmax_length($errorFile$value, =$param) 'validate/errors.english.php'
    {
        return (strlen($value) > $param);
    }

    public function email($value) 
    {
        return !filter_var($value, FILTER_VALIDATE_EMAIL);
    }

    $errorFilepublic =function dirnamerequired(__FILE__$value)."/ 
    {$errorFile
        return (strlen($value) !== 0);
    }";

    public function ip($value)  
 if   {
        return !filter_var(empty$value, FILTER_VALIDATE_IP);
    }

    public function match($this->valErrors$value, $param)
    {
        return ($value != $param);
    }

    public function match_exact($value, $param) 
    {
        return ($value !== $param);
    }

    public function match_password($value, $param) 
    {
        return ($value !== $param);
    }

    public function alphanum($value)
    {
        return false;!ctype_alnum($value);
    }

    public function url($value)
    {
        return !filter_var($value, FILTER_VALIDATE_URL);
    }

    public function numeric($value)
  $errors = new{
 Errors       return !($errorFileis_numeric($value));
    }

    public function min($value) 
    {
        return $errors->error($this->valErrors$value < $param);
    }

    public function max($value) {
        return ($value > $param);
    }

}

I'd like to thank you all for your answers. I really appreciate you guys taking time out of your day to leave such detailed responses.

I've taken everything you've said into consideration and I've split it into classes; trying to give each one a single responsibility. I hope this is a better attempt!

Thanks.

<?php

require_once dirname(__FILE__).'/validate/rules.php';
require_once dirname(__FILE__).'/validate/errors.php';

Class Validate {

    private $valErrors = array();

    public function rule($value, $name, $rules, $static = FALSE)
    {
        $rules = explode('|', $rules);

        $response = FALSE;

        foreach ($rules as $rule) {

            if (!strlen($value) && $rule != 'required') {
                break;
            }

            if (preg_match('/\[(.*?)\]/', $rule, $match)) {

                $rule = explode('[', $rule);
                $rule = $rule[0];
                $param = $match[1];
                $call = array($value, $param);
            }
            else {
                $call = array($value);
            }
 
            $validate = new Rules;
            if (!method_exists($validate, $rule)) {
                throw new Exception("Method {$rule} not found");
            }
            
            $response = call_user_func_array(array($validate, $rule), $call);   

            if (!$response && !$static) {
                $this->valErrors[] = array('rule' => $rule, 'name' => $name, 'param' => (isset($param)) ? $param : null);
            }
            
        }

        return $response;
    }

    public function errors($errorFile = 'validate/errors.english.php')
    {

        $errorFile = dirname(__FILE__)."/{$errorFile}";

        if (empty($this->valErrors)) {
            return false;
        }

        $errors = new Errors($errorFile);

        return $errors->error($this->valErrors);

    }

}

I'd like to thank you all for your answers. I really appreciate you guys taking time out of your day to leave such detailed responses.

I've taken everything you've said into consideration and I've split it into classes; trying to give each one a single responsibility. I hope this is a better attempt!

Thanks.

<?php

Class Validate {

    private $_errors = array();

    public function __construct()
    {
        require_once 'errors.php';
        $this->errorText = $errorText;
    }

    public function rule($value, $name, $inputRules)
    {

        $inputRules = explode('|', $inputRules);
        foreach ($inputRules as $inputRule) {

            if (!strlen($value) && $inputRule != 'required') {
                break;
            }

            if (preg_match('/\[(.*?)\]/', $inputRule, $match)) {

                $rule = explode('[', $inputRule);
                $rule = $rule[0];
                $param = $match[1];
                if (!method_exists($this, $rule)) {
                    throw new Exception("Method {$rule} does not exist");
                    exit;
                }

                $call = array($value, $param);
            }
            else {
                if (!method_exists($this, $inputRule)) {
                    throw new Exception("Method {$inputRule} does not exist");
                    exit;
                }
                $rule = $inputRule;
                $call = array($value);
            }

            $response = call_user_func_array(array($this, $rule), $call);

            if ($response) {
                $error = $this->errorText[$rule];
                $replace = array( 
                    ':name' => $name, 
                    ':param' => (isset($param)) ? $param : NULL
                );
                $response = str_replace(array_keys($replace), array_values($replace), $error);

                $this->_errors[] = $response;
            } 

        }

    }

    public function exec()
    {
        return (empty($this->_errors)) ? false : $this->_errors;
    }

    /*
    * Rule functions
    */

    public function min_length($value, $param)
    {
        return (strlen($value) < $param);
    }

    public function max_length($value, $param) 
    {
        return (strlen($value) > $param);
    }

    public function email($value) 
    {
        return !filter_var($value, FILTER_VALIDATE_EMAIL);
    }

    public function required($value) 
    {
        return (strlen($value) !== 0);
    }

    public function ip($value)  
    {
        return !filter_var($value, FILTER_VALIDATE_IP);
    }

    public function match($value, $param)
    {
        return ($value != $param);
    }

    public function match_exact($value, $param) 
    {
        return ($value !== $param);
    }

    public function match_password($value, $param) 
    {
        return ($value !== $param);
    }

    public function alphanum($value)
    {
        return !ctype_alnum($value);
    }

    public function url($value)
    {
        return !filter_var($value, FILTER_VALIDATE_URL);
    }

    public function numeric($value)
    {
        return !(is_numeric($value));
    }

    public function min($value) 
    {
        return ($value < $param);
    }

    public function max($value) {
        return ($value > $param);
    }

}
Tweeted twitter.com/#!/StackCodeReview/status/272290274556915712
deleted 1125 characters in body
Source Link
purpletree
  • 213
  • 1
  • 3
  • 7
<?php

require_once dirname(__FILE__).'/validate/rules.php';
require_once dirname(__FILE__).'/validate/errors.php';

Class Validate {

    private $_errors$valErrors = array();

    public function __constructrule()
    {
    $value, $name, $rules, $static require_once= 'errors.php';FALSE)
        $this->errorText = $errorText;{
    }

    public$rules function= ruleexplode($value, $name'|', $inputRules$rules)
    {;

        $inputRules$response = explode('|', $inputRules);FALSE;

        foreach ($inputRules$rules as $inputRule$rule) {

            if (!strlen($value) && $inputRule$rule != 'required') {
                break;
            }

            if (preg_match('/\[(.*?)\]/', $inputRule$rule, $match)) {

                $rule = explode('[', $inputRule$rule);
                $rule = $rule[0];
                $param = $match[1];
                if (!method_exists($this, $rule)) {
                    throw$call new= Exceptionarray("Method {$rule} does not$value, exist"$param);
                    exit;}
               else }
{
                $call = array($value, $param);
            }
   
          else {
 $validate = new Rules;
            if (!method_exists($this$validate, $inputRule$rule)) {
                    throw new Exception("Method {$inputRule$rule} does not exist"found");
                    exit;
                }
                $rule = $inputRule;
                $call = array($value);
            }

            $response = call_user_func_array(array($this$validate, $rule), $call);   

            if (!$response && !$static) {
                $error = $this->errorText[$rule];
                $replace>valErrors[] = array(
                 'rule' => $rule, ':name''name' => $name,
                    ':param''param' => (isset($param)) ? $param : NULL
                );
                $response = str_replace(array_keys($replace), array_values($replace), $errornull);

                $this->_errors[] = $response;
            }

        }

    }

    public function exec()
    {
        return (empty($this->_errors)) ? false : $this->_errors;
    }

    /*
    * Rule functions
    */

    public function min_length($value, $param)
    {
        return (strlen($value) < $param);
    }

    public function max_length($value, $param) 
     {
        return (strlen($value) > $param);$response;
    }

    public function email($value) 
    {
        return !filter_varerrors($value, FILTER_VALIDATE_EMAIL);
    }

    public$errorFile function= required($value'validate/errors.english.php') 
    {
        return (strlen($value) !== 0);
    }

    public$errorFile function= ipdirname($value__FILE__) 
    ."/{
        return !filter_var($value, FILTER_VALIDATE_IP);
    $errorFile}

    public function match($value, $param)
    {";
        return ($value != $param);
    }

    public functionif match_exact($value, $param) 
    {
        return empty($value !== $param$this->valErrors);
    }

    public function match_password($value, $param) 
    {
        return ($value !== $param);
    }

    public function alphanum($value)
    {
        return !ctype_alnum($value);
    }

    public function url($value)
    {false;
        return !filter_var($value, FILTER_VALIDATE_URL);
    }

    public function numeric($value)
    {
      $errors = returnnew !(is_numericErrors($value)$errorFile);
    }

    public function min($value) 
    {
        return $errors->error($value < $param$this->valErrors);
    }

    public function max($value) {
        return ($value > $param);
    }

}

I'd like to thank you all for your answers. I really appreciate you guys taking time out of your day to leave such detailed responses.

I've taken everything you've said into consideration and I've split it into classes; trying to give each one a single responsibility. I hope this is a better attempt!

Thanks.

<?php

Class Validate {

    private $_errors = array();

    public function __construct()
    {
        require_once 'errors.php';
        $this->errorText = $errorText;
    }

    public function rule($value, $name, $inputRules)
    {

        $inputRules = explode('|', $inputRules);
        foreach ($inputRules as $inputRule) {

            if (!strlen($value) && $inputRule != 'required') {
                break;
            }

            if (preg_match('/\[(.*?)\]/', $inputRule, $match)) {

                $rule = explode('[', $inputRule);
                $rule = $rule[0];
                $param = $match[1];
                if (!method_exists($this, $rule)) {
                    throw new Exception("Method {$rule} does not exist");
                    exit;
                }

                $call = array($value, $param);
            }
            else {
                if (!method_exists($this, $inputRule)) {
                    throw new Exception("Method {$inputRule} does not exist");
                    exit;
                }
                $rule = $inputRule;
                $call = array($value);
            }

            $response = call_user_func_array(array($this, $rule), $call);

            if ($response) {
                $error = $this->errorText[$rule];
                $replace = array(
                    ':name' => $name,
                    ':param' => (isset($param)) ? $param : NULL
                );
                $response = str_replace(array_keys($replace), array_values($replace), $error);

                $this->_errors[] = $response;
            }

        }

    }

    public function exec()
    {
        return (empty($this->_errors)) ? false : $this->_errors;
    }

    /*
    * Rule functions
    */

    public function min_length($value, $param)
    {
        return (strlen($value) < $param);
    }

    public function max_length($value, $param) 
     {
        return (strlen($value) > $param);
    }

    public function email($value) 
    {
        return !filter_var($value, FILTER_VALIDATE_EMAIL);
    }

    public function required($value) 
    {
        return (strlen($value) !== 0);
    }

    public function ip($value) 
    {
        return !filter_var($value, FILTER_VALIDATE_IP);
    }

    public function match($value, $param)
    {
        return ($value != $param);
    }

    public function match_exact($value, $param) 
    {
        return ($value !== $param);
    }

    public function match_password($value, $param) 
    {
        return ($value !== $param);
    }

    public function alphanum($value)
    {
        return !ctype_alnum($value);
    }

    public function url($value)
    {
        return !filter_var($value, FILTER_VALIDATE_URL);
    }

    public function numeric($value)
    {
        return !(is_numeric($value));
    }

    public function min($value) 
    {
        return ($value < $param);
    }

    public function max($value) {
        return ($value > $param);
    }

}
<?php

require_once dirname(__FILE__).'/validate/rules.php';
require_once dirname(__FILE__).'/validate/errors.php';

Class Validate {

    private $valErrors = array();

    public function rule($value, $name, $rules, $static = FALSE)
    {
        $rules = explode('|', $rules);

        $response = FALSE;

        foreach ($rules as $rule) {

            if (!strlen($value) && $rule != 'required') {
                break;
            }

            if (preg_match('/\[(.*?)\]/', $rule, $match)) {

                $rule = explode('[', $rule);
                $rule = $rule[0];
                $param = $match[1];
                $call = array($value, $param);
            }
            else {
                $call = array($value);
            }
 
            $validate = new Rules;
            if (!method_exists($validate, $rule)) {
                throw new Exception("Method {$rule} not found");
            }
            
            $response = call_user_func_array(array($validate, $rule), $call);   

            if (!$response && !$static) {
                $this->valErrors[] = array('rule' => $rule, 'name' => $name, 'param' => (isset($param)) ? $param : null);
            }
            
        }

        return $response;
    }

    public function errors($errorFile = 'validate/errors.english.php')
    {

        $errorFile = dirname(__FILE__)."/{$errorFile}";

        if (empty($this->valErrors)) {
            return false;
        }

        $errors = new Errors($errorFile);

        return $errors->error($this->valErrors);

    }

}

I'd like to thank you all for your answers. I really appreciate you guys taking time out of your day to leave such detailed responses.

I've taken everything you've said into consideration and I've split it into classes; trying to give each one a single responsibility. I hope this is a better attempt!

Thanks.

added 3076 characters in body
Source Link
purpletree
  • 213
  • 1
  • 3
  • 7
Loading
Source Link
purpletree
  • 213
  • 1
  • 3
  • 7
Loading