我创建了自定义约束验证器:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<div style="margin-left: 23px;">
<legend class="legend">List of Stores which completed the survey so far</legend>
<select for="postage" id="store_select">
<option value="">Please select...</option>
<option value="finished" id ="Finshed">Stores Completed.</option>
<option value="notFinished" id ="NotFinsh">Stores Not Completed</option>
</select>
<br />
<br />
<div id="completedStores" class="displaystore2">
Stores Completed Div
</div>
<br />
<div id="notCompletedStores" class="displaystore1">
NOT Stores Completed Div
<br />
<br />
</div>
</div>
在docs中声明:
返回当前验证的对象。
,但对我来说,它会返回 <?php
$cn= mysqli_connect('localhost','testuser','password','invoicedb');
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
echo "string";
if($_SERVER["REQUEST_METHOD"] == "POST")
{echo "string";
$fname=$_POST['F_Name'];
$lname=$_POST['L_Name'];
$emailID=$_POST['EmailID'];
$phone_number=$_POST['Phone_Number'];
$reference_code=$_POST[‘R_Code’];
$branch=$_POST[‘subject’];
$percentage=$_POST[‘percentage’];
echo "insert into students(first_name,last_name,email,phone_number,reference_code,branch,percentage) VALUES ('$fname','$lname','$emailID','$phone_number','$reference_code','$branch','$percentage')";
mysqli_query($cn,"insert into students(first_name,last_name,email,phone_number,reference_code,branch,percentage) VALUES ('$fname','$lname','$emailID','$phone_number','$reference_code','$branch','$percentage')");
//best outside the if statement so user isn't stuck on a white blank page.
header("location:confirm.php");
exit;
}
?>
。
P.S。我不想将此约束分配给实体,仅限于某些表单或字段。
我的表单属性已经过验证:
class CustomConstraint extends Constraint
{
public $message = '';
}
class CustomConstraintValidator extends ConstraintValidator
{
public function validate($value, Constraint $constraint)
{
exit($this->context->getObject()); // returns null
}
}
实体中的财产:
NULL
更新
我尝试将约束放在其他字符串属性上,我仍然得到->add('rejectReasons', null, array(
'property' => 'name',
'multiple' => true,
'constraints' => array(
new CustomConstraint(array(
'message' => 'Application can not be refused.'
)),
)
));
。
答案 0 :(得分:2)
看看ExecutionContextInterface它说:
getObject()返回当前验证的对象。
如果验证器当前正在验证类约束,则 返回该类的对象。如果它是验证属性或 getter constraint ,属性/ getter所属的对象是 返回。
在其他情况下,返回null。
正如您所看到的,您必须分配给某个类或属性或getter。否则你会得到null
。
答案 1 :(得分:0)
如果您开发类约束验证器,请记住添加getTargets方法作为示例:
public function getTargets()
{
return self::CLASS_CONSTRAINT;
}
如文档
中所述here答案 2 :(得分:0)
对于那些使用依赖项本身进行表单验证的人可能会有所帮助。
我假设Symfony版本是3.4或4.1,并且您的项目中有df.pivot_table(index=['ticker'], columns=['other'], values=['cov'], fill_value=1).values
array([[ 1.00000000e+00, 5.62318492e-05, 6.05076457e-05,
6.99435817e-05, 4.20812754e-05],
[ 5.62318492e-05, 1.00000000e+00, 8.57872875e-05,
1.16723972e-04, 4.63723078e-05],
[ 6.05076457e-05, 8.57872875e-05, 1.00000000e+00,
8.88688235e-05, 4.29291322e-05],
[ 6.99435817e-05, 1.16723972e-04, 8.88688235e-05,
1.00000000e+00, 5.04454626e-05],
[ 4.20812754e-05, 4.63723078e-05, 4.29291322e-05,
5.04454626e-05, 1.00000000e+00]])
。
处理带有某种依赖性的Symfony表单验证程序的最佳方法是使用CustomValidators
上面是我用来与他们合作的示例。
假设我们有一个类似的实体
symfony/form
我们不需要填充// src/Entity/myEntity.php
namespace App\Entity;
...
class myEntity
{
private $id;
private $name; // string, required
private $canDrive; // bool, not required (default=false)
private $driveLicense; // string, not required (default = null)
public function __construct()
{
$this->canDrive = false;
}
// getters and setters
}
(因为该属性不是必需的),但是如果$driveLicense
从$canDrive
更改为false
,则现在{{1} } 必须具有一个值。
true
与$driveLicense
相关。
要为此构建一个表单并在FormType上正确验证$driveLicense
(最佳实践),我们需要构建一个CustomConstraintValidator。
$canDrive
翻译器文件-可选
$driveLicense
验证者
// src/Validator/Constraints/CanDrive.php
namespace App\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
class CanDrive extends Constraint
{
public $message = 'invalid_candrive_args'; // I like translators :D
}
表单myEntityType
//src/translators/validators.en.yaml //
invalid_candrive_args: When "{{ candrivelabel }} " field is checked you must fill "{{ drivelicenselabel }}"
现在,当使用myEntityType表单的调用// src/Validator/Constraints/CanDriveValidator.php
namespace App\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
class CanDriveValidator extends ConstraintValidator
{
/**
* Checks if the passed value is valid.
*
* @param mixed $value The value that should be validated
* @param Constraint $constraint The constraint for the validation
*/
public function validate($value, Constraint $constraint)
{
$canDriveField = $this->context->getObject(); // the Field using this validator
$form = $canDriveField->getParent(); // the formType where the Field reside
$myEntity = $form->getData(); // The Entity mapped by formType
if ($myEntity->getCanDrive() == true && $myEntity->getDriveLicense() == null) {
$this->context->buildViolation($constraint->message)
->setParameter('{{ candrivelabel }}', 'Can Drive')
->setParameter('{{ drivelicenselabel }}', 'Drive License')
->addViolation();
}
}
}
方法并且检查 canDrive 字段并且 driveLicense 为空白时,将对canDrive字段引发违反。如果 canDrive 设置为false(未选中,未提交),则不会发生任何事情,即使 driveLicense 为空白,表单也将有效。
答案 3 :(得分:0)
答案很简单。写:
this->context->getRoot()->getData()
你有对象。