diff --git a/config/config.php b/config/config.php index 22fd887..8a5d7e8 100644 --- a/config/config.php +++ b/config/config.php @@ -1,7 +1,7 @@ true, - 'plugin' => 'JqueryValidation', - 'route' => 'laravalid' -]; \ No newline at end of file + 'useLaravelMessages' => true, + 'plugin' => 'JqueryValidation', + 'route' => 'laravalid', +]; diff --git a/src/Bllim/Laravalid/Converter/Base/Container.php b/src/Bllim/Laravalid/Converter/Base/Container.php index 247a71e..e05df96 100644 --- a/src/Bllim/Laravalid/Converter/Base/Container.php +++ b/src/Bllim/Laravalid/Converter/Base/Container.php @@ -1,38 +1,38 @@ - * @license MIT + * * @see Illuminate\Html\FormBuilder + * * @version 0.9 */ - -abstract class Container { - - protected $customMethods = []; - - public function convert($name, $parameters) - { - $methodName = strtolower($name); - - if(isset($this->customMethods[$methodName])) - { - return call_user_func_array($this->customMethods[$methodName], $parameters); - } - - if(method_exists($this, $methodName)) - { - return call_user_func_array([$this, $methodName], $parameters); - } - - return []; - } - - public function extend($name, $function) - { - $this->customMethods[$name] = $function; - } - -} \ No newline at end of file +abstract class Container +{ + protected $customMethods = []; + + public function convert($name, $parameters) + { + $methodName = strtolower($name); + + if (isset($this->customMethods[$methodName])) { + return call_user_func_array($this->customMethods[$methodName], $parameters); + } + + if (method_exists($this, $methodName)) { + return call_user_func_array([$this, $methodName], $parameters); + } + + return []; + } + + public function extend($name, $function) + { + $this->customMethods[$name] = $function; + } +} diff --git a/src/Bllim/Laravalid/Converter/Base/Converter.php b/src/Bllim/Laravalid/Converter/Base/Converter.php index 7b6c24b..8df347c 100644 --- a/src/Bllim/Laravalid/Converter/Base/Converter.php +++ b/src/Bllim/Laravalid/Converter/Base/Converter.php @@ -1,276 +1,263 @@ - * @license MIT + * * @see Illuminate\Html\FormBuilder + * * @version 0.9 */ - -abstract class Converter { - - /** - * Rule converter class instance - * - * @var array - */ - protected static $rule; - - /** - * Message converter class instance - * - * @var array - */ - protected static $message; - - /** - * Route redirecter class instance - * - * @var array - */ - protected static $route; - - /** - * Rules which specify input type is numeric - * - * @var array - */ - protected $validationRules = []; - - /** - * Current form name - * - * @var string - */ - protected $currentFormName = null; - - - /** - * Rules which specify input type is numeric - * - * @var array - */ - protected $numericRules = ['integer', 'numeric']; - - public function __construct() - { - self::$rule = new Rule(); - self::$message = new Message(); - self::$route = new Route(); - } - - public function rule() - { - return static::$rule; - } - - public function message() - { - return static::$message; - } - - public function route() - { - return static::$route; - } - - /** - * Set rules for validation - * - * @param array $rules Laravel validation rules - * - */ - public function set($rules, $formName = null) - { - if($rules === null) return; - - $this->validationRules[$formName] = $rules; - } - - /** - * Reset validation rules - * - */ - public function reset() - { - if(isset($this->validationRules[$this->currentFormName])) - { - unset($this->validationRules[$this->currentFormName]); - } - else if(isset($this->validationRules[null])) - { - unset($this->validationRules[null]); - } - } - - /** - * Set form name in order to get related validation rules - * - * @param array $formName Form name - * - */ - public function setFormName($formName) - { - $this->currentFormName = $formName; - } - - - /** - * Get all given validation rules - * - * @param array $rules Laravel validation rules - * - */ - public function getValidationRules() - { - if(isset($this->validationRules[$this->currentFormName])) - { - return $this->validationRules[$this->currentFormName]; - } - else if(isset($this->validationRules[null])) - { - return $this->validationRules[null]; - } - - return null; - } - - /** - * Returns validation rules for given input name - * - * @return string - */ - protected function getValidationRule($inputName) - { - return is_array($this->getValidationRules()[$inputName]) - ? $this->getValidationRules()[$inputName] - : explode('|', $this->getValidationRules()[$inputName]); - } - - /** - * Checks if there is a rules for given input name - * - * @return string - */ - protected function checkValidationRule($inputName) - { - return isset($this->getValidationRules()[$inputName]); - } - - - public function convert($inputName) - { - - $inputName = $this->formatInputName($inputName); - - $outputAttributes = []; - - if($this->checkValidationRule($inputName) === false) - { - return []; - } - - $rules = $this->getValidationRule($inputName); - $type = $this->getTypeOfInput($rules); - - foreach ($rules as $rule) - { - $parsedRule = $this->parseValidationRule($rule); - $outputAttributes = $outputAttributes + $this->rule()->convert($parsedRule['name'], [$parsedRule, $inputName, $type]); - - if(\Config::get('laravalid.useLaravelMessages', true)) - { - $messageAttributes = $this->message()->convert($parsedRule['name'], [$parsedRule, $inputName, $type]); - - // if empty message attributes - if(empty($messageAttributes)) - { - $messageAttributes = $this->getDefaultErrorMessage($parsedRule['name'], $inputName); - } - } - - $outputAttributes = $outputAttributes + $messageAttributes; - } - - return $outputAttributes; - } - - /** - * Get all rules and return type of input if rule specifies type - * Now, just for numeric - * - * @return string - */ - protected function getTypeOfInput($rulesOfInput) - { - foreach ($rulesOfInput as $key => $rule) { - $parsedRule = $this->parseValidationRule($rule); - if(in_array($parsedRule['name'], $this->numericRules)) - { - return 'numeric'; - } - elseif ($parsedRule['name'] === 'array') - { - return 'array'; - } - } - - return 'string'; - } - - /** - * Parses validition rule of laravel - * - * @return array - */ - protected function parseValidationRule($rule) - { - $ruleArray = ['name' => '', 'parameters' => []]; - - $explodedRule = explode(':', $rule); - $ruleArray['name'] = array_shift($explodedRule); - $ruleArray['parameters'] = explode(',', array_shift($explodedRule)); - - return $ruleArray; - } - - /** - * Gets default error message - * - * @return string - */ - protected function getDefaultErrorMessage($laravelRule, $attribute) - { - // getting user friendly validation message - $message = Helper::getValidationMessage($attribute, $laravelRule); - - return ['data-msg-'.$laravelRule => $message]; - } - - /** - * Format recursive array like input names to laravel validation format - * Example name[en] will transform to name.en - * @param string $inputName - * @return string - */ - protected function formatInputName($inputName) - { - preg_match_all("/\[(\s*[\w]*\s*)\]/", $inputName, $output, PREG_PATTERN_ORDER); - - if(!isset($output[1])) return $inputName; - - $replaceWith = $output[1]; - $replace = $output[0]; - - foreach($replaceWith as $key => $r) - $replaceWith[$key] = '.' . $r; - - return str_replace($replace, $replaceWith, $inputName); - } - -} \ No newline at end of file +abstract class Converter +{ + /** + * Rule converter class instance. + * + * @var array + */ + protected static $rule; + + /** + * Message converter class instance. + * + * @var array + */ + protected static $message; + + /** + * Route redirecter class instance. + * + * @var array + */ + protected static $route; + + /** + * Rules which specify input type is numeric. + * + * @var array + */ + protected $validationRules = []; + + /** + * Current form name. + * + * @var string + */ + protected $currentFormName = null; + + /** + * Rules which specify input type is numeric. + * + * @var array + */ + protected $numericRules = ['integer', 'numeric']; + + public function __construct() + { + self::$rule = new Rule(); + self::$message = new Message(); + self::$route = new Route(); + } + + public function rule() + { + return static::$rule; + } + + public function message() + { + return static::$message; + } + + public function route() + { + return static::$route; + } + + /** + * Set rules for validation. + * + * @param array $rules Laravel validation rules + */ + public function set($rules, $formName = null) + { + if ($rules === null) { + return; + } + + $this->validationRules[$formName] = $rules; + } + + /** + * Reset validation rules. + */ + public function reset() + { + if (isset($this->validationRules[$this->currentFormName])) { + unset($this->validationRules[$this->currentFormName]); + } elseif (isset($this->validationRules[null])) { + unset($this->validationRules[null]); + } + } + + /** + * Set form name in order to get related validation rules. + * + * @param array $formName Form name + */ + public function setFormName($formName) + { + $this->currentFormName = $formName; + } + + /** + * Get all given validation rules. + * + * @param array $rules Laravel validation rules + */ + public function getValidationRules() + { + if (isset($this->validationRules[$this->currentFormName])) { + return $this->validationRules[$this->currentFormName]; + } elseif (isset($this->validationRules[null])) { + return $this->validationRules[null]; + } + + return; + } + + /** + * Returns validation rules for given input name. + * + * @return string + */ + protected function getValidationRule($inputName) + { + return is_array($this->getValidationRules()[$inputName]) + ? $this->getValidationRules()[$inputName] + : explode('|', $this->getValidationRules()[$inputName]); + } + + /** + * Checks if there is a rules for given input name. + * + * @return string + */ + protected function checkValidationRule($inputName) + { + return isset($this->getValidationRules()[$inputName]); + } + + public function convert($inputName) + { + $inputName = $this->formatInputName($inputName); + + $outputAttributes = []; + + if ($this->checkValidationRule($inputName) === false) { + return []; + } + + $rules = $this->getValidationRule($inputName); + $type = $this->getTypeOfInput($rules); + + foreach ($rules as $rule) { + $parsedRule = $this->parseValidationRule($rule); + $outputAttributes = $outputAttributes + $this->rule()->convert($parsedRule['name'], [$parsedRule, $inputName, $type]); + + if (\Config::get('laravalid.useLaravelMessages', true)) { + $messageAttributes = $this->message()->convert($parsedRule['name'], [$parsedRule, $inputName, $type]); + + // if empty message attributes + if (empty($messageAttributes)) { + $messageAttributes = $this->getDefaultErrorMessage($parsedRule['name'], $inputName); + } + } + + $outputAttributes = $outputAttributes + $messageAttributes; + } + + return $outputAttributes; + } + + /** + * Get all rules and return type of input if rule specifies type + * Now, just for numeric. + * + * @return string + */ + protected function getTypeOfInput($rulesOfInput) + { + foreach ($rulesOfInput as $key => $rule) { + $parsedRule = $this->parseValidationRule($rule); + if (in_array($parsedRule['name'], $this->numericRules)) { + return 'numeric'; + } elseif ($parsedRule['name'] === 'array') { + return 'array'; + } + } + + return 'string'; + } + + /** + * Parses validition rule of laravel. + * + * @return array + */ + protected function parseValidationRule($rule) + { + $ruleArray = ['name' => '', 'parameters' => []]; + + $explodedRule = explode(':', $rule); + $ruleArray['name'] = array_shift($explodedRule); + $ruleArray['parameters'] = explode(',', array_shift($explodedRule)); + + return $ruleArray; + } + + /** + * Gets default error message. + * + * @return string + */ + protected function getDefaultErrorMessage($laravelRule, $attribute) + { + // getting user friendly validation message + $message = Helper::getValidationMessage($attribute, $laravelRule); + + return ['data-msg-'.$laravelRule => $message]; + } + + /** + * Format recursive array like input names to laravel validation format + * Example name[en] will transform to name.en. + * + * @param string $inputName + * + * @return string + */ + protected function formatInputName($inputName) + { + preg_match_all("/\[(\s*[\w]*\s*)\]/", $inputName, $output, PREG_PATTERN_ORDER); + + if (!isset($output[1])) { + return $inputName; + } + + $replaceWith = $output[1]; + $replace = $output[0]; + + foreach ($replaceWith as $key => $r) { + $replaceWith[$key] = '.'.$r; + } + + return str_replace($replace, $replaceWith, $inputName); + } +} diff --git a/src/Bllim/Laravalid/Converter/Base/Message.php b/src/Bllim/Laravalid/Converter/Base/Message.php index b65df6c..3e99789 100644 --- a/src/Bllim/Laravalid/Converter/Base/Message.php +++ b/src/Bllim/Laravalid/Converter/Base/Message.php @@ -1,14 +1,17 @@ - * @license MIT + * * @see Illuminate\Html\FormBuilder + * * @version 0.9 */ - -abstract class Message extends Container { - -} \ No newline at end of file +abstract class Message extends Container +{ +} diff --git a/src/Bllim/Laravalid/Converter/Base/Route.php b/src/Bllim/Laravalid/Converter/Base/Route.php index c871ea4..496b647 100644 --- a/src/Bllim/Laravalid/Converter/Base/Route.php +++ b/src/Bllim/Laravalid/Converter/Base/Route.php @@ -1,5 +1,8 @@ -customMethods[$methodName])) - { - return call_user_func_array($this->customMethods[$methodName], $parameters); - } - - if(method_exists($this, $methodName)) - { - return call_user_func_array([$this, $methodName], $parameters); - } - - return $this->defaultRoute($name, $parameters); - } - - public function defaultRoute($name, $parameters) - { - $params = Helper::decrypt($parameters['params']); - unset($parameters['params']); - - $rules = array(); - foreach ($parameters as $k => $v) - { - $rules[$k] = $name . ':' . $params; - } - - $validator = \Validator::make( - $parameters, - $rules - ); - - if (!$validator->fails()) - return \Response::json(true); - - return \Response::json($validator->messages()->first()); - } -} \ No newline at end of file +abstract class Route extends Container +{ + public function convert($name, $parameters) + { + $methodName = strtolower($name); + + if (isset($this->customMethods[$methodName])) { + return call_user_func_array($this->customMethods[$methodName], $parameters); + } + + if (method_exists($this, $methodName)) { + return call_user_func_array([$this, $methodName], $parameters); + } + + return $this->defaultRoute($name, $parameters); + } + + public function defaultRoute($name, $parameters) + { + $params = Helper::decrypt($parameters['params']); + unset($parameters['params']); + + $rules = []; + foreach ($parameters as $k => $v) { + $rules[$k] = $name.':'.$params; + } + + $validator = \Validator::make( + $parameters, + $rules + ); + + if (!$validator->fails()) { + return \Response::json(true); + } + + return \Response::json($validator->messages()->first()); + } +} diff --git a/src/Bllim/Laravalid/Converter/Base/Rule.php b/src/Bllim/Laravalid/Converter/Base/Rule.php index 11485c3..f1e2380 100644 --- a/src/Bllim/Laravalid/Converter/Base/Rule.php +++ b/src/Bllim/Laravalid/Converter/Base/Rule.php @@ -1,14 +1,17 @@ - * @license MIT + * * @see Illuminate\Html\FormBuilder + * * @version 0.9 */ - -abstract class Rule extends Container { - -} \ No newline at end of file +abstract class Rule extends Container +{ +} diff --git a/src/Bllim/Laravalid/Converter/JqueryValidation/Converter.php b/src/Bllim/Laravalid/Converter/JqueryValidation/Converter.php index 0a28742..1ad790a 100644 --- a/src/Bllim/Laravalid/Converter/JqueryValidation/Converter.php +++ b/src/Bllim/Laravalid/Converter/JqueryValidation/Converter.php @@ -1,16 +1,17 @@ - $message]; + } + + public function same($parsedRule, $attribute, $type) + { + $message = Lang::get('validation.'.$parsedRule['name'], ['attribute' => $attribute]); + + return ['data-msg-equalto' => $message]; + } + + public function alpha($parsedRule, $attribute, $type) + { + $message = Helper::getValidationMessage($attribute, $parsedRule['name']); + + return ['data-msg-regex' => $message]; + } + + public function alphanum($parsedRule, $attribute, $type) + { + $message = Helper::getValidationMessage($attribute, $parsedRule['name']); + + return ['data-msg-regex' => $message]; + } + + public function integer($parsedRule, $attribute, $type) + { + $message = Helper::getValidationMessage($attribute, $parsedRule['name']); + + return ['data-msg-number' => $message]; + } + + public function numeric($parsedRule, $attribute, $type) + { + $message = Helper::getValidationMessage($attribute, $parsedRule['name']); + + return ['data-msg-number' => $message]; + } + + public function max($parsedRule, $attribute, $type) + { + $message = Helper::getValidationMessage($attribute, $parsedRule['name'], ['max' => $parsedRule['parameters'][0]], $type); + switch ($type) { + case 'numeric': + return ['data-msg-max' => $message]; + break; + + default: + return ['data-msg-maxlength' => $message]; + break; + } + } + + public function min($parsedRule, $attribute, $type) + { + $message = Helper::getValidationMessage($attribute, $parsedRule['name'], ['min' => $parsedRule['parameters'][0]], $type); + switch ($type) { + case 'numeric': + return ['data-msg-min' => $message]; + break; + + default: + return ['data-msg-minlength' => $message]; + break; + } + } -class Message extends \Bllim\Laravalid\Converter\Base\Message { - - public function ip($parsedRule, $attribute, $type) - { - $message = Helper::getValidationMessage($attribute, $parsedRule['name']); - return ['data-msg-ipv4' => $message]; - } - - public function same($parsedRule, $attribute, $type) - { - $message = Lang::get('validation.'.$parsedRule['name'], ['attribute' => $attribute]); - return ['data-msg-equalto' => $message]; - } - - public function alpha($parsedRule, $attribute, $type) - { - $message = Helper::getValidationMessage($attribute, $parsedRule['name']); - return ['data-msg-regex' => $message]; - } - - public function alphanum($parsedRule, $attribute, $type) - { - $message = Helper::getValidationMessage($attribute, $parsedRule['name']); - return ['data-msg-regex' => $message]; - } - - public function integer($parsedRule, $attribute, $type) - { - $message = Helper::getValidationMessage($attribute, $parsedRule['name']); - return ['data-msg-number' => $message]; - } - - public function numeric($parsedRule, $attribute, $type) - { - $message = Helper::getValidationMessage($attribute, $parsedRule['name']); - return ['data-msg-number' => $message]; - } - - public function max($parsedRule, $attribute, $type) - { - $message = Helper::getValidationMessage($attribute, $parsedRule['name'], ['max' => $parsedRule['parameters'][0]], $type); - switch ($type) { - case 'numeric': - return ['data-msg-max' => $message]; - break; - - default: - return ['data-msg-maxlength' => $message]; - break; - } - } - - public function min($parsedRule, $attribute, $type) - { - $message = Helper::getValidationMessage($attribute, $parsedRule['name'], ['min' => $parsedRule['parameters'][0]], $type); - switch ($type) { - case 'numeric': - return ['data-msg-min' => $message]; - break; - - default: - return ['data-msg-minlength' => $message]; - break; - } - } - - public function between($parsedRule, $attribute, $type) - { - $message = Helper::getValidationMessage($attribute, $parsedRule['name'], ['min' => $parsedRule['parameters'][0], 'max' => $parsedRule['parameters'][1]], $type); - switch ($type) { - case 'numeric': - return ['data-msg-range' => $message]; - break; - - default: - return ['data-msg-minlength' => $message, 'data-msg-maxlength' => $message]; - break; - } - } + public function between($parsedRule, $attribute, $type) + { + $message = Helper::getValidationMessage($attribute, $parsedRule['name'], ['min' => $parsedRule['parameters'][0], 'max' => $parsedRule['parameters'][1]], $type); + switch ($type) { + case 'numeric': + return ['data-msg-range' => $message]; + break; + default: + return ['data-msg-minlength' => $message, 'data-msg-maxlength' => $message]; + break; + } + } } diff --git a/src/Bllim/Laravalid/Converter/JqueryValidation/Route.php b/src/Bllim/Laravalid/Converter/JqueryValidation/Route.php index 2ac2063..5e5fb99 100644 --- a/src/Bllim/Laravalid/Converter/JqueryValidation/Route.php +++ b/src/Bllim/Laravalid/Converter/JqueryValidation/Route.php @@ -1,8 +1,7 @@ - '', 'parameters' => []] - * @param array - * @param array type of input - * @return array - */ - - public function email($parsedRule, $attribute, $type) - { - return ['data-rule-email' => 'true']; - } - - public function required($parsedRule, $attribute, $type) - { - return ['data-rule-required' => 'true']; - } - - public function url($parsedRule, $attribute, $type) - { - return ['data-rule-url' => 'true']; - } - - public function integer($parsedRule, $attribute, $type) - { - return ['data-rule-number' => 'true']; - } - - public function numeric($parsedRule, $attribute, $type) - { - return ['data-rule-number' => 'true']; - } - - public function ip($parsedRule, $attribute, $type) - { - return ['data-rule-ipv4' => 'true']; - } - - public function same($parsedRule, $attribute, $type) - { - $value = vsprintf("*[name='%1s']", $parsedRule['parameters']); - return ['data-rule-equalto' => $value]; - } - - public function regex($parsedRule, $attribute, $type) - { - $rule = $parsedRule['parameters'][0]; - - if(substr($rule, 0, 1) == substr($rule, -1, 1)) - { - $rule = substr($rule, 1, -1); - } - - return ['data-rule-regex' => $rule]; - } - - public function alpha($parsedRule, $attribute, $type) - { - return ['data-rule-regex' => "^[A-Za-z _.-]+$"]; - } - - public function alphanum($parsedRule, $attribute, $type) - { - return ['data-rule-regex' => "^[A-Za-z0-9 _.-]+$"]; - } - - public function image($parsedRule, $attribute, $type) - { - return ['accept' => "image/*"]; - } - - public function date($parsedRule, $attribute, $type) - { - return ['data-rule-date' => "true"]; - } - - public function min($parsedRule, $attribute, $type) - { - switch ($type) - { - case 'numeric': - return ['data-rule-min' => vsprintf("%1s", $parsedRule['parameters'])]; - break; - - default: - return ['data-rule-minlength' => vsprintf("%1s", $parsedRule['parameters'])]; - break; - } - } - - public function max($parsedRule, $attribute, $type) - { - switch ($type) - { - case 'numeric': - return ['data-rule-max' => vsprintf("%1s", $parsedRule['parameters'])]; - break; - - default: - return ['data-rule-maxlength' => vsprintf("%1s", $parsedRule['parameters'])]; - break; - } - } - - public function between($parsedRule, $attribute, $type) - { - switch ($type) - { - case 'numeric': - return ['data-rule-range' => vsprintf("%1s,%2s", $parsedRule['parameters'])]; - break; - - default: - return ['data-rule-minlength' => $parsedRule['parameters'][0], 'data-rule-maxlength' => $parsedRule['parameters'][1]]; - break; - } - } - - public function unique($parsedRule, $attribute, $type) - { - $param = implode(',', $parsedRule['parameters']); - $encrpytedParam = Helper::encrypt($param); - $route = \Config::get('laravalid.route', 'laravalid'); - return ['data-rule-remote' => url('/' . $route . '/unique').'?params=' . $encrpytedParam]; - } - - public function exists($parsedRule, $attribute, $type) - { - $param = implode(',', $parsedRule['parameters']); - $encrpytedParam = Helper::encrypt($param); - $route = \Config::get('laravalid.route', 'laravalid'); - return ['data-rule-remote' => url('/' . $route . '/exists').'?params=' . $encrpytedParam]; - } - - -} \ No newline at end of file +class Rule extends \Bllim\Laravalid\Converter\Base\Rule +{ + /** + * Rules convertions which return attributes as an array. + * + * @param array ['name' => '', 'parameters' => []] + * @param array + * @param array type of input + * + * @return array + */ + public function email($parsedRule, $attribute, $type) + { + return ['data-rule-email' => 'true']; + } + + public function required($parsedRule, $attribute, $type) + { + return ['data-rule-required' => 'true']; + } + + public function url($parsedRule, $attribute, $type) + { + return ['data-rule-url' => 'true']; + } + + public function integer($parsedRule, $attribute, $type) + { + return ['data-rule-number' => 'true']; + } + + public function numeric($parsedRule, $attribute, $type) + { + return ['data-rule-number' => 'true']; + } + + public function ip($parsedRule, $attribute, $type) + { + return ['data-rule-ipv4' => 'true']; + } + + public function same($parsedRule, $attribute, $type) + { + $value = vsprintf("*[name='%1s']", $parsedRule['parameters']); + + return ['data-rule-equalto' => $value]; + } + + public function regex($parsedRule, $attribute, $type) + { + $rule = $parsedRule['parameters'][0]; + + if (substr($rule, 0, 1) == substr($rule, -1, 1)) { + $rule = substr($rule, 1, -1); + } + + return ['data-rule-regex' => $rule]; + } + + public function alpha($parsedRule, $attribute, $type) + { + return ['data-rule-regex' => '^[A-Za-z _.-]+$']; + } + + public function alphanum($parsedRule, $attribute, $type) + { + return ['data-rule-regex' => '^[A-Za-z0-9 _.-]+$']; + } + + public function image($parsedRule, $attribute, $type) + { + return ['accept' => 'image/*']; + } + + public function date($parsedRule, $attribute, $type) + { + return ['data-rule-date' => 'true']; + } + + public function min($parsedRule, $attribute, $type) + { + switch ($type) { + case 'numeric': + return ['data-rule-min' => vsprintf('%1s', $parsedRule['parameters'])]; + break; + + default: + return ['data-rule-minlength' => vsprintf('%1s', $parsedRule['parameters'])]; + break; + } + } + + public function max($parsedRule, $attribute, $type) + { + switch ($type) { + case 'numeric': + return ['data-rule-max' => vsprintf('%1s', $parsedRule['parameters'])]; + break; + + default: + return ['data-rule-maxlength' => vsprintf('%1s', $parsedRule['parameters'])]; + break; + } + } + + public function between($parsedRule, $attribute, $type) + { + switch ($type) { + case 'numeric': + return ['data-rule-range' => vsprintf('%1s,%2s', $parsedRule['parameters'])]; + break; + + default: + return ['data-rule-minlength' => $parsedRule['parameters'][0], 'data-rule-maxlength' => $parsedRule['parameters'][1]]; + break; + } + } + + public function unique($parsedRule, $attribute, $type) + { + $param = implode(',', $parsedRule['parameters']); + $encrpytedParam = Helper::encrypt($param); + $route = \Config::get('laravalid.route', 'laravalid'); + + return ['data-rule-remote' => url('/'.$route.'/unique').'?params='.$encrpytedParam]; + } + + public function exists($parsedRule, $attribute, $type) + { + $param = implode(',', $parsedRule['parameters']); + $encrpytedParam = Helper::encrypt($param); + $route = \Config::get('laravalid.route', 'laravalid'); + + return ['data-rule-remote' => url('/'.$route.'/exists').'?params='.$encrpytedParam]; + } +} diff --git a/src/Bllim/Laravalid/Facade.php b/src/Bllim/Laravalid/Facade.php index 1a0a551..23a7148 100644 --- a/src/Bllim/Laravalid/Facade.php +++ b/src/Bllim/Laravalid/Facade.php @@ -1,7 +1,11 @@ -converter = $converter; - } - - /** - * Set rules for validation - * - * @param array $rules Laravel validation rules - * - */ - public function setValidation($rules, $formName = null) - { - $this->converter()->set($rules, $formName); - } - - /** - * Get binded converter class - * - * @param array $rules Laravel validation rules - * - */ - public function converter() - { - return $this->converter; - } - - /** - * Reset validation rules - * - */ - public function resetValidation() - { - $this->converter()->reset(); - } - - /** - * Opens form, set rules - * - * @param array $rules Laravel validation rules - * - * @see Illuminate\Html\FormBuilder - */ - public function open(array $options = array(), $rules = null) - { - $this->setValidation($rules); - - if(isset($options['name'])) - { - $this->converter->setFormName($options['name']); - } - else - { - $this->converter->setFormName(null); - } - - return parent::open($options); - } - - /** - * Create a new model based form builder. - * - * @param array $rules Laravel validation rules - * - * @see Illuminate\Html\FormBuilder - */ - public function model($model, array $options = array(), $rules = null) - { - $this->setValidation($rules); - return parent::model($model, $options); - } - - /** - * @see Illuminate\Html\FormBuilder - */ - public function input($type, $name, $value = null, $options = []) - { - $options = $this->converter->convert(Helper::getFormAttribute($name)) + $options; - return parent::input($type, $name, $value, $options); - } - - /** - * @see Illuminate\Html\FormBuilder - */ - public function textarea($name, $value = null, $options = []) - { - $options = $this->converter->convert(Helper::getFormAttribute($name)) + $options; - return parent::textarea($name, $value, $options); - } - - /** - * @see Illuminate\Html\FormBuilder - */ - public function select($name, $list = [], $selected = null, $options = []) - { - $options = $this->converter->convert(Helper::getFormAttribute($name)) + $options; - return parent::select($name, $list, $selected, $options); - } - - protected function checkable($type, $name, $value, $checked, $options) - { - $options = $this->converter->convert(Helper::getFormAttribute($name)) + $options; - return parent::checkable($type, $name, $value, $checked, $options); - } - - /** - * Closes form and reset $this->rules - * - * @see Illuminate\Html\FormBuilder - */ - public function close() - { - $this->resetValidation(); - return parent::close(); - } - - -} \ No newline at end of file + +class FormBuilder extends \Collective\Html\FormBuilder +{ + protected $converter; + + public function __construct(\Collective\Html\HtmlBuilder $html, \Illuminate\Routing\UrlGenerator $url, $csrfToken, Converter\Base\Converter $converter) + { + parent::__construct($html, $url, $csrfToken); + $plugin = \Config::get('laravalid.plugin'); + $this->converter = $converter; + } + + /** + * Set rules for validation. + * + * @param array $rules Laravel validation rules + */ + public function setValidation($rules, $formName = null) + { + $this->converter()->set($rules, $formName); + } + + /** + * Get binded converter class. + * + * @param array $rules Laravel validation rules + */ + public function converter() + { + return $this->converter; + } + + /** + * Reset validation rules. + */ + public function resetValidation() + { + $this->converter()->reset(); + } + + /** + * Opens form, set rules. + * + * @param array $rules Laravel validation rules + * + * @see Illuminate\Html\FormBuilder + */ + public function open(array $options = [], $rules = null) + { + $this->setValidation($rules); + + if (isset($options['name'])) { + $this->converter->setFormName($options['name']); + } else { + $this->converter->setFormName(null); + } + + return parent::open($options); + } + + /** + * Create a new model based form builder. + * + * @param array $rules Laravel validation rules + * + * @see Illuminate\Html\FormBuilder + */ + public function model($model, array $options = [], $rules = null) + { + $this->setValidation($rules); + + return parent::model($model, $options); + } + + /** + * @see Illuminate\Html\FormBuilder + */ + public function input($type, $name, $value = null, $options = []) + { + $options = $this->converter->convert(Helper::getFormAttribute($name)) + $options; + + return parent::input($type, $name, $value, $options); + } + + /** + * @see Illuminate\Html\FormBuilder + */ + public function textarea($name, $value = null, $options = []) + { + $options = $this->converter->convert(Helper::getFormAttribute($name)) + $options; + + return parent::textarea($name, $value, $options); + } + + /** + * @see Illuminate\Html\FormBuilder + */ + public function select($name, $list = [], $selected = null, $options = []) + { + $options = $this->converter->convert(Helper::getFormAttribute($name)) + $options; + + return parent::select($name, $list, $selected, $options); + } + + protected function checkable($type, $name, $value, $checked, $options) + { + $options = $this->converter->convert(Helper::getFormAttribute($name)) + $options; + + return parent::checkable($type, $name, $value, $checked, $options); + } + + /** + * Closes form and reset $this->rules. + * + * @see Illuminate\Html\FormBuilder + */ + public function close() + { + $this->resetValidation(); + + return parent::close(); + } +} diff --git a/src/Bllim/Laravalid/Helper.php b/src/Bllim/Laravalid/Helper.php index db8d0a2..1f54bd2 100644 --- a/src/Bllim/Laravalid/Helper.php +++ b/src/Bllim/Laravalid/Helper.php @@ -1,57 +1,58 @@ - * @license MIT + * * @see Illuminate\Html\FormBuilder + * * @version 0.9 */ - -class Helper { - - public static function encrypt($data) - { - return \Crypt::encrypt($data); - } - - public static function decrypt($data) - { - return \Crypt::decrypt($data); - } - - /** - * Get user friendly validation message - * - * @return string - */ - public static function getValidationMessage($attribute, $rule, $data = [], $type = null) - { - $path = $rule; - if ($type !== null) - { - $path .= '.' . $type; - } - - if (\Lang::has('validation.custom.' . $attribute . '.' . $path)) - { - $path = 'custom.' . $attribute . '.' . $path; - } - - $niceName = !\Lang::has('validation.attributes.'.$attribute) ? $attribute : \Lang::get('validation.attributes.'.$attribute); - - return \Lang::get('validation.' . $path, $data + ['attribute' => $niceName]); - } - - /** - * Get the raw attribute name without array braces - * - * @return string - */ - public static function getFormAttribute($name) - { - return substr($name, -2) === '[]' ? substr($name, 0, -2) : $name; - } -} \ No newline at end of file +class Helper +{ + public static function encrypt($data) + { + return \Crypt::encrypt($data); + } + + public static function decrypt($data) + { + return \Crypt::decrypt($data); + } + + /** + * Get user friendly validation message. + * + * @return string + */ + public static function getValidationMessage($attribute, $rule, $data = [], $type = null) + { + $path = $rule; + if ($type !== null) { + $path .= '.'.$type; + } + + if (\Lang::has('validation.custom.'.$attribute.'.'.$path)) { + $path = 'custom.'.$attribute.'.'.$path; + } + + $niceName = !\Lang::has('validation.attributes.'.$attribute) ? $attribute : \Lang::get('validation.attributes.'.$attribute); + + return \Lang::get('validation.'.$path, $data + ['attribute' => $niceName]); + } + + /** + * Get the raw attribute name without array braces. + * + * @return string + */ + public static function getFormAttribute($name) + { + return substr($name, -2) === '[]' ? substr($name, 0, -2) : $name; + } +} diff --git a/src/Bllim/Laravalid/LaravalidServiceProvider.php b/src/Bllim/Laravalid/LaravalidServiceProvider.php index c7fdaa0..46c9364 100644 --- a/src/Bllim/Laravalid/LaravalidServiceProvider.php +++ b/src/Bllim/Laravalid/LaravalidServiceProvider.php @@ -1,92 +1,92 @@ -publishes([ - __DIR__.'/../../../config' => config_path('laravalid'), - ], 'config'); - - $this->publishes([ - __DIR__.'/../../../public' => public_path('laravalid'), - ], 'public'); - - $routeName = \Config::get('laravalid.route'); - - // remote validations - \Route::any($routeName.'/{rule}', '\Bllim\Laravalid\RuleController@getIndex'); - } - - /** - * Register the service provider. - * - * @return void - */ - public function register() - { - $this->registerResources(); - - if(!isset($this->app['html'])) - { - $this->app->singleton('html', function($app) - { - return new \Collective\Html\HtmlBuilder($app['url']); - }); +class LaravalidServiceProvider extends ServiceProvider +{ + /** + * Indicates if loading of the provider is deferred. + * + * @var bool + */ + protected $defer = false; + + /** + * Bootstrap the application events. + * + * @return void + */ + public function boot() + { + $this->publishes([ + __DIR__.'/../../../config' => config_path('laravalid'), + ], 'config'); + + $this->publishes([ + __DIR__.'/../../../public' => public_path('laravalid'), + ], 'public'); + + $routeName = \Config::get('laravalid.route'); + + // remote validations + \Route::any($routeName.'/{rule}', '\Bllim\Laravalid\RuleController@getIndex'); + } + + /** + * Register the service provider. + * + * @return void + */ + public function register() + { + $this->registerResources(); + + if (!isset($this->app['html'])) { + $this->app->singleton('html', function ($app) { + return new \Collective\Html\HtmlBuilder($app['url']); + }); } $this->app->singleton('laravalid', function ($app) { - $plugin = \Config::get('laravalid.plugin'); - $converterClassName = 'Bllim\Laravalid\Converter\\'.$plugin.'\Converter'; - $converter = new $converterClassName(); + $plugin = \Config::get('laravalid.plugin'); + $converterClassName = 'Bllim\Laravalid\Converter\\'.$plugin.'\Converter'; + $converter = new $converterClassName(); + + $form = new FormBuilder($app->make('html'), $app->make('url'), $app->make('session.store')->getToken(), $converter); - $form = new FormBuilder($app->make('html'), $app->make('url'), $app->make('session.store')->getToken(), $converter); - return $form->setSessionStore($app->make('session.store')); + return $form->setSessionStore($app->make('session.store')); } ); - } - - /** - * Register the package resources. - * - * @return void - */ - protected function registerResources() - { - $userConfigFile = app()->configPath().'/laravalid/config.php'; - $packageConfigFile = __DIR__.'/../../../config/config.php'; - $config = $this->app['files']->getRequire($packageConfigFile); - - if (file_exists($userConfigFile)) { - $userConfig = $this->app['files']->getRequire($userConfigFile); - $config = array_replace_recursive($config, $userConfig); - } - - $this->app['config']->set('laravalid', $config); - } - - /** - * Get the services provided by the provider. - * - * @return array - */ - public function provides() - { - return array(); - } + } + + /** + * Register the package resources. + * + * @return void + */ + protected function registerResources() + { + $userConfigFile = app()->configPath().'/laravalid/config.php'; + $packageConfigFile = __DIR__.'/../../../config/config.php'; + $config = $this->app['files']->getRequire($packageConfigFile); + + if (file_exists($userConfigFile)) { + $userConfig = $this->app['files']->getRequire($userConfigFile); + $config = array_replace_recursive($config, $userConfig); + } + + $this->app['config']->set('laravalid', $config); + } + /** + * Get the services provided by the provider. + * + * @return array + */ + public function provides() + { + return []; + } } diff --git a/src/Bllim/Laravalid/RuleController.php b/src/Bllim/Laravalid/RuleController.php index 53cba80..a94327d 100644 --- a/src/Bllim/Laravalid/RuleController.php +++ b/src/Bllim/Laravalid/RuleController.php @@ -1,13 +1,14 @@ -route()->convert($rule, \Input::all()); - } -} \ No newline at end of file +route()->convert($rule, \Input::all()); + } +}