Skip to content

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

@josefbk

Description

@josefbk

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="/api/flow.js?q=https%3A%2F%2Fgithub.com%2Fnette%2Fforms%2Fissues%2Fvendor%2Fnette%2Fforms%2Fsrc%2Fassets%2FnetteForms.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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions