[英]Typo3 flow validation error
I have problem with validation in Typo3 flow. 我在Typo3流中验证存在问题。
I'm doing POST request for REST and this is action in controller for it. 我正在执行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());
}
}
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 致命错误:在第87行的/path_to_project/flow2/Data/Temporary/Development/Cache/Code/Flow_Object_Classes/path_to_Controller.php中的非对象上,调用成员函数getMessage()
If I don't put validation inside action function then I get validation message but it saves to database what it shouldn't do. 如果我不将验证放在动作函数中,则会收到验证消息,但是它将不应该执行的操作保存到数据库中。
if ($result->hasErrors()) {
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()
.. 但是..父级不会将其存储在错误数组中-属性会存储-这样。. $result->getErrors()
与$result->getFirstError()
。
To get access from parent to properties errors you can use: 要从父级访问属性错误,可以使用:
$errors = $result->getFlattenedErrors();
or 要么
$result->forProperty('name')->getFirstError();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.