Author Topic: How to make one of custom field REQUIRED?  (Read 19899 times)

spyderwebdesign

  • Jr. Member
  • **
  • Posts: 132
Re: How to make one of custom field REQUIRED?
« Reply #15 on: June 19, 2012, 22:34:14 pm »
On a side note I also changed the "Continue shopping" and "close" link in the modal popup to href="javascript:history.go(0)"   - so that you always go back the current page and refresh if you want to continue shopping instead of always ending up in the category browse..

How did you do this? What file? What code did you change and what code did you change it to?

Genius WebDesign

  • 3rd party VirtueMart Developer
  • Jr. Member
  • *
  • Posts: 175
    • Genius WebDesign
Re: How to make one of custom field REQUIRED?
« Reply #16 on: June 20, 2012, 00:46:16 am »
Regarding stopping the customer from adding the parent to the cart see post #3 http://forum.virtuemart.net/index.php?topic=100330.0

Regarding the "continue shopping" link you must edit the card.php file located in /components/com_virtuemart/controllers/
Just  change the href="' . $continue_link . '" to href="javascript:history.go(0)"    after    $this->json->msg =

Remember to make backup before you start editing!:)


spyderwebdesign

  • Jr. Member
  • **
  • Posts: 132
Re: How to make one of custom field REQUIRED?
« Reply #17 on: June 20, 2012, 02:27:31 am »
Regarding the "continue shopping" link you must edit the card.php file located in /components/com_virtuemart/controllers/

So this is odd. I am running VM 2.0.6 and I do not have a /components/com_virtuemart/controllers/card.php file...
Are you running the same version?

spyderwebdesign

  • Jr. Member
  • **
  • Posts: 132
Re: How to make one of custom field REQUIRED?
« Reply #18 on: June 20, 2012, 02:31:20 am »
I figured it out. You meant cart.php

spyderwebdesign

  • Jr. Member
  • **
  • Posts: 132
Re: How to make one of custom field REQUIRED?
« Reply #19 on: June 20, 2012, 02:32:59 am »
Regarding stopping the customer from adding the parent to the cart see post #3 http://forum.virtuemart.net/index.php?topic=100330.0

Did you mean reply #3 and not post #3? Post number 3 is

Quote
One word: GREAT!

One small suggestion: when trying to add the parent to the shopping cart, give a error message that the user has to choose!

Genius WebDesign

  • 3rd party VirtueMart Developer
  • Jr. Member
  • *
  • Posts: 175
    • Genius WebDesign
Re: How to make one of custom field REQUIRED?
« Reply #20 on: June 20, 2012, 03:14:34 am »
yea, sry bout my typos..
I meant cart.php and "reply #3"

Virtual

  • Beginner
  • *
  • Posts: 10
Re: How to make one of custom field REQUIRED?
« Reply #21 on: July 17, 2012, 08:40:27 am »
Is main topic still actual for somebody?
Please send personal message with link to your site

Robert_ITMan

  • Jr. Member
  • **
  • Posts: 114
    • websites developed and managed @ ourfingertips.com
  • VirtueMart Version: 3.2.12 and 3.2.15
Re: How to make one of custom field REQUIRED?
« Reply #22 on: April 01, 2013, 06:15:52 am »
HELP – we just need someone to get validation to work on the product page add to cart – similar to how it already works for the user tmpl files (user/edit.php and user/edit_address.php).
!? I’m not sure which file to add the formvalidation to, maybe:
(1) template default_addtocart.php as it has the form in it, but the form class is used to autoupdate the price – I tried this but my attempt at adding the validation code and validation to the add to cart button fails so far.
(2) template default_customfields.php is the safest place as it only affects the custom fields but it might not pass the needed info to the form in the cart.

!? I’m not sure how to pass the required=”required” to the $field->display – I see hints at this in the user/edit_address.php form where it uses jquery. So instead I found I can edit the vmcustom tmpl files, for example:

plugins/vmcustom/textinput/textinput/tmpl/default.php
Code: [Select]
<input class="<?php echo $class ?>" type="text" value="" size="<?php echo $this->params->custom_size ?>" name="customPlugin[<?php echo $viewData[0]->virtuemart_customfield_id ?>][<?php echo $this->_name?>][comment]" <?php if ( $this->is_required "1" ){ echo 'required="required"';} // added by RM - if statement doesn’t actually work – simply adding the required=”required” would work well enough for me here ?>><br />
NOTE: the following edits will add is_required similar to is_hidden to all VM2 Custom Fields so anyone can easily make these required or mandatory (we still need help getting the validation working, see above)

