Symfony 表单或普通 HTML 表单

发布于 2024-09-25 05:32:19 字数 171 浏览 0 评论 0 原文

我正在使用 Symfony 1.4 创建项目,我需要根据来自数据库的问题集类型创建动态表单。我在项目的其余部分中使用了 Symfony 表单,但在本例中使用 symfony 表单似乎很困难,因为我需要动态表单。 在 symfony 项目中使用普通的 HTML 表单是否安全,或者建议使用 Symfony 表单。所以需要你的帮助。

I am using Symfony 1.4 to create project, and i need to create dynamic forms depending upon the question set type coming from database. i have used Symfony forms in rest of my project, but in this case using symfony forms seems to be tough, as i need dynamic form.
can it be safe to use normal HTML forms..in symfony project, or it is advisable to use Symfony forms. so need your help.

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

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

发布评论

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

评论(2

鸢与 2024-10-02 05:32:19

您可以使用 html 表单,但它会绕过 symfony 的表单验证系统。

您仍然可以通过创建输入小部件并将其添加到当前表单或操作内的新表单来构建动态表单。然后,您可以在模板中回显表单,动态生成的字段也将成为表单的一部分。

如果您从 lib/forms 中的 MyForm.class.php 开始,请确保添加:

$this->validatorSchema->setOption('allow_extra_fields', true);

否则,您将自动收到验证错误。如果您想在操作中向表单添加字段,您可以执行以下操作:

$this->form = new MyForm();
$widgetSchema = $this->form->getWidgetSchema();
$widgetSchema['add_field'] = new sfWidgetFormInputText();

当您回显表单时,“add_field”输入将添加到其中。

You can use html forms, but it would bypass symfony's form validation system.

You can still build dynamic forms by creating and adding input widgets to the current form, or a new form inside an action. You can then echo the form in the template and the dynamically generated fields will be part of the form as well.

If you start with a MyForm.class.php in the lib/forms, make sure to add:

$this->validatorSchema->setOption('allow_extra_fields', true);

Otherwise, you will automatically get validation errors. If you want to add fields to a form in an action you would do something like this:

$this->form = new MyForm();
$widgetSchema = $this->form->getWidgetSchema();
$widgetSchema['add_field'] = new sfWidgetFormInputText();

When you echo your form the 'add_field' input will be added to it.

∝单色的世界 2024-10-02 05:32:19

获得有关您正在做的事情的更多信息会有所帮助,但这里是 Symfony 中表单动态化的一种方法。此代码根据问题的“类型”动态地为调查创建小部件和验证器:

class SurveyAnswerForm extends BaseSurveyAnswerForm
{
  public function configure()
  {
    $question = $this->object->Question;

    $method = sprintf('createWidgetAndValidatorFor%sInputType', $question->type);
    $this->$method($question);
    $this->getWidget('value')->setOption('label', $question->question);
    $this->getValidator('value')->setOption('required', $question->required);
  }

  protected function createWidgetAndValidatorForTextFieldInputType(Question $question)
  {
    $this->setWidget('value', new sfWidgetFormInputText());
    $this->setValidator('value', new sfValidatorString());
  }

  protected function createWidgetAndValidatorForTextAreaInputType(Question $question)
  {
    $this->setWidget('value', new wfWidgetFormTextareaAutosize());
    $this->setValidator('value', new sfValidatorString());
  }

  //etc. for as many types as you require
}

注意:虽然此答案是来自我的项目之一的代码,但它受到 此答案位于 SymfonyExperts 上。

It would help to have more information about what you're doing, but here's one way in which forms can be dynamic in Symfony. This code creates widgets and validators for a survey dynamically based on the "type" of a question:

class SurveyAnswerForm extends BaseSurveyAnswerForm
{
  public function configure()
  {
    $question = $this->object->Question;

    $method = sprintf('createWidgetAndValidatorFor%sInputType', $question->type);
    $this->$method($question);
    $this->getWidget('value')->setOption('label', $question->question);
    $this->getValidator('value')->setOption('required', $question->required);
  }

  protected function createWidgetAndValidatorForTextFieldInputType(Question $question)
  {
    $this->setWidget('value', new sfWidgetFormInputText());
    $this->setValidator('value', new sfValidatorString());
  }

  protected function createWidgetAndValidatorForTextAreaInputType(Question $question)
  {
    $this->setWidget('value', new wfWidgetFormTextareaAutosize());
    $this->setValidator('value', new sfValidatorString());
  }

  //etc. for as many types as you require
}

Note: while this answer is code from one of my projects, it was heavily influenced by this answer over on SymfonyExperts.

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