symfony 1.4 动态添加表单小部件而不使用嵌入表单

发布于 2024-10-17 07:09:15 字数 163 浏览 2 评论 0原文

我有一个 symfony 表单,我需要通过 jquery 动态添加几个字段(小部件)。假设我有一个用户表单,我希望用户能够设置多个电子邮件地址,

“电子邮件”只是一个简单的小部件,而不是一个表单,因此使用嵌入表单,就像网上提供的大多数教程一样,没有意义。

对此最好的方法是什么?

I have a symfony form and i need to add a couple of fields(widgets) dynamicly via jquery. Let´s say i have an User form and I want the user to be able to set multiple emails addresses,

The "email" is just a simple widget and not a form so using embeded forms ,like most of the tutorials avaliable in the net, doesn´t make sense.

What is the best approach to this?

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

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

发布评论

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

评论(1

靖瑶 2024-10-24 07:09:15

建议的方法是使用嵌入式表单,但我发现这对您没有用(假设表单输入是通过 Javascript 添加的)。

我建议的一个解决方案(同时渴望看到其他人如何解决这个问题)是从小部件架构中删除电子邮件字段并仅添加它 到您的验证模式(使用自定义回调)。

未经测试,但作为指导。

class YourForm extends sfForm {

  public function configure() {
    $this->setValidators(array(
      'email' => new sfValidatorCallback(array(
        'callback' => array($this, 'my_email_validator')
      ))
    ));
  }

  public function my_email_validator($validator, $value) {
    $email_validator = new sfValidatorEmail();
    try {
      foreach($value as $email) { // multiple fields with the same name
        $email_validator->clean($email); // throws exception when not valid
      }
    }
    catch(sfValidatorError $e) {
      // $email is invalid - notify user or whatever
      // you could also rethrow a sfValidatorError so that the form->isValid
      // would return false
    }
  }
}

您的模板中将包含该字段(手动是的)。

<input type="text" name="email[]" />
<input type="text" name="email[]" />
<input type="text" name="email[]" />
<input type="text" name="email[]" />

我仍然希望看到其他人的建议,因为这对我来说也看起来很黑客。

The suggested way would be to use the embedded form, but I see how that could not be useful to you (given the form inputs are added via Javascript).

One solution I suggest (while eager to see how other people would solve this) is to remove the email field from the widget schema and only add it to your validation schema (with custom callback).

Not tested but something along the lines as a guide.

class YourForm extends sfForm {

  public function configure() {
    $this->setValidators(array(
      'email' => new sfValidatorCallback(array(
        'callback' => array($this, 'my_email_validator')
      ))
    ));
  }

  public function my_email_validator($validator, $value) {
    $email_validator = new sfValidatorEmail();
    try {
      foreach($value as $email) { // multiple fields with the same name
        $email_validator->clean($email); // throws exception when not valid
      }
    }
    catch(sfValidatorError $e) {
      // $email is invalid - notify user or whatever
      // you could also rethrow a sfValidatorError so that the form->isValid
      // would return false
    }
  }
}

And you would have in your template the field (manually yes).

<input type="text" name="email[]" />
<input type="text" name="email[]" />
<input type="text" name="email[]" />
<input type="text" name="email[]" />

I would still like to see others suggestion, because this looks hackish to me as well.

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