Symonfy 1.4 动态验证可能吗?

发布于 2024-09-16 09:34:36 字数 894 浏览 6 评论 0原文

我正在尝试创建一个表单,根据 html 表单字段中的选择选项来更改字段的验证。

例如:如果用户从下拉字段“选项”中选择选项 1,我希望字段“指标”验证为 sfValidatorInteger。如果用户从字段“options”中选择选项 2,我希望字段“metric”验证为 sfValidatorEmail 等。

因此,在 public 函数 configure() { 中,我有 switch 语句来捕获“options”的值,并创建验证器基于从“选项”返回的值。

1.) 如何获取“选项”的价值?我已经尝试过了:

$this->getObject()->options
$this->getTaintedValues()

目前唯一对我有用的是,但它并不是真正的 MVC:

$params = sfcontext::getInstance()->getRequest()->getParameter('options');

2.)一旦我捕获了该信息,如何将“metric”的值分配给不同的字段? (“metric”不是数据库中的真实列)。所以我需要将“metric”的值分配给不同的字段,例如“email”,“age”......目前我正在像这样的帖子验证器中处理这个问题,只是想知道我是否可以在配置中分配值( ):

$this->validatorSchema->setPostValidator(new sfValidatorCallback(array('callback' => array($this, 'checkMetric'))));

public function checkMetric($validator, $values) {

}

谢谢!

I'm trying to create a form that change the validation of a field based on the select option from the html form field.

Ex: if user select a option 1 from drop down field "options", I want the field "metric" to validate as sfValidatorInteger. If user select option 2 from field "options", I want the field "metric" to validate as sfValidatorEmail, etc.

So inside the public function configure() { I have the switch statement to capture the value of "options", and create the validator based on that value returned from the "options".

1.) How do I capture the value of "options" ? I've tried:

$this->getObject()->options
$this->getTaintedValues()

The only thing that's currently working for me is but it's not really MVC:

$params = sfcontext::getInstance()->getRequest()->getParameter('options');

2.) Once I've captured that information, how can I assign the value of "metric" to a different field? ("metric" is not a real column in db). So I need to assign the value of "metric" to different field such as "email", "age" ... Currently I'm handling this at the post validator like this, just wondering if I can assign value within the configure():

$this->validatorSchema->setPostValidator(new sfValidatorCallback(array('callback' => array($this, 'checkMetric'))));

public function checkMetric($validator, $values) {

}

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

橙味迷妹 2024-09-23 09:34:36

您想使用后验证器。尝试在您的表单中执行以下操作:

public function configure()
{
  $choices = array('email', 'integer');
  $this->setWidget('option', new sfWidgetFormChoice(array('choices' => $choices))); //option determines how field "dynamic_validation" is validated
  $this->setValidator('option', new sfValidatorChoice(array('choices' => array_keys($choices)));
  $this->setValidator('dynamic_validation', new sfValidatorPass()); //we're doing validation in the post validator
  $this->mergePostValidator(new sfValidatorCallback(array(
    'callback' => array($this, 'postValidatorCallback')
  )));
}

public function postValidatorCallback($validator, $values, $arguments)
{
   if ($values['option'] == 'email')
   {
     $validator = new sfValidatorEmail();
   }
   else //we know it's one of email or integer at this point because it was already validated
   {
     $validator = new sfValidatorInteger();
   }
   $values['dynamic_validation'] = $validator->clean($values['dynamic_validation']); //clean will throw exception if not valid
   return $values;
}

You want to use a post validator. Try doing something like this in your form:

public function configure()
{
  $choices = array('email', 'integer');
  $this->setWidget('option', new sfWidgetFormChoice(array('choices' => $choices))); //option determines how field "dynamic_validation" is validated
  $this->setValidator('option', new sfValidatorChoice(array('choices' => array_keys($choices)));
  $this->setValidator('dynamic_validation', new sfValidatorPass()); //we're doing validation in the post validator
  $this->mergePostValidator(new sfValidatorCallback(array(
    'callback' => array($this, 'postValidatorCallback')
  )));
}

public function postValidatorCallback($validator, $values, $arguments)
{
   if ($values['option'] == 'email')
   {
     $validator = new sfValidatorEmail();
   }
   else //we know it's one of email or integer at this point because it was already validated
   {
     $validator = new sfValidatorInteger();
   }
   $values['dynamic_validation'] = $validator->clean($values['dynamic_validation']); //clean will throw exception if not valid
   return $values;
}
无可置疑 2024-09-23 09:34:36

1) 在后验证器中,可以通过 使用 $values 参数。只需使用 $values['options'] 就可以了...或者您想从代码的另一部分访问这些值吗?我认为,一旦您的表单绑定到一个对象, $this->getObject()->widgetSchema['options'] 也应该起作用。

2) configure() 方法在表单构造函数的末尾调用,因此值尚未绑定或可访问,除非您使用数据库中的对象初始化表单(不需要任何验证)。但如果你想从 $_POST 初始化你的表单,恕我直言,后验证器绝对是正确的选择。

1) In a post validator, values can be accessed by using the $values parameter. Just use $values['options'] and it should be fine... or did you want to access this values from another part of you code? $this->getObject()->widgetSchema['options'] should work too I think, once your form is bound to an object.

2) The configure() method is called at the end of the form constructor, so values are not bound nor accessible yet, unless you are initializing your form with an object from the db (which does not require any validation). But if you want to initialize your form from $_POST, a post validator is definitely the way to go IMHO.

丢了幸福的猪 2024-09-23 09:34:36

我通过抛出 sfValidatorErrorSchema 而不是 sfValidatorError 来使验证错误出现在字段旁边。

$values['dynamic_validation'] = $validator->clean($values['dynamic_validation']);

…变成…

try
{
    $values['dynamic_validation'] = $validator->clean($values['dynamic_validation']);
}
catch(sfValidatorError $e)
{
    $this->getErrorSchema()->addError($e, 'dynamic_validation');
    throw $this->getErrorSchema();
}

不确定这是否是获得此结果的最佳方法,但目前它似乎对我有用。

I got the validation error to appear alongside the field by throwing a sfValidatorErrorSchema instead of a sfValidatorError.

$values['dynamic_validation'] = $validator->clean($values['dynamic_validation']);

…becomes…

try
{
    $values['dynamic_validation'] = $validator->clean($values['dynamic_validation']);
}
catch(sfValidatorError $e)
{
    $this->getErrorSchema()->addError($e, 'dynamic_validation');
    throw $this->getErrorSchema();
}

Not sure if this is the best way to get this result, but it seems to be working for me at the moment.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文