Database Edit:
Code: [Select]
ALTER TABLE `jos_virtuemart_customs` ADD `is_required` TINYINT( 1 ) NOT NULL DEFAULT '0' AFTER `is_hidden`
File Edits

components/com_virtuemart/views/productdetails/tmpl/default_addtocart.php
templates\(my template)/html/com_virtuemart/productdetails/default_addtocart.php
Code: [Select]
// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die('Restricted access');
// added by RM
// Implement Joomla's form validation
JHTML::_('behavior.formvalidation');
JHTML::stylesheet('vmpanels.css', JURI::root() . 'components/com_virtuemart/assets/css/');
?>
administrator/language/en-GB/en-GB.com_virtuemart.ini
Code: [Select]
COM_VIRTUEMART_CUSTOM_IS_HIDDEN="Hidden"
COM_VIRTUEMART_CUSTOM_IS_REQUIRED="Required" ; added by RM

COM_VIRTUEMART_TOGGLE_HIDDEN="Toggle Hidden"
COM_VIRTUEMART_TOGGLE_REQUIRED="Toggle Required" ; added by RM
administrator/components/com_virtuemart/views/custom/tmpl/default.php
Code: [Select]
<th><?php echo JText::_('COM_VIRTUEMART_CUSTOM_IS_HIDDEN'); ?></th>
<th><?php echo JText::_('COM_VIRTUEMART_CUSTOM_IS_REQUIRED'); // added by RM ?></th>

<td><a href="javascript:void(0);" onclick="return listItemTask('cb<?php echo $i;?>','toggle.is_hidden')" title="<?php echo ($custom->is_hidden ) ? JText::_('COM_VIRTUEMART_YES') : JText::_('COM_VIRTUEMART_NO');?>"><span class="vmicon <?php echo ( $custom->is_hidden  'vmicon-16-checkin' 'vmicon-16-bug' );?>"></span></a></td>
<?php // following <td>..</td> added by RM ; ?>
                <td><a href="javascript:void(0);" onclick="return listItemTask('cb<?php echo $i;?>','toggle.is_required')" title="<?php echo ($custom->is_required ) ? JText::_('COM_VIRTUEMART_YES') : JText::_('COM_VIRTUEMART_NO');?>"><span class="vmicon <?php echo ( $custom->is_required  'vmicon-16-checkin' 'vmicon-16-bug' );?>"></span></a></td>
administrator/components/com_virtuemart/views/custom/view.html.php
Code: [Select]
JToolBarHelper::custom('toggle.is_hidden.1', 'publish','', JText::_('COM_VIRTUEMART_TOGGLE_HIDDEN'), true);
JToolBarHelper::custom('toggle.is_hidden.0', 'unpublish','', JText::_('COM_VIRTUEMART_TOGGLE_HIDDEN'), true);
JToolBarHelper::custom('toggle.is_required.1', 'publish','', JText::_('COM_VIRTUEMART_TOGGLE_HIDDEN'), true); // added by RM
JToolBarHelper::custom('toggle.is_required.0', 'unpublish','', JText::_('COM_VIRTUEMART_TOGGLE_HIDDEN'), true); // added by RM
administrator/components/com_virtuemart/tables/customs.php
Code: [Select]
/** @var int(1)  1= hidden field info */
var $is_hidden = 0;
/** @var int(1)  1= required field info */
var $is_required = 0; // added by RM
administrator/components/com_virtuemart/models/custom.php
Code: [Select]
$this->setToggleName('is_hidden');
$this->setToggleName('is_required'); added by RM
administrator/components/com_virtuemart/models/customfields.php
Code: [Select]
$html .= VmHTML::row ('booleanlist', 'COM_VIRTUEMART_CUSTOM_IS_HIDDEN', 'is_hidden', $datas->is_hidden);
$html .= VmHTML::row ('booleanlist', 'COM_VIRTUEMART_CUSTOM_IS_REQUIRED', 'is_required', $datas->is_required); // added by RM

// in the following , `is_required` added after  , `is_hidden` // edited by RM
$query = 'SELECT C.`virtuemart_custom_id` , `custom_element`, `custom_jplugin_id`, `custom_params`, `custom_parent_id` , `admin_only` , `custom_title` , `custom_tip` , C.`custom_value` AS value, `custom_field_desc` , `field_type` , `is_list` , `is_cart_attribute` , `is_hidden` , `is_required` , C.`published` , field.`virtuemart_customfield_id` , field.`custom_value`,field.`custom_param`,field.`custom_price`,field.`ordering`
FROM `#__virtuemart_customs` AS C
LEFT JOIN `#__virtuemart_product_customfields` AS field ON C.`virtuemart_custom_id` = field.`virtuemart_custom_id`
Where `virtuemart_product_id` =' . $virtuemart_product_id . ' order by field.`ordering` ASC';

