Forums / Setup & design / Problem with empty option in select input

Problem with empty option in select input

Author Message

Julien Lavergne

Tuesday 01 July 2008 4:11:12 pm

Hello,

In my project, I need to display a select list in a form. This select is required but has no default value.
To avoid the fact that ezpublish select the first item by default, in my class definition, I put an empty line option at the start of options list of my selection attribute. So my select list is well displayed with the empty line at the start.
But if I submit the form, ezpublish send value="0" instead of value="" so the value is not recognize as invalid.

So my question is : Is there a way to have a required select list with no default value (or with a null default value) without having issues with the form submit ?

Thank you

Michael Scofield

Tuesday 01 July 2008 6:28:54 pm

I'm not too experienced with Ez Publish, but I think you need to create a new datatype for that.

E.g. You can duplicate the code for existing Selection datatype, and just change or add the code specific for what you want to do. In this case, you should override the function validateCollectionAttributeHTTPInput() inside your custom datatype, and add the code for validation (if the submitted value is '0', then you return the value eZInputValidator::STATE_INVALID).

I had the same problems last week and I solved the problem creating a new datatype based on the Selection that is bundled with ez publish.

It was difficult to me to understand datatype creation because there isn't any good material on-line available explaining how to create a datatype, but I found a book (I had to pay for it) called "Learning Ez Publish", by Martin Bauer, Tony Wood, Paul Forsyt ... from Packt Publishing, and that was good enough and I could create my datatype.

You can find fragmented information in the next references - but I recommend the book above:

http://ez.no/ezpublish/documentation/development/extensions/datatypes/new_datatype

http://ez.no/developer/articles/creating_datatypes_in_ez_publish_4

Book: Ez Publish Basics, by Balazs Halasy

I hope it helps.
Michael

Julien Lavergne

Tuesday 01 July 2008 7:14:25 pm

Thank you Michael, but something tell me that it is not just a matter of new type.

In fact, as I saw in ezselection.tpl file, "selected_id_array" variable is managed as an array type variable. So the POST variable is an array, we get an array, we send an array etc... (see name="{$attribute_base}_ezselect_selected_array_{$attribute.id}[]")

But in ezselectiontype.php file, to check if the variable is valid or not, we manage this array as a string (see $data == "")

So I would say that there is a mistake in ezselectiontype.php file and that the variable has to be managed as in ezcountrytype.php (see count( $data ) > 0 and $data[0] != '').
By this way, I will be able to change just ezselection.tpl file to handle empty options like :

  {if $Options.item.name|compare("")}
    <option value="" {if $selected_id_array|count()|eq(0)}selected="selected"{/if}>{$Options.item.name|wash( xhtml )}</option>
  {else}
    <option value="{$Options.item.id}" {section show=$selected_id_array|contains( $Options.item.id )}selected="selected"{/section}>{$Options.item.name|wash( xhtml )}</option>
  {/if}

Am I totally wrong ?

<b>ezselection.tpl</b>

{* DO NOT EDIT THIS FILE! Use an override template instead. *}
{default attribute_base=ContentObjectAttribute}
{let selected_id_array=cond( is_set( $#collection_attributes[$attribute.id] ), $#collection_attributes[$attribute.id].content, $attribute.content )}

{* Always set the .._selected_array_.. variable, this circumvents the problem when nothing is selected. *}
<input type=hidden name="{$attribute_base}_ezselect_selected_array_{$attribute.id}" value="">

<select name="{$attribute_base}_ezselect_selected_array_{$attribute.id}[]" {section show=$attribute.class_content.is_multiselect}multiple{/section}>
{section var=Options loop=$attribute.class_content.options}
<option value="{$Options.item.id}" {section show=$selected_id_array|contains( $Options.item.id )}selected="selected"{/section}>{$Options.item.name|wash( xhtml )}</option>
{/section}
</select>
{/let}
{/default}

<b>ezselectiontype.php</b>

function validateCollectionAttributeHTTPInput( $http, $base, $contentObjectAttribute )
    {
        if ( $http->hasPostVariable( $base . '_ezselect_selected_array_' . $contentObjectAttribute->attribute( 'id' ) ) )
        {
            $data = $http->postVariable( $base . '_ezselect_selected_array_' . $contentObjectAttribute->attribute( 'id' ) );

            if ( $data == "" && $contentObjectAttribute->validateIsRequired() )
            {
                $contentObjectAttribute->setValidationError( ezi18n( 'kernel/classes/datatypes', 'Input required.' ) );
                return eZInputValidator::STATE_INVALID;
            }
            else
            {
                return eZInputValidator::STATE_ACCEPTED;
            }
        }
        else
        {
            return eZInputValidator::STATE_INVALID;
        }
    }

<b>ezcountrytype.php</b>

function validateCollectionAttributeHTTPInput( $http, $base, $contentObjectAttribute )
    {
        if ( !$contentObjectAttribute->validateIsRequired() )
            return eZInputValidator::STATE_ACCEPTED;

        if ( $http->hasPostVariable( $base . '_country_' . $contentObjectAttribute->attribute( 'id' ) ) )
        {
            $data = $http->postVariable( $base . '_country_' . $contentObjectAttribute->attribute( 'id' ) );

            if ( count( $data ) > 0 and $data[0] != '' )
                return eZInputValidator::STATE_ACCEPTED;
        }

        $contentObjectAttribute->setValidationError( ezi18n( 'kernel/classes/datatypes',
                                                             'Input required.' ) );
        return eZInputValidator::STATE_INVALID;
    }