[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??
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
Quote:
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.
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
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
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.
Re: Mandatory text attribute -problem
Moderator's note: Original thread merged here.
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.
Re: Mandatory text attribute -problem
Quote:
Originally Posted by
tsrplatelayer
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) ...