Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

how to manually reset/delete all validation messages in a model

Hi All,

is there a way to manually reset/delete all validation messages that are already generated?

What I have found is that invoking validation() method several times multiply number of generated messages. On the other hand invoking create() method cause reseting/deleting all messages.

My model with 2 validators:

class Customers extends \Phalcon\Mvc\Model {

  public function validation() {

          $this->validate(new PresenceOf(array(
                'field' => 'country',
                'message' => "My individual message!"
            )));

          $this->validate(new InclusionIn(
              array(
                  "field"  => "country",
                  "domain" => array("sk","cz"),
                  'message' => "Wrong country."
              )
          ));

          return $this->validationHasFailed() != true;
      }

}

My controller:

$customer = new Customers();
$customer->validation();

//at the time there are 2 messages generated (one message for each validator)

$customer->validation();
$customer->validation();
$customer->validation();

//at the time there are 8 messages generated

$customer->create(); //this method reset messages and performs new validation

//at the moment there is only one message ("country is required")

$customer->validation();

/*at the moment there are 3 messages:
"country is required"
"My individual message!"
"Wrong country."
*/

At the end I would like to have in the array only last 2 messages - I mean those performed in the last validation().

TIA

My first question would be why are you calling validation multiple times? I can't think of a reason you would want/need to.

Anyway, the messages are stored in a variable called $_messages. You can override your $customer->validation() method to flush $_messages first, then call parent::validation()



1.6k

@quasipickle

Thank you!

My first question would be why are you calling validation multiple times? I can't think of a reason you would want/need to.

I've called it four times only to show that the messages are being multiplied. I've forgot to stress it. I can't think of such reason too :)

I need to get rid of default messages made by PresenceOf notNullValidations.

Anyway, the messages are stored in a variable called $messages. You can override your $customer->validation() method to flush $messages first, then call parent::validation()

I have tried your solution but have following problems:

  • Could you specify where to put parent::validation() and how exactly to flush $_messages variable? I've tried to do so:
class Customers extends \Phalcon\Mvc\Model {

  public function validation() {

    $this->_messages = array(); //flushing the variable

    $this->validate(new PresenceOf(array(
      'field' => 'country',
      'message' => "My individual message!"
    )));

    $this->validate(new InclusionIn(array(
      "field"  => "country",
      "domain" => array("sk","cz"),
      'message' => "Wrong country."
    )));

    parent::validation();

    return $this->validationHasFailed() != true;
  }

}

but I am getting the PHP error: The method "validation" doesn't exist on model "Customers".

  • I have var_dump'ed my $customer object and there is no such variable as _messages. Are you sure on it?

How you've written your validation() method is just how I would go about doing it. I'm not sure why you're getting that error.

As far as $_messages is concerned, I'm somewhat certain. I had to dig through the C source to find a reference to it. Maybe it would be easier to just make a clearMessages() method that empties it out.



1.6k

Found the array with messages, it's name is _errorMessages.

This code works:

class Customers extends \Phalcon\Mvc\Model {

  public function validation() {

    $this->_errorMessages = array(); //flushing the messages

    $this->validate(new PresenceOf(array(
      'field' => 'country',
      'message' => "My individual message!"
    )));

    return $this->validationHasFailed() != true;
  }

}

So I think there is no need to invoke parent::validation().

But is the above safe? I mean, is it any risk it will couse some problems, some inconsistency within a model?



46.8k
Accepted
answer

"safe" in this context is a subjective term. You're resetting an array used internally by Phalcon. Any time you mess around with the inner workings of a system, you risk the chance that you're going to cause a condition the system isn't built to handle. With that said though, _errorMessages is just an array, so there's not a lot that could go wrong.

If I were in your position, my first approach would be to eliminate the situation where I needed to reset _errorMessages. I don't know your situation in full, but it seems like a sub-optimal design if validation needs to be referenced multiple times.

My second approach, if the first couldn't be accomplished, would be to do exactly what you're doing. While it's not ideal in my opinion, I don't think resetting _errorMessages will cause anything to break.



1.6k

Invoking clearMessages() couses: The method "clearMessages" doesn't exist on model "Customers".

Empting $this->_errorMessages works.

My all concern is about this consistency, thanks a lot quasipickle!