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;
    }

eZ debug

Timing: Jan 18 2025 04:06:36
Script start
Timing: Jan 18 2025 04:06:36
Module start 'content'
Timing: Jan 18 2025 04:06:37
Module end 'content'
Timing: Jan 18 2025 04:06:37
Script end

Main resources:

Total runtime0.6966 sec
Peak memory usage4,096.0000 KB
Database Queries194

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0059 587.8438180.8125
Module start 'content' 0.00590.5674 768.6563500.9453
Module end 'content' 0.57330.1232 1,269.6016337.4688
Script end 0.6965  1,607.0703 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00400.5681210.0002
Check MTime0.00150.2121210.0001
Mysql Total
Database connection0.00070.098210.0007
Mysqli_queries0.633790.97261940.0033
Looping result0.00180.26431920.0000
Template Total0.668696.020.3343
Template load0.00230.330920.0012
Template processing0.666295.643120.3331
Template load and register function0.00010.014910.0001
states
state_id_array0.00080.121210.0008
state_identifier_array0.00090.134120.0005
Override
Cache load0.00190.2673340.0001
Sytem overhead
Fetch class attribute can translate value0.00150.218430.0005
Fetch class attribute name0.00070.096530.0002
XML
Image XML parsing0.00050.065730.0002
class_abstraction
Instantiating content class attribute0.00000.000730.0000
General
dbfile0.00260.3689210.0001
String conversion0.00000.000730.0000
Note: percentages do not add up to 100% because some accumulators overlap

CSS/JS files loaded with "ezjscPacker" during request:

CacheTypePacklevelSourceFiles
CSS0extension/community/design/community/stylesheets/ext/jquery.autocomplete.css
extension/community_design/design/suncana/stylesheets/scrollbars.css
extension/community_design/design/suncana/stylesheets/tabs.css
extension/community_design/design/suncana/stylesheets/roadmap.css
extension/community_design/design/suncana/stylesheets/content.css
extension/community_design/design/suncana/stylesheets/star-rating.css
extension/community_design/design/suncana/stylesheets/syntax_and_custom_tags.css
extension/community_design/design/suncana/stylesheets/buttons.css
extension/community_design/design/suncana/stylesheets/tweetbox.css
extension/community_design/design/suncana/stylesheets/jquery.fancybox-1.3.4.css
extension/bcsmoothgallery/design/standard/stylesheets/magnific-popup.css
extension/sevenx/design/simple/stylesheets/star_rating.css
extension/sevenx/design/simple/stylesheets/libs/fontawesome/css/all.min.css
extension/sevenx/design/simple/stylesheets/main.v02.css
extension/sevenx/design/simple/stylesheets/main.v02.res.css
JS0extension/ezjscore/design/standard/lib/yui/3.17.2/build/yui/yui-min.js
extension/ezjscore/design/standard/javascript/jquery-3.7.0.min.js
extension/community_design/design/suncana/javascript/jquery.ui.core.min.js
extension/community_design/design/suncana/javascript/jquery.ui.widget.min.js
extension/community_design/design/suncana/javascript/jquery.easing.1.3.js
extension/community_design/design/suncana/javascript/jquery.ui.tabs.js
extension/community_design/design/suncana/javascript/jquery.hoverIntent.min.js
extension/community_design/design/suncana/javascript/jquery.popmenu.js
extension/community_design/design/suncana/javascript/jScrollPane.js
extension/community_design/design/suncana/javascript/jquery.mousewheel.js
extension/community_design/design/suncana/javascript/jquery.cycle.all.js
extension/sevenx/design/simple/javascript/jquery.scrollTo.js
extension/community_design/design/suncana/javascript/jquery.cookie.js
extension/community_design/design/suncana/javascript/ezstarrating_jquery.js
extension/community_design/design/suncana/javascript/jquery.initboxes.js
extension/community_design/design/suncana/javascript/app.js
extension/community_design/design/suncana/javascript/twitterwidget.js
extension/community_design/design/suncana/javascript/community.js
extension/community_design/design/suncana/javascript/roadmap.js
extension/community_design/design/suncana/javascript/ez.js
extension/community_design/design/suncana/javascript/ezshareevents.js
extension/sevenx/design/simple/javascript/main.js

Templates used to render the page:

UsageRequested templateTemplateTemplate loadedEditOverride
1node/view/full.tplfull/forum_topic.tplextension/sevenx/design/simple/override/templates/full/forum_topic.tplEdit templateOverride template
3content/datatype/view/ezxmltext.tpl<No override>extension/community_design/design/suncana/templates/content/datatype/view/ezxmltext.tplEdit templateOverride template
9content/datatype/view/ezxmltags/paragraph.tpl<No override>extension/ezwebin/design/ezwebin/templates/content/datatype/view/ezxmltags/paragraph.tplEdit templateOverride template
3content/datatype/view/ezxmltags/line.tpl<No override>design/standard/templates/content/datatype/view/ezxmltags/line.tplEdit templateOverride template
4content/datatype/view/ezxmltags/literal.tpl<No override>extension/community/design/standard/templates/content/datatype/view/ezxmltags/literal.tplEdit templateOverride template
1pagelayout.tpl<No override>extension/sevenx/design/simple/templates/pagelayout.tplEdit templateOverride template
 Number of times templates used: 21
 Number of unique templates used: 6

Time used to render debug report: 0.0002 secs