Symonfy 1.4 动态验证可能吗?
我正在尝试创建一个表单,根据 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您想使用后验证器。尝试在您的表单中执行以下操作:
You want to use a post validator. Try doing something like this in your form:
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.
我通过抛出
sfValidatorErrorSchema
而不是sfValidatorError
来使验证错误出现在字段旁边。…变成…
不确定这是否是获得此结果的最佳方法,但目前它似乎对我有用。
I got the validation error to appear alongside the field by throwing a
sfValidatorErrorSchema
instead of asfValidatorError
.…becomes…
Not sure if this is the best way to get this result, but it seems to be working for me at the moment.