I have problem with validation in Typo3 flow.
I'm doing POST request for REST and this is action in controller for it.
/**
* Create Customer
*
*
* @param int $clientId
* @param string $name
* @param string $phone
* @param string $mail
* @param string $zip
* @param string $city
* @param int countryId
* @return string
*/
public function createAction($clientId, $name, $phone, $mail, $zip, $city, $countryId) {
try{
$this->checkAccessArgs();
$client = $this->clientCustomerRepository->getReference('\Shopbox\API\Domain\Model\Client',$clientId);
$country = $this->clientCustomerRepository->getReference('\Shopbox\API\Domain\Model\Country',$countryId);
$newCustomer = new ClientCustomer();
$newCustomer->setClient($client);
$newCustomer->setCountry($country);
$newCustomer->setName($name);
$newCustomer->setPhone($phone);
$newCustomer->setMail($mail);
$newCustomer->setZip($zip);
$newCustomer->setCity($city);
$newCustomer->setCrdate(time());
$newCustomer->setTstamp(time());
$newCustomer->setDeleted(0);
$newCustomer->setHidden(0);
$customerValidator = $this->validatorResolver->getBaseValidatorConjunction('\Shopbox\API\Domain\Model\ClientCustomer');
$result = $customerValidator->validate($newCustomer);
if($result->hasErrors()){
throw new \Exception($result->getFirstError()->getMessage() ,$result->getFirstError()->getCode());
}
$this->clientCustomerRepository->add($newCustomer);
$this->persistenceManager->persistAll();
$this->response->setStatus(201);
$this->view->setVariablesToRender(array(self::JSON_RESPONSE_ROOT_SINGLE));
$this->view->assign(self::JSON_RESPONSE_ROOT_SINGLE,
array(self::JSON_RESPONSE_ROOT_SINGLE=> $newCustomer)
);
}
catch(\Exception $e){
$this->response->setStatus($e->getCode());
return $this->assignError($e->getMessage());
}
}
This is how I validate in Model.
/**
* @var string
* @Flow\Validate(type="EmailAddress")
*/
protected $mail;
/**
* @var string
* @Flow\Validate(type="StringLength", options={ "minimum"=8, "maximum"=8 })
*/
protected $phone;
Error that I get is this.
Fatal error: Call to a member function getMessage() on a non-object in /path_to_project/flow2/Data/Temporary/Development/Cache/Code/Flow_Object_Classes/path_to_Controller.php on line 87
If I don't put validation inside action function then I get validation message but it saves to database what it shouldn't do.
Here your object has errors is true, because at least one of its properties have errors (parent is notified).
But.. parent doesn't store it in his errors array - property does - so..
$result->getErrors()
is empty same as$result->getFirstError()
..To get access from parent to properties errors you can use:
or