I recently started porting some traditional html based forms over to an API... I copied over my Form
classes and started converting them into Validation
classes since I didnt need a lot of the Form functionality any longer... But i encountered a problem and some unexpected behavior.
Traditionally when validating through a Form for an entity that already exists, I use the constructor like so:
$form = new UserForm($_POST, $existingUser);
This allows me to do some conditional checks in the forms initialize()
method. IE:
if ($this->getEntity()){
// do something different
}
The Validation
class doesnt have an entity argument in its constructor... It does however have an entity argument in its validate()
method. So I tried this:
$userValidation = new UserValidation();
$messages = $userValidation->validate($_POST, $existingUser);
I THOUGHT this was working, as i had access to $this->getEntity()
in my initialize()
method. However it seems the validation isnt working as expected. The validators (such as EmailValidator, etc...) are validating against the entities properties, not $_POST. The source code in validation.zep indeed shows thats whats going on. My question is why? Whats the use case for using the validation class with an entity? Should I just stick to Forms or am I doing something wrong?
The above code examples are contrived.