Skip to content
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

Error in JS validation for rule Form::Image #332

Closed
josefbk opened this issue May 9, 2024 · 0 comments
Closed

Error in JS validation for rule Form::Image #332

josefbk opened this issue May 9, 2024 · 0 comments

Comments

@josefbk
Copy link

josefbk commented May 9, 2024

Version: 3.2.3

Bug Description

A rule in attribute data-nette-rules generates an array (rule as Form::MimeType) or object (rule Form::Image) into arg. In the case of an object, the validation javascript then fails with Uncaught TypeError: arg.replace is not a function

args.forEach((arg) => re.push('^' + arg.replace(/([^\w])/g, '\\$1').replace('\\*', '.*') + '$'));

//Form::MimeType
data-nette-rules='[{"op":":fileSize","msg":"The size of the uploaded file can be up to 134217728 bytes.","arg":134217728},{"op":":mimeType","msg":"The File must be image in format JPEG, GIF, PNG or WebP.","arg":["image/gif","image/jpeg","image/png","image/webp","image/bmp"]}]'

//Form::Image
data-nette-rules='[{"op":":fileSize","msg":"The size of the uploaded file can be up to 134217728 bytes.","arg":134217728},{"op":":image","msg":"The uploaded file must be image in format JPEG, GIF, PNG or WebP.","arg":{"0":"image/gif","1":"image/jpeg","2":"image/png","3":"image/webp","5":"image/bmp"}}]'

Steps To Reproduce

composer.json

{
    "require": {
        "nette/forms": "3.2.3"
    }
}

index.php

<?php
require_once './vendor/autoload.php';
use Nette\Forms\Form;

$f = new Form();
// works as expected
$f->addUpload('test_mime')->addRule(Form::MimeType,'The File must be image in format JPEG, GIF, PNG or WebP.', ["image/gif","image/jpeg","image/png","image/webp","image/bmp"]);
//js throw: Uncaught TypeError: arg.replace is not a function
$f->addUpload('test_image')->addRule(Form::Image);
$f->addSubmit('submit','Submit');
?>
<!DOCTYPE html>
<head>
	<title>test</title>
	<script src="vendor/nette/forms/src/assets/netteForms.js"></script>
</head>
<body>
<?php
$f->render();
?>
</body>
</html>

Expected Behavior

The arg in both rules should be generated in the same way or netteForms.js should be able to handle the object.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants