VirtueMart 2 > Product creation
How to make one of custom field REQUIRED?
fabelmik:
yea, sry bout my typos..
I meant cart.php and "reply #3"
Virtual:
Is main topic still actual for somebody?
Please send personal message with link to your site
Robert_ITMan:
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: --- <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 />
--- End code ---
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: ---ALTER TABLE `jos_virtuemart_customs` ADD `is_required` TINYINT( 1 ) NOT NULL DEFAULT '0' AFTER `is_hidden`
--- End code ---
File Edits
components/com_virtuemart/views/productdetails/tmpl/default_addtocart.php
templates\(my template)/html/com_virtuemart/productdetails/default_addtocart.php
--- Code: ---// 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/');
?>
--- End code ---
administrator/language/en-GB/en-GB.com_virtuemart.ini
--- Code: ---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
--- End code ---
administrator/components/com_virtuemart/views/custom/tmpl/default.php
--- Code: --- <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>
--- End code ---
administrator/components/com_virtuemart/views/custom/view.html.php
--- Code: --- 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
--- End code ---
administrator/components/com_virtuemart/tables/customs.php
--- Code: --- /** @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
--- End code ---
administrator/components/com_virtuemart/models/custom.php
--- Code: --- $this->setToggleName('is_hidden');
$this->setToggleName('is_required'); added by RM
--- End code ---
administrator/components/com_virtuemart/models/customfields.php
--- Code: --- $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 . '"';
--- End code ---
PolishedGeek:
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
Navigation
[0] Message Index
[*] Previous page
Go to full version