Hi,
I have a CCK Select list in my user profile (profiles as nodes), field_country, with a list of all the countries in the world and another one for birth date.
When I set field_country as required it will be set to the first country in the list (Afghanistan).

So if someone goes through the registration quickly (most people do) they will for example have todays date as their birthday and come from Afghanistan.

Is it possible to have a required select list where the user has to do an active chose (hence no default setting of the select list)?

Help appreciated!
Finn

Comments

mooffie’s picture

Is it possible to have a required select list where the user has to do an active chose (hence no default setting of the select list)?

Yes, there's a way to force a user to choose an item from a 'select list'. (I'll soon put this in a handbook page, together with other tips geared towards "Building surveys using CCK").

Here it is:

Add a "|" to the top of the "Allowed values" list. This will add a blank option to the select box. Since you're making this field 'required', Drupal will complain when this blank option is submitted.

(As for date widgets, they aren't "select lists", so this tip doesn't apply to them. But I'm not sure you were complaining about date fields. Why the date field designers decided that 'required' date fields won't have blank options as default? I don't know and I consider this a big usabilty bug. Meanwhile, if this bothers you, make your date field non-required and check its existance in a hook_nodeapi($op=='validate').)

timpa’s picture

Thanks!

"|" works excellent! Just what I was looking for!
/Finn

timpa’s picture

I found another problem with the date field.
Settings for date field year:
Advanced select options:
Select list, Years back: 100

In this way the user can choose between 1907 and 2007. The first time the user sets his birth year to e.g. 1984, everything is fine. If he then logs in again and wants to change his year of birth to e.g. 1988, it is not possible because the drop-down is now between 1882 and 1982!

Is this a bug or am I doing something wrong?

A (bad) way to solve all this would be to don't use date field at all and just insert the year, month and day as normal text field selection list.

Thanks!

mooffie’s picture

Select list, Years back: 100

You don't see 'Years forward' anywhere?

I'm using a newer version of the module, which has "Years back and forward" instead --and this could solve your problem.

A (bad) way to solve all this would be to don't use date field at all and just insert the year, month and day as normal text field selection list.

BTW, the [newer] version I'm using let us choose which date component to display as textfields and which as selection lists.

In short,
I suggest you upgrade to the latest sane version. Note that the HEAD version doesn't realy work yet, so download the 'dev' one.

timpa’s picture

Hi,
I am using the Date 5.x-1.6 (2007-Jul-06).
I see that I can set forward, but I thought that these two fields were counting from today and not from the input of the user.
Example:
If you set it to 10 years back and 10 years forward, the user can chose between 1997 and 2017. If the user chooses 1997 next time he logs in he can choose between 1987 and 2007. Little bit strange that users in this way can change the values in the selection list depending on their previous choices....??

I want people to choose their birthday, so it doesn't make sense to have years forward [only people who are born can enter their birthrate ;) ]. Simply a selection list from 2007 and 100 years back and the list should always between 2007 and 1907.

The best way might be to skip the date field and make 3 normal text field selection list, one with all months, one with all days and one with all years (which I have to update with "2008" next year).

What do you think?

Thanks,
Timpa

mooffie’s picture

Little bit strange [...] [to] change the values in the selection list depending on their previous choices

I think you have a valid point here. Please open an 'issue' and let the discussion begin.

There are several requests to improve the range handling, like this and this, and if these suggestions are implemented then a better dropdown list can be build.

The best way might be to skip the date field and make 3 normal text field selection list,

There are two different ways to understand what you're writing here.

If you mean that you want to use three different CCK fields, probably 'number' fields, then the answer is a resounding 'NO!'. Don't do that. You'll loose all the benefits of a date field. Of having a single field. It's like storing numbers ranging from 0 to 99 in two fields: one for the tens digits and one for the ones digits. You will have a hard time filtering by dates (when using Views or when issuing SQL directly) and you won't be able to put them on the Calendar.

But if you mean that you're going to use the Date widget ability to display the year component as a textfield, then why not? Go for it. There isn't any problem here. Whatever you do, the widget settings don't affect your database, so feel free to experiment.

timpa’s picture

If you mean that you want to use three different CCK fields, probably 'number' fields, then the answer is a resounding 'NO!'. Don't do that. You'll loose all the benefits of a date field. Of having a single field. It's like storing numbers ranging from 0 to 99 in two fields: one for the tens digits and one for the ones digits. You will have a hard time filtering by dates (when using Views or when issuing SQL directly) and you won't be able to put them on the Calendar.

Yes, this is what I meant... but I see your point! I'll try the other option!

Thanks!
/timpa

v1nce’s picture

Check out issue 198564. Patches are available there.

kriskd’s picture

dd a "|" to the top of the "Allowed values" list. This will add a blank option to the select box. Since you're making this field 'required', Drupal will complain when this blank option is submitted.

Should this work with select lists for profiles?

I put the | symbol as the first item in my select options for a profile field and it just behaves like the rest of the item, ie, it can be selected to fill the field just like the other options.

seth97’s picture

The field has to be set as required.
If the user choses the empty option, |, it will be denied since a value is required.

Kissa aldrig i motvind!

kriskd’s picture

Are you sure... I'm talking about the profile mod here, not regular nodes or nodeprofiles. The pipe works fine in regular nodes, but not the profile node.

In one of the field in my profile, I have it set to The user must enter a value. with the | as the first option and that can be selected line any other value.

However, on my test site, I have a nodeprofile with a select list with | as the first option and that works exactly as it should. The user must select an option, but the | symbol doesn't show and you are forced to choose an option.

I'd really like to switch over to nodeprofiles completely to take advantage of this, but it's causing me problems with a view I want to create. :-(

seth97’s picture

Sorry, I am not using the profile module....
I am using Michelle's tutorial: http://shellmultimedia.com/misc/user-profiles

Kissa aldrig i motvind!

FranciscoLuz’s picture

Hi,
In the PHP code for the "Allowed values", how to structure the return array in order to make the first option blank?

Drupal in the Amazon Jungle

jumpbug’s picture

Exactly what I was looking for as well, thanks!

sutriadi’s picture

I have similar problems but different. eg a field called SEX with allowed values: MALE and FEMALE. Then I made a choice with an empty value: ANY SEX --i added it on top allowed value: |ANY SEX. The difference is when I create a view based on the field and exposed. If I choose ANY SEX, the result is empty. How to have when choosing ANY SEX will bring up result all of the MALE and FEMALE?

thanks,

---
Indra Sutriadi Pipii
http://sutriadi.web.id

augustofagioli’s picture

I confirm the pipe ( | ) at the top of list do the job well.

How can I do something similar when populating cck select text field with a view result?
[ Which means, using the " Advanced - Nodes that can be referenced (View) " feature in the field configuration form ]

Do I have to add an empty result in my view ?

thanks

zapscribbles’s picture

I have the same question as augustofagioli.

I am getting a list of Users from a view to populate a User Reference select list field, and I need the default option to be blank.

Anyone know how to do this?

Thanks,
Stephen

swopit’s picture

Exactly what I was searching for. Thank you!

I also have another issue with the select list, maybe you guys have an easy solution for that as well.

How can I adjust the width of my select lists?

I have node types with various different select lists in the node creation view, and it just looks so ugly with different widths all the time. Is there a fast and easy way to set all of the select lists to the same width?

Please help!

Patrick

zebadix’s picture

I'm pretty new to drupal and was wondering how to replace the -none- in the select list with -Please select-

It seems like an obvious choice for a required field,

Thanks

Zeb

Zeb Adix

fnikola’s picture

As per mooffie's comment above I was able to replace the "none" in the select list by using "|please select" as the first value in the allowed values as long as the field is set to required.

petermilad’s picture

Hello,
Setting a list of allowed values actually will neglect the default value, more details comes in a second.

lets do this scenario:

1- create content type with a select list.
2- set the allowed values for this list to call an API (I'm using geonames module to get the countries list)
3- create some contents of the specified content type and setting this select list with value "none" or leave it empty
4- set this select list to be required.
5- put a default value for the select list, for example "USA".
5- edit the created content you will see that the default country is set to the first element of the list and IGNORING the default value (in my case Afghanistan would be selected not USA).

hint-1: If put "|" as a first element, it would be selected in the instead of Afghanistan.
hint-2: The default value seems to work just fine in node creation, but not in node editing (creating a new node, I see USA is selected, editing an existing node that doesn't has a selected item yet, will neglect any assigned default value and just select the first element of the list).

Thanks in advance

Peter Aziz

bussinessacc’s picture

Im made some fields in user profile , like country and i will like after selecting country to apear city from a list , so a selection field that depends on a previdios selection !

thank you

Mihai

Wolfflow’s picture

Hi @bussinessacc even if your question is some how related with this post I suggest you to create a new post with the title:

"Need to build a country list for profile that will open a new list of cities/locality related to the selected country"

So you get a better chance that someone will help you with your question.

Regards

Learning for Module Developer Status

rocheking’s picture

Same question as above... (I'm on drupal 6)
"How can I do something similar when populating cck select text field with a view result?
[ Which means, using the " Advanced - Nodes that can be referenced (View) " feature in the field configuration form ]

Do I have to add an empty result in my view ?"

suffering drupal’s picture

Is there any solution yet to this problem since the post by kriskd on December 9, 2007 at 1:16am

"dd a "|" to the top of the "Allowed values" list. This will add a blank option to the select box. Since you're making this field 'required', Drupal will complain when this blank option is submitted."

Because as he states this does NOT work for profiles, no-one has come up with an answer in four years?

I started with Drupal in 2007 and then my life got stuck...

suffering drupal’s picture

I have installed content profile, and use an additional field (not sure if this comes from CCK or from somewhere else) for country.
There I made the list with "|" as first (blank) option (which shows up empty indeed) and then the typical Afghanistan, Albania, Algeria and the rest. Two things:

1 - The "empty" option is accepted as valid even if the field is required.

2 - The dropdown is not a dropdown but a select box, more than 1 option is valid too.

Do we have to understand that creating a normal, simple signup form with a few required fields is actually not covered by Drupal?
- name
- email
- country (first empty field which really requires a selection (ONE selection))
- gender (male/female, but with first empty field, same as above)
- date of birth

The date fields throws and additional problem. In case of using the date-field from the CORE profile module, today's date is already filled out (so complies with "required"). If not changed your new user is born TODAY. In case of using fields (from CCK?) in the content profile module, the date actually turns up blank at start, and you DO get a warning that it has to be filled out and go back to the page. However in that second round the date now IS filled out, with the first option of day, month and year. So then the user CAN create a new account, still without having to select anything.

Did anyone manage to solve this essential issue (without programming, php, hooks, patches....)?

I started with Drupal in 2007 and then my life got stuck...

neos’s picture

Hi,

It's now January 2013 and still no solution?

The funny thing is, as a new Drupal user I went to register and noticed that the 'select country' list was compulsory. So I decided to leave the field unselected as "not specified" and sure enough it allowed me to register!

Drupal is great but I wish there was a better method for such seemingly simple requests to be escalated in the community. maybe there are, like I said I'm a newbie.

FranciscoLuz’s picture

"not specified" is a valid option and it does not have a null key, that's the why it does not trigger a validation error, you can check it with firebug. The reason for that is to allowed users to declare that they do not wish to specify their country.

If you set you list field value options key|value pair leaving the key empty than a validation error will be prompt to the user.

For example:
|-- select --
key_1|Option one
key_2|Option two

If you are doing that programmatically than set an option array like this:

<?php
 

$options
= array (
 
'' => t('-- select --'),
 
'key_1' => t('Option one'),
 
'key_2' => t('Option two'),
  ...
);
?>

Drupal in the Amazon Jungle