nette / forms

📝 Generating, validating and processing secure forms in PHP. Handy API, fully customizable, server & client side validation and mature design.

Home Page:https://doc.nette.org/forms

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Error in JS validation for rule Form::Image

josefbk opened this issue · comments

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.