Writing a validation rule for ezselection

Author Message

James Packham

Wednesday 03 March 2004 4:20:25 am

Hi,

I found out in this post http://www.ez.no/community/forum/setup_design/specify_null_in_a_select_statement that I need to do a slight hack to add a validation rule for ezselection (in kernel/classes/datatypes/ezselection/ezselectiontype.php). Unfortunately my PHP isn't too hot, but I've got this so far:

Taken from line 67.

function validateClassAttributeHTTPInput( &$http, $base, &$contentObjectAttribute )
{
/*
If a post variable exists and this is not a multiple select then check whether
the value contains the word "NULL" if it does return state invalid.
*/
if ( $http->hasPostVariable( $base . '_ezselect_selected_array_' . $contentObjectAttribute->attribute( 'id' ) . '.0' ) )
{
$selection =& $http->postVariable( $base . '_ezselect_selected_array_' . $contentObjectAttribute->attribute( 'id' ) . '.0' );
if ( $selection == "NULL" )
{
return EZ_INPUT_VALIDATOR_STATE_INVALID;
}
else
{
return EZ_INPUT_VALIDATOR_STATE_ACCEPTED;
}
}
else
{
return EZ_INPUT_VALIDATOR_STATE_ACCEPTED;
}
}

As you can see I'm trying to make the select throw an error if the given value is the work "NULL". Unfortunately this doesn't seem to be working and I was wondering if anyone could give some feedback on my code? I realise that I haven't implemented the recognition of a multiple select part yet, but I'm pretty sure this isn't my problem.

Thanks,

James

Paul Forsyth

Wednesday 03 March 2004 6:04:35 am

Hi James,

You are on the right track, apart from the function you are using. It should be:

function validateObjectAttributeHTTPInput( &$http, $base, &$contentObjectAttribute )

The class function you are using is used to check changes you make to the class itself. In other datatypes this would check default values and do further things for more complex datatypes.

One thing to note from the previous thread, you may want to check on the values coming over via $http since you may never reach a null statement because you always have an option. Therefore you may need to check the first element is as your expect, maybe use something else apart from null and test by examining the string.

Hope this helps

paul

James Packham

Wednesday 03 March 2004 6:37:09 am

Thanks Paul,

Please excuse me reusing your text :) I moved my code into the correct statement (I should have realised I got this wrong!).

>> One thing to note from the previous thread, you may want to check on the values coming over via
>> $http since you may never reach a null statement because you always have an option.

I think I'm ok on this, I'm running it with a {$http.post|attribute(show)} I get:

Attribute Type Value
ContentObjectAttribute_ezselect_selected_array_5457 array Array(1)
>0 string 'NULL'

>> Therefore you may need to check the first element is as your expect, maybe use something else apart
>> from null and test by examining the string.

Good idea! However, when I tried it I get "uerl" and "test" in place of NULL (as specified in the select in the tpl). I was wondering - am I specifying the array attribute properly?

My code still looks the same as above, except the first line is now:

function validateObjectAttributeHTTPInput( &$http, $base, &$contentObjectAttribute )

There also doesn't appear to be anything in the debug output relating to the PHP file.

Regards,

James

Paul Forsyth

Wednesday 03 March 2004 6:51:29 am

Ok, assume your template code is as before (just being explicit here). Notice i give a value for the option

<select name="{$attribute_base}_ezselect_selected_array_{$attribute.id}[]" {section show=$attribute.class_content.is_multiselect}multiple{/section}>

<option value="default">Please choose a region</option>

</select>

and your kernel code is:

function validateClassAttributeHTTPInput( &$http, $base, &$contentObjectAttribute )
{
/*
If a post variable exists and this is not a multiple select then check whether
the value contains the word "NULL" if it does return state invalid.
*/
if ( $http->hasPostVariable( $base . '_ezselect_selected_array_' . $contentObjectAttribute->attribute( 'id' ) ) )
{
   $selection =& $http->postVariable( $base . '_ezselect_selected_array_' . $contentObjectAttribute->attribute( 'id' ) );

   /* Debug */
   eZDebug::writeNotice("Selection count".count($selection));
   eZDebug::writeNotice("First selection".$selection[0]));

   if ( $selection == "default" )
   {
        return EZ_INPUT_VALIDATOR_STATE_INVALID;
   }
   else
   {
        return EZ_INPUT_VALIDATOR_STATE_ACCEPTED;
   }
else
{
   return EZ_INPUT_VALIDATOR_STATE_ACCEPTED;
}
}

Have a look in var/log/notice.log to see if these lines are being output - they should be. Somtimes you cant see results in the browser window due to redirections going on in the kernel, but they are output to file.

What values do you get for $selection[0]?

paul

James Packham

Wednesday 03 March 2004 8:50:39 am

Ah ha! We figured it out, I should have set

if ( $http->hasPostVariable( $base . '_ezselect_selected_array_' . $contentObjectAttribute->attribute( 'id' ) ) )
{
$selection =& $http->postVariable( $base . '_ezselect_selected_array_' . $contentObjectAttribute->attribute( 'id' ) );
if ( $selection[0] == "" )

to make it work. I got someone who knows a little more about this I to expand on the PHP a bit more, to check for that the array has only one item etc.

Thanks for your help Paul :)

Regards,

James

Powered by eZ Publish™ CMS Open Source Web Content Management. Copyright © 1999-2014 eZ Systems AS (except where otherwise noted). All rights reserved.