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

Validation rule Form::PATTERN for Nette\Http\FileUpload #130

Closed
h4kuna opened this issue Oct 19, 2017 · 5 comments
Closed

Validation rule Form::PATTERN for Nette\Http\FileUpload #130

h4kuna opened this issue Oct 19, 2017 · 5 comments

Comments

@h4kuna
Copy link

h4kuna commented Oct 19, 2017

  • bug report? yes
  • feature request? no
  • version: 2.4

Description

Hi,
i have use case

$form->addUpload('csv')
    ->addRule($form::MIME_TYPE, 'Foo', 'text/*')
    // next line does not work
    ->addRule($form::PATTERN, 'Message', '*.csv');

I found in Validator::validatePattern where first parameter is (IControl)->getValue(). It's ok. If i look at (FileUploadControl)->getValue() you get FileUpload and method __toString return tmpName. Where is problem? I don't need run validation on temporary name, but on original name.

For first validation check extension of file. Please you don't write, this valid is not relevant. I can't use PATTERN validation for this moment.

Or throw exception, this rule is not relevant for FileUpload. Because here is wtf behavior.

I understant if you change return value in method __toString(), it can be BC break or anything worse.

@h4kuna h4kuna changed the title Validation file extension by Nette\Http\FileUpload Validation rule Form::PATTERN for Nette\Http\FileUpload Oct 19, 2017
@dg
Copy link
Member

dg commented Feb 6, 2018

Feel free to make addUpload() and PATTERN work together. But for consistency you should write .*\.csv instead of *.csv, which can be very confusing.

@h4kuna
Copy link
Author

h4kuna commented Feb 14, 2018

I am sorry if my issue written aggressively.

Still does not work.

require __DIR__ . '/vendor/autoload.php';

$form = new \Nette\Forms\Form();
$form->addUpload('csv')
	->setRequired(false)
	// next line does not work
	->addRule($form::PATTERN, 'File is not csv.', '.*\.csv');

$form->addSubmit('foo');

$form->onSuccess[] = function ($form, $values) {
	dump($values);die();
};

echo $form;

save this like foo.csv and try upload

a,b,c
e,f,g

In content of Validator::validatePattern() is Strings::match($control->getValue(), "\x01^(?:$pattern)\\z\x01u"); Where $control->getValue() return FileUpload and this class has magic method __toString where return $this->tmpName instance of $this->name. Regular expression check this /tmp/phprSHBNS instance of foo.csv. In class String is strlen where is called __toString too.

If you change pattern for match tmp path, than upload is correct

->addRule($form::PATTERN, 'File is not csv.', '.tmp.*');

@dg
Copy link
Member

dg commented Feb 14, 2018

I understand that it doesn't work, as I said, feel free to fix it. But consider, that PATTERN is used for regular expressions, so it may be confusing to use it for wildcards.

@h4kuna
Copy link
Author

h4kuna commented Feb 23, 2018

Ok.
If i change return value for FileUpload::__toString(). Is this bad way, isn't it?

Second way, I can add condition to Validator::validatePattern, where check instance of FileUpload and call $control->getValue()->getName().

@h4kuna
Copy link
Author

h4kuna commented Feb 23, 2018

Pull request

@dg dg closed this as completed Feb 25, 2018
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