// in the following , `is_required` added after  , `is_hidden` // edited by RM
$query = 'SELECT C.`virtuemart_custom_id` , `custom_element`, `custom_params`, `custom_parent_id` , `admin_only` , `custom_title` , `custom_tip` , C.`custom_value` AS value, `custom_field_desc` , `field_type` , `is_list` , `is_hidden` , `is_required` , `layout_pos`, C.`published` , field.`virtuemart_customfield_id` , field.`custom_value`, field.`custom_param`, field.`custom_price`, field.`ordering`
FROM `#__virtuemart_customs` AS C
LEFT JOIN `#__virtuemart_product_customfields` AS field ON C.`virtuemart_custom_id` = field.`virtuemart_custom_id`
Where `virtuemart_product_id` =' . (int)$product->virtuemart_product_id . ' and `field_type` != "G" and `field_type` != "R" and `field_type` != "Z"';

// in the following , `is_required` added after  , `is_hidden` // edited by RM
$query = 'SELECT C.`virtuemart_custom_id` , `custom_parent_id` , `admin_only` , `custom_title` , `custom_tip` , C.`custom_value` AS value, `custom_field_desc` , `field_type` , `is_list` , `is_hidden` , `is_required` , C.`published` , field.`virtuemart_customfield_id` , field.`custom_value`, field.`custom_param`, field.`custom_price`, field.`ordering`
FROM `#__virtuemart_customs` AS C
LEFT JOIN `#__virtuemart_product_customfields` AS field ON C.`virtuemart_custom_id` = field.`virtuemart_custom_id`
Where `virtuemart_product_id` =' . (int)$product->virtuemart_product_id . ' and `field_type` = "Z"';

// in the following , `is_required` added after  , `is_hidden` // edited by RM
$query = 'SELECT C.`virtuemart_custom_id` , `custom_parent_id` , `admin_only` , `custom_title` , `custom_tip` , C.`custom_value` AS value, `custom_field_desc` , `field_type` , `is_list` , `is_hidden` , `is_required` , C.`published` , field.`virtuemart_customfield_id` , field.`custom_value`, field.`custom_param`, field.`custom_price`, field.`ordering`
FROM `#__virtuemart_customs` AS C
LEFT JOIN `#__virtuemart_product_customfields` AS field ON C.`virtuemart_custom_id` = field.`virtuemart_custom_id`
Where `virtuemart_product_id` =' . (int)$product->virtuemart_product_id . ' and `field_type` = "R"';

// group by virtuemart_custom_id
// in the following , `is_required` added after  , `is_hidden` // edited by RM
$query = 'SELECT C.`virtuemart_custom_id`, `custom_title`, C.`custom_value`,`custom_field_desc` ,`custom_tip`,`field_type`,field.`virtuemart_customfield_id`,`is_hidden` , `is_required`
FROM `#__virtuemart_customs` AS C
LEFT JOIN `#__virtuemart_product_customfields` AS field ON C.`virtuemart_custom_id` = field.`virtuemart_custom_id`
Where `virtuemart_product_id` =' . (int)$product->virtuemart_product_id . ' and `field_type` != "G" and `field_type` != "R" and `field_type` != "Z"';

$db = JFactory::getDBO ();
// in the following , `is_required` added after  , `is_hidden` // edited by RM
$query = 'SELECT C.`virtuemart_custom_id` , `custom_element` , `custom_parent_id` , `admin_only` , `custom_title` , `custom_tip` ,
C.`custom_value` AS value, `custom_field_desc` , `field_type` , `is_list` , `is_cart_attribute` , `is_hidden` , `is_required` , C.`published` ,
field.`virtuemart_customfield_id` , field.`custom_value`,field.`custom_param`,field.`custom_price`
FROM `#__virtuemart_customs` AS C
LEFT JOIN `#__virtuemart_product_customfields` AS field ON C.`virtuemart_custom_id` = field.`virtuemart_custom_id`
WHERE `virtuemart_customfield_id` ="' . (int)$selected . '"';
www.ourfingertips.com - websites developed and managed @ ourfingertips

Let me help you make the most of the technology at our fingertips

PolishedGeek

  • 3rd party VirtueMart Developer
  • Jr. Member
  • *
  • Posts: 263
  • Joomla! Innovation Refined.
    • Polished Geek, LLC - Joomla! extensions, web development & custom integrations
