Kie*_*ham 4 symfony1 symfony-1.4 symfony-forms
我正在尝试创建一个表单,根据html表单字段中的select选项更改字段的验证.
例如:如果用户从下拉字段"options"中选择选项1,我希望字段"metric"验证为sfValidatorInteger.如果用户从字段"options"中选择选项2,我希望字段"metric"验证为sfValidatorEmail等.
所以在public函数内部configure(){我有switch语句来捕获"options"的值,并根据"options"返回的值创建验证器.
1.)如何捕获"选项"的值?我试过了:
$this->getObject()->options
$this->getTaintedValues()
Run Code Online (Sandbox Code Playgroud)
目前唯一适用于我的是但它并不是真正的MVC:
$params = sfcontext::getInstance()->getRequest()->getParameter('options');
Run Code Online (Sandbox Code Playgroud)
2.)一旦我捕获了该信息,我如何将"度量"的值分配给不同的字段?("metric"不是db中的真实列).所以我需要将"metric"的值分配给不同的字段,例如"email","age"......目前我正在这样的post验证器处理这个,只是想知道我是否可以在configure中分配值( ):
$this->validatorSchema->setPostValidator(new sfValidatorCallback(array('callback' => array($this, 'checkMetric'))));
public function checkMetric($validator, $values) {
}
Run Code Online (Sandbox Code Playgroud)
谢谢!
您想使用帖子验证器.尝试在表单中执行以下操作:
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;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2903 次 |
| 最近记录: |