Skip to content

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

Closed

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="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.

Activity

  1. added a commit that references this issue on May 12, 2024
    0cfa5b2
  2. added 5 commits that reference this issue on Jun 3, 2024
    06c98c6
    f5c1a2d
    cc6fa37
    98fb2f4
    9f74f26
  3. added a commit that references this issue on Jun 11, 2024
    321e8f6
  4. added 10 commits that reference this issue on Aug 3, 2024
    47995c8
    22a191f
    212dd47
    2eb103d
    52d5f2f
    fb88274
    fd01839
    f7276ee
    451a8e9
    bcd18af
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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