- Posts: 63
COMMUNITY FORUM
[SOLVED] image upload and extra fields required
- Josh
-
Topic Author
- Offline
- Senior Member
I am running a site with a few sub templates and was wondering if there is any way we are able to make to image upload feature required? AIso is this possible for extra fields too? Or even selected extra fields?
I have an events category and would like to force users to upload an image for their submissions(via frontend) and also to fill in the extra fields by default. Is this possible?
Many thanks!
Please Log in or Create an account to join the conversation.
- Krikor Boghossian
-
- Offline
- Platinum Member
- Posts: 15920
For the extrafields it's quite simple. They have a check for being required or not. You can choose which are and which not.
For front end editing you should override the k2 JS scripts and itemform.php to add the functionality you want. Maybe a simple JS script that disables the submit will do the trick.
Please Log in or Create an account to join the conversation.
- Josh
-
Topic Author
- Offline
- Senior Member
- Posts: 63
Thanks for the reply - now that you mention it I have actually seen the extra fields required button, just had a blind moment.. Thanks for the JS suggestion - will check it out and revert should I find a solution. :)
Please Log in or Create an account to join the conversation.
- Krikor Boghossian
-
- Offline
- Platinum Member
- Posts: 15920
Please Log in or Create an account to join the conversation.
- Josh
-
Topic Author
- Offline
- Senior Member
- Posts: 63
Thanks for that - ok for the image required function I am having problems..
In itemform.php i found
defined('_JEXEC') or die;
$document = JFactory::getDocument();
$document->addScriptDeclaration("
Joomla.submitbutton = function(pressbutton){
if (pressbutton == 'cancel') {
submitform( pressbutton );
return;
}
if (\$K2.trim(\$K2('#title').val()) == '') {
alert( '".JText::_('K2_ITEM_MUST_HAVE_A_TITLE', true)."' );
}
else if (\$K2.trim(\$K2('#catid').val()) == '0') {
alert( '".JText::_('K2_PLEASE_SELECT_A_CATEGORY', true)."' );
}
else {
syncExtraFieldsEditor();
var validation = validateExtraFields();
if(validation === true) {
\$K2('#selectedTags option').attr('selected', 'selected');
submitform( pressbutton );
}
}
}
");
I thought this might help however when I try to find the image reference in the database there isnt any due to the way k2 handles images.
How would I be able to implement a check to see if the input has a value selected and only if it does to allow the submit button to work?
something like..
if ($_FILES === 0)
Sorry for asking but JS is not my forte so any assistance / help would be greatly appreciated
Please Log in or Create an account to join the conversation.
- Krikor Boghossian
-
- Offline
- Platinum Member
- Posts: 15920
\$K2.trim(\$K2('#catid').val()) == '0')
you need to duplicate this piece of code and change the #catid to the input id or class you want. the .fileUpload is the upload class and the #existingImageValue is the id of the existing image input
Please Log in or Create an account to join the conversation.
- Josh
-
Topic Author
- Offline
- Senior Member
- Posts: 63
Thanks for the reply again - apologies for the late reply.
Ok yes I though something like that, ok so Ive ammended it to be
<?php
/**
* @version $Id: itemform.php 1959 2013-04-07 19:06:19Z joomlaworks $
* @package K2
* @author JoomlaWorks www.joomlaworks.net
* @copyright Copyright (c) 2006 - 2013 JoomlaWorks Ltd. All rights reserved.
* @license GNU/GPL license: www.gnu.org/copyleft/gpl.html
*/
// no direct access
defined('_JEXEC') or die;
$document = JFactory::getDocument();
$document->addScriptDeclaration("
Joomla.submitbutton = function(pressbutton){
if (pressbutton == 'cancel') {
submitform( pressbutton );
return;
}
if (\$K2.trim(\$K2('#title').val()) == '') {
alert( '".JText::_('K2_ITEM_MUST_HAVE_A_TITLE', true)."' );
}
else if (\$K2.trim(\$K2('#catid').val()) == '0') {
alert( '".JText::_('K2_PLEASE_SELECT_A_CATEGORY', true)."' );
}
else if (\$K2.trim(\$K2('#.fileUpload').val()) == '0') {
alert( '".JText::_('K2_PLEASE_SELECT_AN_IMAGE', true)."' );
}
else {
syncExtraFieldsEditor();
var validation = validateExtraFields();
if(validation === true) {
\$K2('#selectedTags option').attr('selected', 'selected');
submitform( pressbutton );
}
}
}
");
?>
The problem is that when I hit save that the form kicks the errors correctly for the title and category fields but seems to not be checking/validating the image upload input. Do you know if there is another place I need to invoke the check/validation? Also I am wondering if it should validate that an image has been already uploaded but rather that it should check that an image has been selected on save and if yes then continue checks and allow save.
Thanks again for any assistance. I think this could be a nice feature others might be interested in too.
Cheers
Josh
Please Log in or Create an account to join the conversation.
- Krikor Boghossian
-
- Offline
- Platinum Member
- Posts: 15920
You can combine the checks for the input using an && element.
Let me know if that did the trick :)
Please Log in or Create an account to join the conversation.
- Josh
-
Topic Author
- Offline
- Senior Member
- Posts: 63
Ok no luck so far - I changed to .fileUpload but still it allows me to save and no error when no image is added. I am not sure exactly what sort of validation I should be researching.
Thanks again I really appreciate it!
Please Log in or Create an account to join the conversation.
- Krikor Boghossian
-
- Offline
- Platinum Member
- Posts: 15920
Always glad to help.
There is a related post on Stack Overflow that will help you out:
stackoverflow.com/questions/14814075/jquery-validate-file-inputs
Please Log in or Create an account to join the conversation.
- Josh
-
Topic Author
- Offline
- Senior Member
- Posts: 63
Please Log in or Create an account to join the conversation.
- Josh
-
Topic Author
- Offline
- Senior Member
- Posts: 63
<?php
/**
* @version $Id: itemform.php 1959 2013-04-07 19:06:19Z joomlaworks $
* @package K2
* @author JoomlaWorks www.joomlaworks.net
* @copyright Copyright (c) 2006 - 2013 JoomlaWorks Ltd. All rights reserved.
* @license GNU/GPL license: www.gnu.org/copyleft/gpl.html
*/
// no direct access
defined('_JEXEC') or die;
$document = JFactory::getDocument();
$document->addScriptDeclaration("
Joomla.submitbutton = function(pressbutton){
if (pressbutton == 'cancel') {
submitform( pressbutton );
return;
}
if (\$K2.trim(\$K2('#title').val()) == '') {
alert( '".JText::_('K2_ITEM_MUST_HAVE_A_TITLE', true)."' );
}
else if (\$K2.trim(\$K2('#catid').val()) == '0') {
alert( '".JText::_('K2_PLEASE_SELECT_A_CATEGORY', true)."' );
}
else if (\$K2.trim(\$K2('.fileUpload').val()) =='0' {
alert( '".JText::_('select and image', true)."' );
return false;
}
else {
syncExtraFieldsEditor();
var validation = validateExtraFields();
if(validation === true) {
\$K2('#selectedTags option').attr('selected', 'selected');
submitform( pressbutton );
}
}
}
");
?>
Still no go tho..
Any assistance or reference would be greatly appreciated!
Please Log in or Create an account to join the conversation.
- Krikor Boghossian
-
- Offline
- Platinum Member
- Posts: 15920
var files = \$K2('.fileUpload').val();
if(files=='')
{
alert('No image selected');
return false;
} else {
// Do stuff
}
Did you try something like this?
Please Log in or Create an account to join the conversation.
- Josh
-
Topic Author
- Offline
- Senior Member
- Posts: 63
My apologies for the delay - been a bit swamped so this had to take the backburner for a few days.
Thanks a stack for the code above - I tried it however it still allows me to save and doesnt prompt me to add an image before being allowed to save.
I tried this..
/**
* @version $Id: itemform.php 1279 2011-10-27 16:52:58Z joomlaworks $
* @package K2
* @author JoomlaWorks www.joomlaworks.gr
* @copyright Copyright (c) 2006 - 2011 JoomlaWorks Ltd. All rights reserved.
* @license GNU/GPL license: www.gnu.org/copyleft/gpl.html
*/
// no direct access
defined('_JEXEC') or die('Restricted access');
$document = & JFactory::getDocument();
$document->addScriptDeclaration("
Joomla.submitbutton = function(pressbutton){
if (pressbutton == 'cancel') {
submitform( pressbutton );
return;
}
if (\$K2.trim(\$K2('#title').val()) == '') {
alert( '".JText::_('K2_ITEM_MUST_HAVE_A_TITLE', true)."' );
}
else if (\$K2.trim(\$K2('#catid').val()) == '0') {
alert( '".JText::_('K2_PLEASE_SELECT_A_CATEGORY', true)."' );
}
var files = \$K2('.fileUpload').val();
if(files=='')
{
alert('No image selected');
return false;
}
else {
syncExtraFieldsEditor();
\$K2('#selectedTags option').attr('selected', 'selected');
submitform( pressbutton );
}
}
");
Thanks a stack mate - scripting isnt really my strongpoint :)
Please Log in or Create an account to join the conversation.
- Krikor Boghossian
-
- Offline
- Platinum Member
- Posts: 15920
$document = & JFactory::getDocument();
$document->addScriptDeclaration("
Joomla.submitbutton = function(pressbutton){
if (pressbutton == 'cancel') {
submitform( pressbutton );
return;
}
if (\$K2.trim(\$K2('#title').val()) == '') {
alert( '".JText::_('K2_ITEM_MUST_HAVE_A_TITLE', true)."' );
}
else if (\$K2.trim(\$K2('#catid').val()) == '0') {
alert( '".JText::_('K2_PLEASE_SELECT_A_CATEGORY', true)."' );
}
else if( \$K2('.fileUpload').val() == '')
{
alert('No image selected');
return false;
}
else {
syncExtraFieldsEditor();
\$K2('#selectedTags option').attr('selected', 'selected');
submitform( pressbutton );
}
}
");
This forces the user to upload a new item each time. tested it and works.
$document = & JFactory::getDocument();
$document->addScriptDeclaration("
Joomla.submitbutton = function(pressbutton){
if (pressbutton == 'cancel') {
submitform( pressbutton );
return;
}
if (\$K2.trim(\$K2('#title').val()) == '') {
alert( '".JText::_('K2_ITEM_MUST_HAVE_A_TITLE', true)."' );
}
else if (\$K2.trim(\$K2('#catid').val()) == '0') {
alert( '".JText::_('K2_PLEASE_SELECT_A_CATEGORY', true)."' );
}
else if( \$K2('.fileUpload').val() == '' && \$K2('#existingImageValue').val() == '' )
{
alert('No image selected');
return false;
}
else {
syncExtraFieldsEditor();
\$K2('#selectedTags option').attr('selected', 'selected');
submitform( pressbutton );
}
}
");
This will check both the new AND existing image fields.
Please note this ONLY for adding new item, You should make a case where the default code is used when the $task is editing.
Please Log in or Create an account to join the conversation.
- Josh
-
Topic Author
- Offline
- Senior Member
- Posts: 63
Thanks for that mate! I tried it as below but I must be missing something - tell me, this is for adding new article via the frontend right?
Please Log in or Create an account to join the conversation.
- Jared
-
- Offline
- New Member
- Posts: 5
I am trying to do this too and it doesn't seem to work. I am not sure why it doesn't validate. Is there an update on this?
Thanks!
Jared
Please Log in or Create an account to join the conversation.
- Jared
-
- Offline
- New Member
- Posts: 5
Thanks for your help on this Krikor!
Please Log in or Create an account to join the conversation.
- Jared
-
- Offline
- New Member
- Posts: 5
Thank you for this code. Implemented it and it works fine. However, I just realized that it requires the user to upload an image again when they are editing the post. Can you please point me in a direction to resolve this? I see that you say we should create a case when $task is being used. Would you be able to elaborate on that more? Your assistance is greatly appreciated.
Thank You!
Jared
Please Log in or Create an account to join the conversation.