Zen Cart Logo
Forums / Bug Reports / [Done v1.5] Mandatory text attribute -problem

[Done v1.5] Mandatory text attribute -problem

Locked

Views: 1,670

Results 1 to 8 of 8
This thread is locked. New replies are disabled.
29 Jun 2011, 10:18 AM
#1
tsrplatelayer avatar

tsrplatelayer

Zen Follower

Join Date:
Oct 2010
Location:
Shropshire
Posts:
177
Plugin Contributions:
0

[Done v1.5] Mandatory text attribute -problem

Whilst playing around trying to get something else working I came across this 'anomally'

I created option-name 'Adult', type 'Text',width '5', max '1' - to display a box 5 characters wide and only permit a single character entry.

I added this attribute to a product and selected 'Attribute reqired for text' - because the buyer must enter a character (actually I want numeric 0-9, but I dont believe there is a way to validate the string entered)

When I then 'buy' the product if I don't enter text I get a warning - exactly as required

BUT:ohmy:

If I enter the character '0' (numeric zero) I still get the same warning message 'user Input required' - is this a bug??:(

AM I correct in thinking there is no way to define a default value for the string, instead of an empty string??

29 Jun 2011, 10:31 AM
#2
tsrplatelayer avatar

tsrplatelayer

Zen Follower

Join Date:
Oct 2010
Location:
Shropshire
Posts:
177
Plugin Contributions:
0

Re: [Done v1.5] Mandatory text attribute -problem

I found in functions_lookups.php in the zen_get_attributes_valid function.

// text required validation
    if (preg_match('/^txt_/', $option)) {
      $check_attributes = $db->Execute("select attributes_display_only, attributes_required from " . TABLE_PRODUCTS_ATTRIBUTES . " where products_id='" . (int)$product_id . "' and options_id='" . (int)preg_replace('/txt_/', '', $option) . "' and options_values_id='0'");
// text cannot be blank
      if ($check_attributes->fields['attributes_required'] == '1' and empty($value)) {
        $check_valid = false;
      }
    }

This appears to use the PHP empty() function to check a string

I found http://www.zachstronaut.com/posts/2009/02/09/careful-with-php-empty.html which states

  1. Do Not Use empty() with Strings

In many cases, empty() cannot be used with string variables for the simple fact that empty() returns true if your variable is set the the string value '0'. If the string character '0' is a possibly valid value for your string variable — if all non-zero-length strings are valid — you cannot use empty(). This is even more dangerous if you are using empty() to check if a string variable is defined. Also worth mentioning, empty() returns false for a string value that is nothing but a single space (or any number of spaces really).

Which thus suggests that this attribute checking code has a problem - which I dont (yet) know how to fix.

29 Jun 2011, 2:27 PM
#3
tsrplatelayer avatar

tsrplatelayer

Zen Follower

Join Date:
Oct 2010
Location:
Shropshire
Posts:
177
Plugin Contributions:
0

Re: [Done v1.5] Mandatory text attribute -problem

How about changing

// text cannot be blank
      if ($check_attributes->fields['attributes_required'] == '1' and empty($value)) {
        $check_valid = false;

to

// text cannot be blank
      if ($check_attributes->fields['attributes_required'] == '1' and empty($value) and !is_numeric($value)) {
        $check_valid = false;

see the first user contribution on http://php.net/manual/en/function.empty.php

29 Jun 2011, 2:52 PM
#4
tsrplatelayer avatar

tsrplatelayer

Zen Follower

Join Date:
Oct 2010
Location:
Shropshire
Posts:
177
Plugin Contributions:
0

Re: [Done v1.5] Mandatory text attribute -problem

tried it on my local copy - corrected the specific problem - but I dont know if there are any deeper ramifications

29 Jun 2011, 2:55 PM
#5
tsrplatelayer avatar

tsrplatelayer

Zen Follower

Join Date:
Oct 2010
Location:
Shropshire
Posts:
177
Plugin Contributions:
0

Re: [Done v1.5] Mandatory text attribute -problem

Take a look at this other thread
http://www.zen-cart.com/forum/showthread.php?p=1043112#post1043112

With a text attribute defined as mandatory and max length 1 character entering a single '0' character is reported as still being empty

using the PHP function empty() on string variables is known to report certain strings as empty which are not infact empty.

29 Jun 2011, 3:12 PM
#6
Kim avatar

Kim

Obaa-san

Join Date:
Jun 2003
Location:
West Coast, North America
Posts:
26,617
Plugin Contributions:
0

Re: [Done v1.5] Mandatory text attribute -problem

Moderator's note: Original thread merged here.

16 Aug 2011, 1:39 AM
#7
swguy avatar

swguy

Administrator

Join Date:
Feb 2006
Location:
Tampa Bay, Florida
Posts:
10,709
Plugin Contributions:
56

Re: [Done v1.5] Mandatory text attribute -problem

Have you reviewed this post:
http://www.zen-cart.com/forum/showthread.php?p=1056323#post1056323

Some mods clobber the database fields which enforce the required text attribute logic.

16 Aug 2011, 1:50 AM
#8
ajeh avatar

ajeh

Oba-san

Join Date:
Sep 2003
Location:
Ohio
Posts:
62,757
Plugin Contributions:
1

Re: [Done v1.5] Mandatory text attribute -problem

tsrplatelayer:

How about changing

// text cannot be blank
if ($check_attributes->fields['attributes_required'] == '1' and empty($value)) {
$check_valid = false;

> to
> ```
// text cannot be blank
      if ($check_attributes->fields['attributes_required'] == '1' and empty($value) and !is_numeric($value)) {
        $check_valid = false;

see the first user contribution on http://php.net/manual/en/function.empty.php

This should fix the issue where the input on a text required attribute is 0 (the number zero) as empty($value) will evaluate as empty if that value is 0 (the number zero) ...