Forums

Optional and validated input

rennye 26 Nov, 2011
How can I add a input field to a form that is validated but optional. I use Auto Server Side Validation for form validation and I want to have a text field that can only contain alphanumeric content. If I set the validation action to validate the input field as an alphanumeric content then it will not allow empty values. I don't quite understand what is the point of having a separate 'Not Empty' validation rule when all the validation rules require that the content is not empty.

I checked the code and found this regular expression
function validate_alphanumeric($str, $form){
  if(isset($form->data[$str])){
    return preg_match('/^[a-z0-9 ._-]+$/i', $form->data[$str]);
  }
}


I would change the plus sign at the end of expression to ?, but that is just my opinion.

Got any ideas for a solution?

-Rennye
GreyHead 29 Nov, 2011
Hi Rennye,

This looks like a new bug find to me. I'll add it to the bugs list.

You could change the regex to '/^[a-z0-9 ._-]*$/' I think that would work but better to change the if clause so that it only runs if there is data in the input:
function validate_alphanumeric($str, $form){
  if ( isset($form->data[$str]) && $form->data[$str] ){
    return preg_match('/^[a-z0-9 ._-]+$/i', $form->data[$str]);
  }
}

This would apply to most of the functions in administrator/components/com_chronoforms/form_actions/auto_serverside_validation/auto_serverside_validation.php

The problem is that isset() returns true if a value exists even if the value is zero, or an empty string (it returns false if the value does not exist or is NULL).

Bob
rennye 29 Nov, 2011
Thanks for the reply.

To be precise, I think that the second condition in the if clause should actually be something like

strlen($form->data[$str])


...and this because a string containing the character '0' is evaluated to false when cast to boolean (and so is the number zero also). Another option is that you could use an alternative for the empty-function, that would actually test if the variable has no content. The PHP documentation site has a couple of good examples in the comments section of the documentation page for the empty()-function.

I will make fix to the component. Hopefully this issue is solved in the next release.

-Rennye
GreyHead 29 Nov, 2011
Hi rennye,

Good catch.

I'd also wondered if some of the PHP sanitization/validation functions (and the Joomla! JRequest implementations) might be useful. But as I have no influence over the code Max writes that's a bit academic.

Bob
rennye 29 Nov, 2011
It still isn't right..😉

I tried the code and noticed, that when the input data is empty the preg_match function never gets called and function runs out and returns false (when it actually should have returned true, because the input was empty). In other words there is no point in allowing the regular expression to pass through empty strings when the empty strings are blocked before the ever reach the expression. I came up with two different solutions..

function validate_alphanumeric($str, $form){
	if(isset($form->data[$str]) && strlen($form->data[$str])){
		// note: only non-empty strings are passed
		return preg_match('/^[a-z0-9 ._-]+$/i', $form->data[$str]);
	}
	
	return true;
}


...or...

function validate_alphanumeric($str, $form){
	if(isset($form->data[$str])){
		return preg_match('/^[a-z0-9 ._-]*$/i', $form->data[$str]);
	}
}



-Rennye
GreyHead 29 Nov, 2011
Hi rennye,

What about using the existing validate_not_empty() function??
function validate_alpha($str, $form){
  if( validate_not_empty($str, $form) ) {
    return preg_match('/^[a-z ._-]+$/i', $form->data[$str]);
  }
}

Bob
This topic is locked and no more replies can be posted.