Hi,
Is there a bug in AkActiveRecord->validatesNumericalityOf with the $allow_null test. It seems like there is a test for empty missing?
public function validatesNumericalityOf($attribute_names, $message = 'not_a_number', $only_integer = false, $allow_null = false)
{
$message = isset($this->_defaultErrorMessages[$message]) ? $this->t($this->_defaultErrorMessages[$message]) : $message;
$attribute_names = Ak::toArray($attribute_names);
foreach ($attribute_names as $attribute_name){
if (isset($this->$attribute_name)){
$value = $this->$attribute_name;
if ($only_integer){
$is_int = is_numeric($value) && (int)$value == $value;
$has_error = !$is_int;
}else{
$has_error = !is_numeric($value);
}
}else{
$has_error = $allow_null ? false : true;
}
if ($has_error){
$this->addError($attribute_name, $message);
}
}
}
Should it be:
public function validatesNumericalityOf($attribute_names, $message = 'not_a_number', $only_integer = false, $allow_null = false)
{
$message = isset($this->_defaultErrorMessages[$message]) ? $this->t($this->_defaultErrorMessages[$message]) : $message;
$attribute_names = Ak::toArray($attribute_names);
foreach ($attribute_names as $attribute_name){
if (isset($this->$attribute_name) && !empty($this->$attribute_name)){
$value = $this->$attribute_name;
if ($only_integer){
$is_int = is_numeric($value) && (int)$value == $value;
$has_error = !$is_int;
}else{
$has_error = !is_numeric($value);
}
}else{
$has_error = $allow_null ? false : true;
}
if ($has_error){
$this->addError($attribute_name, $message);
}
}
}
Not sure if this forum is still active or not and whether this is the correct place to post this type of issue. If not could someone direct me to the best place to post a fix.
Cheers
Thanks for reporting the issue, ideally bugs should be reported on the Trac site. You'll need to create an account first.
$val = 0;
var_dump(!empty($val)); // FALSE
Looks like your patch will not accept zero, which is still a numeric integer.
Hi Bermi,
Ah yes, I see your point. This will allow 0 when $allow_null is true but will reject it otherwise. How about this:
public function validatesNumericalityOf($attribute_names, $message = 'not_a_number', $only_integer = false, $allow_null = false)
{
$message = isset($this->_defaultErrorMessages[$message]) ? $this->t($this->_defaultErrorMessages[$message]) : $message;
$attribute_names = Ak::toArray($attribute_names);
foreach ($attribute_names as $attribute_name){
if ((isset($this->$attribute_name) && !empty($this->$attribute_name)) || (int)$this->$attribute_name==0){
$value = $this->$attribute_name;
if ($only_integer){
$is_int = is_numeric($value) && (int)$value == $value;
$has_error = !$is_int;
}else{
$has_error = !is_numeric($value);
}
}else{
$has_error = $allow_null ? false : true;
}
if ($has_error){
$this->addError($attribute_name, $message);
}
}
}
I'll try adding this over on Trac but I wanted to update the post in case.
Cheers
1 to 3 of 3