-1

I have an implemented control check on a form coded this way:

public function checkCittaResidenza() {
    if (is_string($this->citta_residenza) && (strlen($this->citta_residenza) <= 45 || strlen($this->citta_residenza == 0))) {
        $this->corretti['citta_residenza'] = htmlentities($this->citta_residenza, ENT_QUOTES);
    } else {
        $this->ErroriTrack('citta_residenza');
    }
}

In this version, it simply checks if it is a string and check its lenght that should be less than 45 chars. It puts the string in an array corretti() if positive, else it initialize an error message specified above in an abstract class parent of the checking class.

What i'd love it to do is:

1) make a check on the string to see if it's not null. 2) if it's not null, do the check (that could be even more particular than the simple one shown here, but i don't have problems on this), put it in corretti() if correct and initializing the error if it's not, as the code now says.

3) if the string is null, the program should skip the check and directly write the null value into the array corretti(), because the form is imagined to be completed in different steps over the time, so it always happen that it's not fully filled.

I'm having problem on coding the if cycle for this last condition, every cycle i tried and imagined puts the empty condition as a cause for initializing an error.

Thank you!

tereško
  • 58,060
  • 25
  • 98
  • 150
  • Something like this will do ? `$this->corretti['citta_residenza'] = isset($this->citta_residenza)?$this->citta_residenza:NULL;` – Shankar Narayana Damodaran Mar 11 '14 at 11:47
  • Didn't read the question well yet - but a quick note: `isset()` will check if the passed thing actually exists, and is not null. If you want to check for existence but allow NULL, use property_exists – AKS Mar 11 '14 at 19:08

1 Answers1

0

Try this,

public function checkCittaResidenza() {
    if(isset($this->citta_residenza)){
if ((is_string($this->citta_residenza) && (strlen($this->citta_residenza) <= 45) || $this->citta_residenza == "")) {
    $this->corretti['citta_residenza'] = htmlentities($this->citta_residenza, ENT_QUOTES);
} else {
    $this->ErroriTrack('citta_residenza');
}
} else {
$this->corretti['citta_residenza'] = "null";
}
}
shauns2007
  • 128
  • 9
  • mmm as it happened to me before, the 2 conditions seem to conflict, now it seems not checking the request <=45 strlen. – CodeClimber Mar 11 '14 at 12:00
  • thanks for help, i finally solved this way: public function checkCittaResidenza() { if ($this->citta_residenza == NULL) { $this->corretti['citta_residenza'] = htmlentities($this->citta_residenza, ENT_QUOTES); } elseif (is_string($this->citta_residenza) && (strlen($this->citta_residenza) <= 45 )) { $this->corretti['citta_residenza'] = htmlentities($this->citta_residenza, ENT_QUOTES); } else { $this->ErroriTrack('citta_residenza'); } } simpler that it seemed to be – CodeClimber Mar 11 '14 at 12:34
  • i tried it too in the past, isset somehow checks only if the variable is set, and not its content. To clarify, if variable is null, isset give TRUE. Thanks for your help anyway! – CodeClimber Mar 11 '14 at 14:39