|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
iarfhlaith Smarty Rookie
Joined: 20 Dec 2006 Posts: 13 Location: Dublin, Ireland
|
Posted: Wed Dec 20, 2006 3:39 pm Post subject: smartyValidate - Image Validation issue for isFileType |
|
|
Hi All,
Long time reader, first time writer.
This is a great forum and it has helped me hugely in the past. But I have come up against a brick wall with this one so I've decided to post it up here.
I have a straightforward form that needs to validate an uploaded image by its file type. (file extension validation will do fine).
I have looked for examples within this forum and within the smartyValidate documentation but I havn't found an example of what I need.
Here's the relevant code snippets:
PHP Code:
Code: |
if(empty($_POST)) {
SmartyValidate::connect($smarty, true);
SmartyValidate::register_validator('msgNameBlank' , 'txtName' , 'notEmpty');
SmartyValidate::register_validator('msgNameLength' , 'txtName:-1:45' , 'isLength');
SmartyValidate::register_validator('msgMessage' , 'txtMessage:1:160' , 'isLength');
SmartyValidate::register_validator('msgImageType' , 'txtImage:jpg,gif,png' , 'isFileType', true);
}
else {
SmartyValidate::connect($smarty);
if(SmartyValidate::is_valid($_POST)) {
SmartyValidate::disconnect();
// insert into database...
// display message to user...
}
}
|
TPL Code:
Code: |
<form enctype="multipart/form-data" id='form' name='addPixCodes' action='/add.php' method='post'>
<input type="hidden" name="MAX_FILE_SIZE" value="5242880" /> <!-- 5 MegaBytes -->
<table>
<tr>
<td class='labelcell'>
<label for="txtName">Name <span class='required'>*</span></label></td>
<td class='fieldcell'>
<input type="text" name="txtName" id="name" tabindex="1" value="{$txtName|escape}" />
<div class='appNote'><span class='warning'>{validate id="msgNameBlank" message="PixCode Name Cannot Be Empty"}</span></div>
<div class='appNote'><span class='warning'>{validate id="msgNameLength" message="Max Length is 45 Characters"}</span></div>
</td>
</tr>
<tr>
<td class='labelcell'><label for="txtMessage">Text <span class='required'>*</span></label></td>
<td class='fieldcell'>
<textarea name="txtMessage" id="message" tabindex="2" onKeyDown="return textBlocker(this, 160);" onKeyuUp="return textBlocker(this, 160);" >{$txtMessage|escape}</textarea>
<div class='appNote'><span class='warning'>{validate id="msgMessage" message="Text Must Be Between 1 and 160 Chars"}</span></div>
<div class='appNote'>
Max Length: 160
</div>
</td>
</tr>
<tr>
<td class='labelcell'><label for="txtImage">Image:</label></td>
<td class='fieldcell'>
<input type="file" name="txtImage" id="file" tabindex="3" size="33" />
<div class='appNote'><span class='warning'>{validate field="file:txtImage" id="msgImageType" message="File must be either JPG, GIF or PNG"}</span></div>
</td>
</tr>
<tr>
<td class='labelcell'><label for="txtStatus">Status</label></td>
<td class='fieldcell'>
{html_radios name="txtStatus" options=$statusOptions selected=$txtStatus style="width:50px;" tabindex="4"}
</td>
</tr>
<tr>
<td colspan="2" align='center'>
<br /><br />
<input type="submit" name="txtSubmit" value="Create PixCode" tabindex="5" class='button' />
<br /><br />
</td>
</tr>
</table>
</form>
|
I have been careful to include the enctype value within the form tag.
I have not defined the form name within smartyValidate as the default is working just fine.
I have found a post with a similar issue here:
http://www.phpinsider.com/smarty-forum/viewtopic.php?t=5785
but the syntax and semantics of the way the plugin is being used seems different here. In this post the validation criteria is specified in the template inside the {validate} tag. I'm doing it slightly differently and using register_validator() instead.
My code works for all of the other fields in the form and the validation works well for them.
Could anyone provide me with an example of how to test a filetype using the register_validator() method?
I have a sneaky suspician that it has to do with inserting 'file:' in front of something but I'm not sure what. Possibly the id field in the {validate} tag?
My main problem is that the uploaded file is validating regardless of what filetype is submitted.
Any help on this greatly apprciated,
regards,
Iarfhlaith |
|
Back to top |
|
iarfhlaith Smarty Rookie
Joined: 20 Dec 2006 Posts: 13 Location: Dublin, Ireland
|
Posted: Wed Dec 20, 2006 6:44 pm Post subject: $_SESSION and $_FILES content |
|
|
To help make things a little clearer:
Here's the contents of my SmartyValidate session:
Code: |
Array
(
[default] => Array
(
[registered_funcs] => Array
(
[criteria] => Array
(
)
[transform] => Array
(
)
)
[validators] => Array
(
[0] => Array
(
[field] => txtName
[id] => msgNameBlank
[criteria] => notEmpty
[message] => PixCode Name Cannot Be Empty
[trim] =>
[empty] =>
[halt] =>
[transform] =>
[valid] => 1
)
[1] => Array
(
[field] => txtName
[field2] => -1
[field3] => 45
[id] => msgNameLength
[criteria] => isLength
[message] => Max Length is 45 Characters
[trim] =>
[empty] =>
[halt] =>
[transform] =>
[valid] => 1
)
[2] => Array
(
[field] => txtMessage
[field2] => 1
[field3] => 160
[id] => msgMessage
[criteria] => isLength
[message] => Text Must Be Between 1 and 160 Chars
[trim] =>
[empty] =>
[halt] =>
[transform] =>
[valid] => 1
)
[3] => Array
(
[field] => file:txtImage
[field2] => jpg,gif,png
[id] => msgImageType
[criteria] => isFileType
[message] => File must be either JPG, GIF or PNG
[trim] =>
[empty] => 1
[halt] =>
[transform] =>
[valid] => 1
)
)
[is_error] =>
[is_init] =>
)
)
|
And a dump of the $_FILES super global:
Code: |
Array
(
[txtImage] => Array
(
[name] => latest.txt
[type] => text/plain
[tmp_name] => C:\PHP\uploadtemp\php202.tmp
[error] => 0
[size] => 171
)
)
|
An Finally just if you're interested, here's the $_POST dump:
Code: |
Array
(
[MAX_FILE_SIZE] => 5242880
[txtName] => This is the title
[txtMessage] => This is the message contents
[txtStatus] => 0
[txtSubmit] => Create
)
|
In this example I have attempted to upload a text file which when validated should fail because it has an extension of 'txt' and that is not listed as an accepted file extension in the register_validator() function.
However, there is something unexpected happening and the txtImage field is never validated (or it is validated and it returns valid in error).
Any ideas anyone? |
|
Back to top |
|
TGKnIght Smarty Junkie
Joined: 07 Sep 2005 Posts: 580 Location: Philadelphia, PA
|
|
Back to top |
|
iarfhlaith Smarty Rookie
Joined: 20 Dec 2006 Posts: 13 Location: Dublin, Ireland
|
Posted: Thu Dec 21, 2006 1:20 am Post subject: |
|
|
Hi TGKnIght,
Thanks for your interest. I've read many of your reply's within this forum and they're always helpful. Keep up the good work!
As far as I know, 'file:' is used to identify content contained within the $_FILES global as opposed to the $_POST global.
I was unsure whether to use it or not as it's not mentioned in the smartyValidate documentation, however I've tested the code with it and without it. Neither of them work.
The reason I tried it is because there is a reference to the use of 'file:' in this topic:
http://phpinsider.com/smarty-forum/viewtopic.php?t=3540
Examples of how to use the isFileType and isFileSize functions are few and far between and I tried this method in the hope that I would have more success. Though none as of yet.
Overall, and as far as I can tell I'm having a similar problem to the one outlined here: http://www.phpinsider.com/smarty-forum/viewtopic.php?t=7858&
But in my case I'm not trying to display form values after they have been submitted. So i don't know how I would have an undefined index.
Here are the error notices I'm getting:
Code: |
Notice: Undefined index: txtImage in C:\PHP\includes\smarty\libs\SmartyValidate.class.php on line 206
Notice: Undefined index: txtImage in C:\PHP\includes\smarty\libs\SmartyValidate.class.php on line 206
Notice: Undefined index: txtImage in C:\PHP\includes\smarty\libs\SmartyValidate.class.php on line 221
Notice: Undefined index: txtImage in C:\PHP\includes\smarty\libs\SmartyValidate.class.php on line 235
|
I'm surprised I'm getting these as I'm not deliberately using $txtImage in the template after a form submission. |
|
Back to top |
|
TGKnIght Smarty Junkie
Joined: 07 Sep 2005 Posts: 580 Location: Philadelphia, PA
|
|
Back to top |
|
iarfhlaith Smarty Rookie
Joined: 20 Dec 2006 Posts: 13 Location: Dublin, Ireland
|
Posted: Fri Dec 22, 2006 1:27 am Post subject: |
|
|
I've made some progress.
I removed the 'file:' code and now the isFileType works as expected. However I am still receiving the error notices.
I have traced it down to this: The Smarty Validate isValid() function is outputting the error notices because it is looking for the field txtImage within the $_POST super global but can't find it. (txtImage is kept in the $_FILES super global as it's of type file).
So, I'm not sure what to do now; is it a bug in the isValid() function that it always looks for registered fields in $_POST regardless of where it actually is?
I've managed to get around this issue by changing the error level in my php.ini file; it will no longer display error notices. This is a poor mans work around though and I'd really like to solve it properly.
Has anyone used the isFileType or isFileSize plugins without getting the error notices? It might just be a bug in the core validation code...
As an aside, I wrote a new plugin called isMimeType. This plugin works in much the same way as isFileType but checks the actual MIME type of the file as opposed to it's file extension. I have provided it's source and usage details in this post: http://phpinsider.com/smarty-forum/viewtopic.php?t=9911. I hope it comes in handy for someone else. |
|
Back to top |
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|
|