Results 1 to 8 of 8
  1. #1
    Join Date
    Oct 2010
    Location
    Shropshire
    Posts
    174
    Plugin Contributions
    0

    Default [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

    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??

  2. #2
    Join Date
    Oct 2010
    Location
    Shropshire
    Posts
    174
    Plugin Contributions
    0

    Default Re: Mandatory text attribute -problem

    I found in functions_lookups.php in the zen_get_attributes_valid function.
    Code:
    // 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/20...php-empty.html which states
    2. 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.

  3. #3
    Join Date
    Oct 2010
    Location
    Shropshire
    Posts
    174
    Plugin Contributions
    0

    Default Re: Mandatory text attribute -problem

    How about changing
    Code:
    // text cannot be blank
          if ($check_attributes->fields['attributes_required'] == '1' and empty($value)) {
            $check_valid = false;
    to
    Code:
    // 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

  4. #4
    Join Date
    Oct 2010
    Location
    Shropshire
    Posts
    174
    Plugin Contributions
    0

    Default Re: Mandatory text attribute -problem

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

  5. #5
    Join Date
    Oct 2010
    Location
    Shropshire
    Posts
    174
    Plugin Contributions
    0

    Default Mandatory text attribute incorrectly returns 'user input required'

    Take a look at this other thread
    http://www.zen-cart.com/forum/showth...12#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.

  6. #6
    Join Date
    Jun 2003
    Posts
    33,715
    Plugin Contributions
    0

    Default Re: Mandatory text attribute -problem

    Moderator's note: Original thread merged here.
    Please do not PM for support issues: a private solution doesn't benefit the community.

    Be careful with unsolicited advice via email or PM - Make sure the person you are talking to is a reliable source.

  7. #7
    Join Date
    Feb 2006
    Location
    Tampa Bay, Florida
    Posts
    9,621
    Plugin Contributions
    123

    Default Re: Mandatory text attribute -problem

    Have you reviewed this post:
    http://www.zen-cart.com/forum/showth...23#post1056323

    Some mods clobber the database fields which enforce the required text attribute logic.
    That Software Guy. My Store: Zen Cart Modifications
    Available for hire - See my ad in Services
    Plugin Moderator, Documentation Curator, Chief Cook and Bottle-Washer.
    Do you benefit from Zen Cart? Then please support the project.

  8. #8
    Join Date
    Sep 2003
    Location
    Ohio
    Posts
    69,402
    Plugin Contributions
    6

    Default Re: Mandatory text attribute -problem

    Quote Originally Posted by tsrplatelayer View Post
    How about changing
    Code:
    // text cannot be blank
          if ($check_attributes->fields['attributes_required'] == '1' and empty($value)) {
            $check_valid = false;
    to
    Code:
    // 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) ...
    Linda McGrath
    If you have to think ... you haven't been zenned ...

    Did YOU buy the Zen Cart Team a cup of coffee and a donut today? Just click here to support the Zen Cart Team!!

    Are you using the latest? Perhaps you've a problem that's fixed in the latest version: [Upgrade today: v1.5.5]
    Officially PayPal-Certified! Just click here

    Try our Zen Cart Recommended Services - Hosting, Payment and more ...
    Signup for our Announcements Forums to stay up to date on important changes and updates!

 

 

Similar Threads

  1. v154 File Attribute Mandatory
    By jskandhari in forum Setting Up Categories, Products, Attributes
    Replies: 1
    Last Post: 3 Feb 2015, 10:06 PM
  2. Mandatory File Attribute?
    By JB2011 in forum Setting Up Categories, Products, Attributes
    Replies: 1
    Last Post: 9 Dec 2011, 06:29 PM
  3. Text Attribute, can this be done
    By canihave in forum Setting Up Categories, Products, Attributes
    Replies: 0
    Last Post: 6 Jan 2011, 06:26 PM
  4. TEXT attribute is no longer Mandatory
    By slavka001 in forum Setting Up Categories, Products, Attributes
    Replies: 3
    Last Post: 24 Aug 2007, 05:03 PM
  5. Mandatory Text Attribute
    By JSWeb in forum Setting Up Categories, Products, Attributes
    Replies: 2
    Last Post: 31 Jan 2007, 05:22 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
disjunctive-egg
Zen-Cart, Internet Selling Services, Klamath Falls, OR