我在 Typo3 流程中的验证有问题。
我正在为 REST 做 POST 请求,这是控制器中的操作。
/**
* 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());
}
}
这就是我在模型中验证的方式。
/**
* @var string
* @Flow\Validate(type="EmailAddress")
*/
protected $mail;
/**
* @var string
* @Flow\Validate(type="StringLength", options={ "minimum"=8, "maximum"=8 })
*/
protected $phone;
我得到的错误是这个。
致命错误:在第 87 行的 /path_to_project/flow2/Data/Temporary/Development/Cache/Code/Flow_Object_Classes/path_to_Controller.php 中的非对象上调用成员函数 getMessage()
如果我不将验证放在操作函数中,那么我会收到验证消息,但它会将不应执行的操作保存到数据库中。