Community & Support

#options array sets <option value = 0>, <option value = 1>, etc

In the following code:

$form['priority'] = array(
        '#type' => 'radios',
        '#title' => t('Requirement Priority'),
        '#description' => t('Allows author to prioritize the requirement'),
        '#options' => array(t('High'), t('Medium'), t('Low'))
       
       
        );

...outputs the following html:

<select name="status" class="form-select" id="edit-status" ><option value="0">Incomplete</option><option value="1">Complete</option></select>

This causes major problems when inserting records in to a db, using drupal_write_record, as it is inserting a 0, 1,2, etc in the place of the actual text value of the picklist. Has anyone handled this successfully, to grab the real values from the form?

Thanks

Comments

It is generally best practice

It is generally best practice to explicitly set key values.
This

        '#options' => array(t('High'), t('Medium'), t('Low'))

implies
        '#options' => array(0 => t('High'), 1 => t('Medium'), 2 => t('Low'))

To explicit set you might use
        '#options' => array('high' => t('High'), 'medium' => t('Medium'),  => t('Low'))

Note key values should not use the t() function.

nevets, Good idea! I'll

nevets,
Good idea! I'll give it a shot.

interesting

nevets,
This is working in general, except for one of my elements, even though drupal appears to be outputting the desired markup:

<select name="status" class="form-select" id="edit-status" ><option value="incomplete">Incomplete</option><option value="complete">Complete</option></select>

It still inserts 0 and 1 into the 'status' field of the schema...not sure what could be causing this. The other form elements are behaving properly, however.

Do you have that as an

Do you have that as an integer field, possibly a checkbox?

no...the schema

ok, the problem was I had a column in one of my tables called 'status'. This was conflicting with drupals 'Publishing options' area that is default when creating content:

<label class="option" for="edit-status-1"><input type="checkbox" name="status" id="edit-status-1" value="1"  checked="checked"  class="form-checkbox" /> Published</label>

Solution was to change it to 'requirement_status', everything working as planned now.

Thanks for your help...should have known better than to use such a generic name for a column.

Show us your code.

Show us your code.

Full-time freelancer, always looking for work.
jaypan.com (my portfolio)

nobody click here