Symfony Validator 修改表单字段
Symfony Validator modify form fields
我有一个如下所示的验证器:
class ValidUSPSAddressValidator extends ConstraintValidator
{
public function validate($protocol, Constraint $constraint)
{
$verify = new USPSAddressVerify('*******');
$uspsAddress = new USPSAddress();
$apartment = $protocol->getApartment();
$address = $protocol->getAddress();
$city = $protocol->getCity();
$state = $protocol->getState();
$zip = $protocol->getZip();
$uspsAddress->setApt($apartment);
$uspsAddress->setAddress($address);
$uspsAddress->setCity($city);
$uspsAddress->setState($state);
$uspsAddress->setZip5($zip);
$uspsAddress->setZip4('');
$verify->addAddress($uspsAddress);
$verify->verify();
$response = $verify->getArrayResponse();
$responseAddress = $response['AddressValidateResponse']['Address'];
if ($verify->isError()) {
$this->context->buildViolation($constraint->message)
->addViolation();
}else{
$protocol->setAddress($responseAddress['Address2']);
$protocol->setCity($responseAddress['City']);
$protocol->setState($responseAddress['State']);
$protocol->setZip($responseAddress['Zip5']);
//****** THE BELOW IF ******************
if($responseAddress['Address2'] != $address || $responseAddress['City'] != $city || $responseAddress['State'] != $state || $responseAddress['Zip5'] != $zip){
$this->context->buildViolation($constraint->verify)
->addViolation();
}
}
}
}
查看最终的 IF 语句和其中的代码...我正在尝试将字段设置为 USPS 响应的内容,验证器基本上为用户提供了最后一次机会来查看更新的数据修改字段以及哪些然后将再次重新验证,只要数据与 USPS 数据完全匹配,它就会保留它。问题是 protocol->setXXXXX() 将对象上的数据设置为要持久化,但在重新显示表单时只是不显示字段中的数据...
Validator 的目的是验证 Object,也许可以尝试使用 data transformers or form listeners or even event listeners,并跳过验证器内部的这种逻辑。
我有一个如下所示的验证器:
class ValidUSPSAddressValidator extends ConstraintValidator
{
public function validate($protocol, Constraint $constraint)
{
$verify = new USPSAddressVerify('*******');
$uspsAddress = new USPSAddress();
$apartment = $protocol->getApartment();
$address = $protocol->getAddress();
$city = $protocol->getCity();
$state = $protocol->getState();
$zip = $protocol->getZip();
$uspsAddress->setApt($apartment);
$uspsAddress->setAddress($address);
$uspsAddress->setCity($city);
$uspsAddress->setState($state);
$uspsAddress->setZip5($zip);
$uspsAddress->setZip4('');
$verify->addAddress($uspsAddress);
$verify->verify();
$response = $verify->getArrayResponse();
$responseAddress = $response['AddressValidateResponse']['Address'];
if ($verify->isError()) {
$this->context->buildViolation($constraint->message)
->addViolation();
}else{
$protocol->setAddress($responseAddress['Address2']);
$protocol->setCity($responseAddress['City']);
$protocol->setState($responseAddress['State']);
$protocol->setZip($responseAddress['Zip5']);
//****** THE BELOW IF ******************
if($responseAddress['Address2'] != $address || $responseAddress['City'] != $city || $responseAddress['State'] != $state || $responseAddress['Zip5'] != $zip){
$this->context->buildViolation($constraint->verify)
->addViolation();
}
}
}
}
查看最终的 IF 语句和其中的代码...我正在尝试将字段设置为 USPS 响应的内容,验证器基本上为用户提供了最后一次机会来查看更新的数据修改字段以及哪些然后将再次重新验证,只要数据与 USPS 数据完全匹配,它就会保留它。问题是 protocol->setXXXXX() 将对象上的数据设置为要持久化,但在重新显示表单时只是不显示字段中的数据...
Validator 的目的是验证 Object,也许可以尝试使用 data transformers or form listeners or even event listeners,并跳过验证器内部的这种逻辑。