15 replies [Last post]
Wonder95's picture
Offline
Joined: 02/20/2008
Juice: 283
Was this information Helpful?

This may seem to be a picky thing, but in setting up some products tonight for a new site, I noticed something sort of unnecessary. If I have an attribute with multiple options, and I enable some of the those options, if I don't set a default option I get this message:

All attributes with enabled options must specify an enabled option as default.

This is all fine and good if the attribute is not set as required, and the default option is what is initially displayed in the select list (or radio button). However, if the attribute is set as required, then the default option is not even used. If the attribute is displayed as a select list (incorrectly labeled as "select box", by the way), the default value is an additional option of "Please select", and if it is displayed as a set of radio buttons, the default is not selected at all.

It seems to me there are three options to fix this:

  1. Get rid of the additional "Please select" option when an required attribute is displayed in a select list and display the default value (the red * next to the field label is usually a good indicator that the field is required), or have the default value selected when displayed as radio buttons
  2. Make it so that the default value is not required when an attribute is set as required (it has already been set by the time the options are selected)
  3. Don't make a default value required at all.

My personal preference is for a combination of 1 and 3. In other words, get rid of the "Please select" option when an attribute is required, but still show the default value if selected. However, don't make setting a default value required in the first place.

Granted, this isn't anything huge, but it was confusing for me and cost me some time tonight trying to figure why a default values was required but then not used.

Thanks.

Ryan's picture
Offline
Joined: 08/07/2007
Juice: 15459
Re: Required default value ignored for required attribute

I think your summary is thorough and ends with good recommendations. I'm going to flag this post to revisit after the update to Drupal 6. I think the main problem we had w/ the defaults was it was a radio select group so there was no way for someone to clear a default option if they didn't want it any longer. Do you have a suggestion for the UI on that?

Wonder95's picture
Offline
Joined: 02/20/2008
Juice: 283
Check to see if field is required

