繁体   English   中英

Typo3流验证错误

[英]Typo3 flow validation error

我在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()

如果我不将验证放在动作函数中,则会收到验证消息,但是它将不应该执行的操作保存到数据库中。

if ($result->hasErrors()) {

这里的对象有错误是真的,因为它的至少一个属性有错误(通知了父对象)。

但是..父级不会将其存储在错误数组中-属性会存储-这样。. $result->getErrors()$result->getFirstError()

要从父级访问属性错误,可以使用:

$errors = $result->getFlattenedErrors();

要么

$result->forProperty('name')->getFirstError();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM