Skip to content

'equalto' applied to '_confirmation' field #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 47 additions & 23 deletions src/HappyDemon/LaravelParsley/ParsleyConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,23 @@

use Illuminate\Translation\Translator;

class ParsleyConverter {
class ParsleyConverter
{

use \Illuminate\Console\AppNamespaceDetectorTrait;

protected $rules = [];
protected $rules = [];
protected $customAttributes = [];

/**
* @type Translator
*/
protected $translator = null;
protected $translator = null;

public function __construct($formRequest=null)
public function __construct($formRequest = null)
{
if($formRequest != null && !is_object($formRequest))
{
$class = $this->getAppNamespace() . 'Http\Requests\\'.$formRequest;
if ($formRequest != null && !is_object($formRequest)) {
$class = $this->getAppNamespace() . 'Http\Requests\\' . $formRequest;
$formRequest = new $class;
}

Expand All @@ -37,6 +38,12 @@ public function __construct($formRequest=null)
public function getFieldRules($field)
{
$rules = [];

if (ends_with($field, '_confirmation')) {
return $this->getConfirmationRule($field);
}


if (isset($this->rules[$field])) {
$rawRules = explode('|', $this->rules[$field]);

Expand All @@ -46,9 +53,23 @@ public function getFieldRules($field)
return $rules;
}

public function getConfirmationRule($field)
{
$rule = "confirmed";
$parsleyRule = 'equalto';
$rules = [$rule];
$attrs = [];
$attribute = substr($field, 0, strlen($field) - 13);
$message = $this->getMessage($attribute, $rule, $rules);
$params = "#{$attribute}";
$attrs['data-parsley-' . $parsleyRule] = $params;
$attrs['data-parsley-' . $parsleyRule . '-message'] = str_replace(':attribute', $this->getAttribute($attribute), $message);
return $attrs;
}

public function convertRules($attribute, $rules)
{
$attrs = [];
$attrs = [];
$message = null;

$date_format = null;
Expand Down Expand Up @@ -91,8 +112,8 @@ public function convertRules($attribute, $rules)

case 'between':
$parsleyRule = 'length';
$params = str_replace([':min', ':max'], $params, '[:min,:max]');
$message = str_replace([':min', ':max'], $params, $message);
$params = str_replace([':min', ':max'], $params, '[:min,:max]');
$message = str_replace([':min', ':max'], $params, $message);
break;

case 'integer':
Expand Down Expand Up @@ -124,14 +145,15 @@ public function convertRules($attribute, $rules)
break;

case 'confirmed':
$parsleyRule = 'equalto';
$params = "#{$attribute}_confirmation";
$parsleyRule = '';
// $parsleyRule = 'equalto';
// $params = "#{$attribute}_confirmation";
break;

case 'different':
$parsleyRule = 'different';
$message = str_replace(':other', $params[0], $message);
$params = '#'.$params[0];
$params = '#' . $params[0];
break;

case 'date_format':
Expand All @@ -153,31 +175,30 @@ public function convertRules($attribute, $rules)
$params = str_replace(array_keys($replace), array_values($replace), $params[0]);
$date_format = $params;
$message = str_replace(':format', $params, $message);
break;
break;
case 'before':
case 'after':
$params = $params[0];
if($date_format !== null)
{
$params .= '|-|'.$date_format;
}
if ($date_format !== null) {
$params .= '|-|' . $date_format;
}
break;
case 'in':
case 'not_in':
$parsleyRule = camel_case($rule).'List';
$parsleyRule = camel_case($rule) . 'List';
$params = implode(',', $params);
break;
default:
$message = null;
break;
}

if ($message) {
if ($message && $parsleyRule) {
if (is_array($params) && count($params) == 1) {
$params = $params[0];
}

$attrs['data-parsley-' . $parsleyRule] = $params;
$attrs['data-parsley-' . $parsleyRule] = $params;
$attrs['data-parsley-' . $parsleyRule . '-message'] = str_replace(':attribute', $this->getAttribute($attribute), $message);
}

Expand Down Expand Up @@ -235,7 +256,8 @@ protected function getAttribute($attribute)
}
}

protected function hasNumericRule($rules) {
protected function hasNumericRule($rules)
{
foreach ($rules as $rule) {
list($rule, $params) = explode(':', $rule . ':');

Expand All @@ -247,7 +269,9 @@ protected function hasNumericRule($rules) {
return false;
}

protected function isNumericRule($rule) {
protected function isNumericRule($rule)
{
return in_array(ucfirst($rule), ['Integer', 'Numeric']);
}

}