At the time the page is displayed, you know whether or not a field is set as required (you can query the db in your form function). What I would do is to check the required status of the field if it is a radio button set; if it is required, select the default (by passing the value to the #default_value form control in the form function), and if it isn't required, don't pass it.

Ryan's picture
Offline
Joined: 08/07/2007
Juice: 15459
Re: Check to see if field is required

I think you're talking about the actual add to cart form display there. Puzzled What do you propose for the administration side, where you have to specify the default value for each attribute. i.e. What if someone had set it in the past but wanted to unset the default but still keep the attribute required? This wouldn't work currently.

Wonder95's picture
Offline
Joined: 02/20/2008
Juice: 283
Clearing Default settings

Oops, sorry, I misunderstood. I see now what you're asking.

Hmmm, good question. Well, there are two options I can think of:

  1. Instead of having a radio button for each option, you could have a select list where you would specify the default value, and the list would have an option of "No default" (or "None", or whatever you want there).
  2. A checkbox at the top of the form that would automatically clear the Default radio button settings (similar to what you see on webmail apps for selecting all items in a list). This could be either an AJAX thing, or something that happens on page submission; when you submit the page, the default setting for the options is deleted from the database so that when the page is displayed the next time, the Default setting is gone.

My preference would be #2, since that's the cleaner way to do it.

If you want to point me in the direction of where the code is that handles this, I'd be happy to work on modifying it myself and submitting a patch.

splash112@drupal.org's picture
Offline
Joined: 03/31/2008
Juice: 413
Re: Clearing Default settings

Thanks for pointing these out. I was breaking my head over it.
Never would have thought I should deselect required when I want to get rid of the "please select option", because an option is required anyway.

I would completely ignore the required or not setting. An option is always required in a select list. Users who would want to make one "nothing" option should create one as an option.

Thanks
Mark

mike-green's picture
Offline
Joined: 10/01/2008
Juice: 8
Re: Required default value ignored for required attribute

Here is a code to remove the unwanted "Please select" option and set up the default attribute.
This is made for attributes that are user as select boxes:

<?php
function yourmodule_form_alter($form_id, &$form) {
   
   
// override product form
   
if (strpos($form_id, 'add_to_cart_form')) {
       
       
// setup default attributes
       
foreach ($form['attributes'] as $key=>$atr) {
            if (
is_array($atr)) {
               
// remove unwanted t('Please select')
               
$opt=array();
                foreach(
$atr['#options'] as $opt_key=>$opt_val) {
                    if (
$opt_val != t('Please select')) {
                       
$opt[$opt_key] = $opt_val;
                    }
                }
               
$form['attributes'][$key]['#options'] = $opt;
               
// setup default attribute
       
$attribute = db_fetch_object(db_query("SELECT a.aid, a.name, a.ordering AS default_ordering, a.required AS default_required, a.display AS default_display, a.description, pa.default_option, pa.required, pa.ordering, pa.display FROM {uc_attributes} AS a LEFT JOIN {uc_product_attributes} AS pa ON a.aid = pa.aid AND pa.nid = %d WHERE a.aid = %d", $form['nid']['#value'], $key));
               
$form['attributes'][$key]['#default_value'] = $attribute->default_option;
            }
        }
    }

}

?>
Lyle's picture
Offline
AdministratoreLiTe!
Joined: 08/07/2007
Juice: 6855
Re: Re: Required default value ignored for required attribute

Or don't select "required" for the attribute. No need to hack the code.

mike-green's picture
Offline
Joined: 10/01/2008
Juice: 8
Wow. Ok, that "required"

Wow. Sad
Ok, that "required" checkbox seems misleading to me now.

Thanks for the tip.

daveX99's picture
Offline
Joined: 03/20/2010
Juice: 52
OMG Thank you. (Required Attribute)

Resurrecting this thread to thank Lyle specifically, and the other posters generally.

I would never have figured this out without his simple reply. My situation: I wanted the default option image to appear when the product page is first loaded. I guess my quibble is with the word "required". The products I'm dealing with can be ordered in many colors, and the customer must choose a color - it is 'required'! But checking the 'required' check box caused the phrase 'Please Select' to appear as the default in the select list drop-down. That meant that none of the option images would appear until you chose one.

This seemed counterintuitive.

Un-selecting 'Required' in the attributes settings makes the option images work the way I want. ... And looking at it now, I know that if a customer doesn't want the product in the default color, they will just switch (duh!).

This is a cool feature, so thanks again all around.

-dave.

greenSkin's picture
Offline
Bug FinderGetting busy with the Ubercode.
Joined: 11/06/2007
Juice: 263
Re: Required default value ignored for required attribute

What's the word on the status of getting this fixed?

ccharlton's picture
Offline
Joined: 05/19/2010
Juice: 18
radios default attribute ignored but selects work

I noticed that my radio buttons with default options chosen do not actually select themselves as expected but select lists work fine for required fields.

~ ~ ~ ~ ~
- Chris Charlton // LA Drupal Manager
... Learn how to theme Drupal! Watch my videos at http://tinyurl.com/theme-drupal
... Download software for Drupal, Eclipse IDE, and Adobe Dreamweaver: http://xtnd.us

a_c_m@drupal.org's picture
Offline
Bug Finder
Joined: 01/02/2008
Juice: 288
BUMP Quite supprised this is

BUMP

Quite supprised this is still an issue. Seems like there should be some code so if 'radio button' is selected is still 'works as expected', i.e. requires a entry, but also respects the default value.

videographics's picture
Offline
Joined: 09/25/2010
Juice: 8
Still an issue

This is still an issue for me as well...

BMWTouring's picture
Offline
Joined: 05/31/2011
Juice: 32
My Solution

in: modules/ubercart/uc_atrribute/uc_attribute.module

758
- if ($attribute->required) {
+ if (!$attribute->required) {

the NOT statement and it's all turned arround.

Maybe somebody already had this solution, but I just wanted to share this

haysuess's picture
Offline
Joined: 07/07/2008
Juice: 386
Re: Required default value ignored for required attribute

For Ubercart 7, I had to hack uc_attribute.module for another reason, but it relates to this as well.

I simply deleted line 1380:

$attribute->default_option = NULL;

This keeps Ubercart from unsetting a default option if the attribute is set to required.

I'll gladly unhack it if someone can provide help for my other issue: https://drupal.org/node/2041977