Skip to content
22 changes: 21 additions & 1 deletion assets/jquery_validate/js/custom_rules.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,24 @@ $.validator.addMethod(
var regexp = new RegExp(pattern);
return this.optional(element) || regexp.test(value);
}
);
);
/*
checks array notation; see effect over checkbox with hidden element
*/
/*
$.extend($.validator.prototype, {
checkForm: function() {
this.prepareForm();
for ( var i = 0, elements = (this.currentElements = this.elements()); elements[i]; i++ ) {
if (this.findByName( elements[i].name ).length != undefined && this.findByName( elements[i].name ).length > 1) {
for (var cnt = 0; cnt < this.findByName( elements[i].name ).length; cnt++) {
this.check( this.findByName( elements[i].name )[cnt] );
}
} else {
this.check( elements[i] );
}
}
return this.valid();
}
});
*/
12 changes: 11 additions & 1 deletion src/StrokerForm/Renderer/JqueryValidate/Renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ public function preRenderForm($formAlias, View $view, FormInterface $form = null
if ($options->getIncludeAssets()) {
$assetBaseUri = $this->getHttpRouter()->assemble([], ['name' => 'strokerform-asset']);
$inlineScript->appendFile($assetBaseUri . '/jquery_validate/js/jquery.validate.js');
//$inlineScript->appendFile($assetBaseUri . '/jquery_validate/js/additional-methods.min.js');

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please include the asset and don't remove this line

$inlineScript->appendFile($assetBaseUri . '/jquery_validate/js/custom_rules.js');
if ($options->isUseTwitterBootstrap() === true) {
$inlineScript->appendFile($assetBaseUri . '/jquery_validate/js/jquery.validate.bootstrap.js');
Expand All @@ -106,7 +107,16 @@ protected function buildInlineJavascript(FormInterface $form, Options $options)
{
$validateOptions = [];
foreach ($options->getValidateOptions() as $key => $value) {
$value = (is_string($value)) ? $value : var_export($value, true);

if(is_string($value)){
if(strpos($value, 'function') === 0){

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a unit test for this case please? Than we can also see if we can improve this code a little because it is a littlebit messy now with all these if / else statements.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not very familiar with unit tests unfortunatelly.

//nothing to do
} else {
$value = '"' . $value .'"';
}
} else {
$value = json_encode($value);
}
$validateOptions[] = '"' . $key . '": ' . $value;
}

Expand Down
3 changes: 1 addition & 2 deletions src/StrokerForm/Renderer/JqueryValidate/Rule/Between.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ public function getRules(ValidatorInterface $validator, ElementInterface $elemen
public function getMessages(ValidatorInterface $validator)
{
return [
'range' =>
sprintf($this->translateMessage('The input is not between %s and %s'), $this->getMin($validator), $this->getMax($validator))
'range' => sprintf($validator->getMessageTemplates()[\Zend\Validator\Between::NOT_BETWEEN], $this->getMin($validator), $this->getMax($validator))

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This message need to be passed through the translator. You can use the same method as before $this->translateMessage()

];
}

Expand Down
2 changes: 1 addition & 1 deletion src/StrokerForm/Renderer/JqueryValidate/Rule/Digits.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function getRules(ValidatorInterface $validator, ElementInterface $elemen
*/
public function getMessages(ValidatorInterface $validator)
{
return ['digits' => $this->translateMessage('The input must contain only digits')];
return ['digits' => $validator->getMessageTemplates()[\Zend\Validator\Digits::NOT_DIGITS]];
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function getRules(ValidatorInterface $validator, ElementInterface $elemen
*/
public function getMessages(ValidatorInterface $validator)
{
return ['email' => $this->translateMessage('Email address is invalid')];
return ['email' => $validator->getMessageTemplates()[\Zend\Validator\EmailAddress::INVALID_FORMAT]];
}

/**
Expand Down
3 changes: 1 addition & 2 deletions src/StrokerForm/Renderer/JqueryValidate/Rule/GreaterThan.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ public function getRules(ValidatorInterface $validator, ElementInterface $elemen
public function getMessages(ValidatorInterface $validator)
{
return [
'min' =>
sprintf($this->translateMessage('The input is not greater than %s'), $validator->getMin())
'min' => sprintf($validator->getMessageTemplates()[\Zend\Validator\GreaterThan::NOT_GREATER], $validator->getMin())
];
}

Expand Down
2 changes: 1 addition & 1 deletion src/StrokerForm/Renderer/JqueryValidate/Rule/Identical.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function getRules(ValidatorInterface $validator, ElementInterface $elemen
*/
public function getMessages(ValidatorInterface $validator)
{
return ['equalTo' => $this->translateMessage('Please enter the same value again.')];
return ['equalTo' => $validator->getMessageTemplates()[\Zend\Validator\Identical::NOT_SAME]];
}

/**
Expand Down
3 changes: 1 addition & 2 deletions src/StrokerForm/Renderer/JqueryValidate/Rule/InArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ public function getRules(ValidatorInterface $validator, ElementInterface $elemen
public function getMessages(ValidatorInterface $validator)
{
return [
'in_array' =>
$this->translateMessage('The input is not a valid option')
'in_array' => $validator->getMessageTemplates()[\Zend\Validator\InArray::NOT_IN_ARRAY]
];
}

Expand Down
3 changes: 1 addition & 2 deletions src/StrokerForm/Renderer/JqueryValidate/Rule/LessThan.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ public function getRules(ValidatorInterface $validator, ElementInterface $elemen
public function getMessages(ValidatorInterface $validator)
{
return [
'max' =>
sprintf($this->translateMessage('The input is not less than %s'), $validator->getMax())
'max' => sprintf($validator->getMessageTemplates()[\Zend\Validator\LessThan::NOT_LESS], $validator->getMax())
];
}

Expand Down
2 changes: 1 addition & 1 deletion src/StrokerForm/Renderer/JqueryValidate/Rule/NotEmpty.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function getRules(ValidatorInterface $validator, ElementInterface $elemen
*/
public function getMessages(ValidatorInterface $validator)
{
return ['required' => $this->translateMessage('The input is required and cannot be empty')];
return ['required' => $validator->getMessageTemplates()[\Zend\Validator\NotEmpty::IS_EMPTY]];
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/StrokerForm/Renderer/JqueryValidate/Rule/Regex.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function getRules(ValidatorInterface $validator, ElementInterface $elemen
*/
public function getMessages(ValidatorInterface $validator)
{
return ['regex' => $this->translateMessage('Field does not match expected pattern')];
return ['regex' => $validator->getMessageTemplates()[\Zend\Validator\Regex::NOT_MATCH]];
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/StrokerForm/Renderer/JqueryValidate/Rule/StringLength.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ public function getMessages(ValidatorInterface $validator)
{
$messages = [];
if ($validator->getMin() > 0) {
$messages['minlength'] = sprintf($this->translateMessage('At least %s characters are required'), $validator->getMin());
$messages['minlength'] = sprintf($validator->getMessageTemplates()[\Zend\Validator\StringLength::TOO_SHORT], $validator->getMin());
}
if ($validator->getMax() > 0) {
$messages['maxlength'] = sprintf($this->translateMessage('At most %s characters are allowed'), $validator->getMax());
$messages['maxlength'] = sprintf($validator->getMessageTemplates()[\Zend\Validator\StringLength::TOO_LONG], $validator->getMax());
}

return $messages;
Expand Down
2 changes: 1 addition & 1 deletion src/StrokerForm/Renderer/JqueryValidate/Rule/Uri.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function getRules(ValidatorInterface $validator, ElementInterface $elemen
*/
public function getMessages(ValidatorInterface $validator)
{
return ['url' => $this->translateMessage('This is not a valid URI')];
return ['url' => $validator->getMessageTemplates()[\Zend\Validator\Uri::NOT_URI]];
}

/**
Expand Down