Re: How to make one of custom field REQUIRED?
« Reply #23 on: April 09, 2013, 01:51:03 am »
For those who would like to do required fields without hacks or code modifications, there is an extension that will let you do this: http://extensions.virtuemart.net/products/breezing-forms-custom-fields-plugin-detail
~ Deb Cinkus, CEO
Polished Geek, LLC   |    www.PolishedGeek.com

Creators of JoomLister - eBay Lister for Joomla!

treodude

  • Jr. Member
  • **
  • Posts: 57
Re: How to make one of custom field REQUIRED?
« Reply #24 on: July 17, 2013, 14:40:28 pm »
Seriously, why is there no good solution that?

One solution is only for dropdown selections and the other one is €50 for something way too simple and small.

Virtuemart, make this a default option!

HenrikS

  • Beginner
  • *
  • Posts: 20
Re: How to make one of custom field REQUIRED?
« Reply #25 on: August 01, 2013, 19:30:01 pm »
Yes why is there no good solution to this yet .. almost all shops use it?

And how come devs. dont answer on this topic.

Robert_ITMan

  • Jr. Member
  • **
  • Posts: 114
    • websites developed and managed @ ourfingertips.com
  • VirtueMart Version: 3.2.12 and 3.2.15
Re: How to make one of custom field REQUIRED?
« Reply #26 on: November 21, 2013, 16:36:43 pm »
To follow up on this I posted a new topic 'VM2 Custom Fields Integration - IT WORKS!' here:
http://forum.virtuemart.net/index.php?topic=120445.0
www.ourfingertips.com - websites developed and managed @ ourfingertips

Let me help you make the most of the technology at our fingertips

artist.trade

  • Beginner
  • *
  • Posts: 1
Re: How to make one of custom field REQUIRED?
« Reply #27 on: November 23, 2013, 21:03:37 pm »
Hey, here is my simple solution - it marks the border color of mandatory fields and disable/enable send button in product details. To mark field as mandatory, simply add three dots at the end of text for index 0. Like "Please choose ...".

Just copy code below into line 29 (above <div class="addtocart-area">) inside \components\com_virtuemart\views\productdetails\tmpl\default_addtocart.php.

Code: [Select]
<script type='text/javascript'>
  // On window load start function
  window.onload = initForm;
  window.onunload = function() {};
 
  function initForm() {
    // Last characters of first index in selection box define mandatory status.
    var mandatory = "...";
    var elements = document.getElementsByClassName('product-field-display');
   
    // Find mandatory fields
    if (elements.length > 0) {
      var j = 0;
      var ids = new Array();
      for (var i = 0; i < elements.length; i++) {
        var text = elements[i].innerHTML;
        var x = text.search("customPrice");
        var y = text.search("name=");
        var id = text.substr(x,y - x - 2);
        var index = document.getElementById(id).options[0].text;
        if (index.substr(index.length - mandatory.length,mandatory.length) == mandatory) {
          ids[j] = id;
          j++;
        }
      }
     
      // Set the color of mandatory fields
      var j = 0;
      for (var i = 0; i < ids.length; i++) {
       
        document.getElementById(ids[i]).setAttribute("onchange", "initForm()");
       
        if (document.getElementById(ids[i]).selectedIndex == 0) {
          document.getElementById(ids[i]).style.borderColor="#FF0000";
          j++;
        } else {
          document.getElementById(ids[i]).style.borderColor="#AAAAAA";
        }
      }
     
      // Disable or enable send button
      if (j != 0) {
        document.getElementsByName("addtocart")[0].disabled = true;
      } else {
        document.getElementsByName("addtocart")[0].disabled = false;
      }
    }
  }
</script>

frighten

  • Beginner
  • *
  • Posts: 5
Re: How to make one of custom field REQUIRED?
« Reply #28 on: February 12, 2014, 00:25:16 am »
Hello artist.trade
Your solution seem to be interesting. Did it work? I added the code and then created some custom fields and added the dots as you described.
Nothing was changed. Maybe I made something wrong in the creation of the custom fields itself...
Is there a proper way to create the fields to make it work with your code?
My main goal is to get one dropdown menu just above the "Add to cart" button. By default it will say "Please Choose..." and then the customer has to choose one of three following options in the dropdown. If they don't choose anything they can not add to cart, or be prompted to add to cart.

Using VM 2.0.26d (with Joomla 2.5.18)

klattr1

  • Jr. Member
  • **
  • Posts: 80
Re: How to make one of custom field REQUIRED?
« Reply #29 on: April 05, 2014, 12:21:30 pm »
This should be a standard feature in VM 2. It is used by so many other eCommerce platforms and prevents users from choosing incorrect cart variant options accidentally.

Mods, pleas add this functionality to the core code.