Symfony1 表单验证已上载XML文件的内容

Symfony1 表单验证已上载XML文件的内容,symfony1,symfony-1.4,Symfony1,Symfony 1.4,我有一个上传XML文件的表单。提交表单后,我必须检查XML文件中一对标记的内容。如果标签的内容与预期的不同,则应在表单旁边显示错误 我不知道如何组织这些代码,有什么帮助吗 标记:预验证、后验证您有几个地方可以执行此检查: 在创建窗体的操作中 在表单中使用自定义验证器 在模型类中(实际上不推荐使用wich…) 我更喜欢自定义验证器,因为如果必须在其他地方重新使用表单,就不必重新实现检查xml的逻辑 因此,在sfForm类中,向文件小部件添加自定义验证器: class MyForm extend

我有一个上传XML文件的表单。提交表单后,我必须检查XML文件中一对标记的内容。如果标签的内容与预期的不同,则应在表单旁边显示错误

我不知道如何组织这些代码,有什么帮助吗


标记:预验证、后验证您有几个地方可以执行此检查:

  • 在创建窗体的操作中
  • 在表单中使用自定义验证器
  • 在模型类中(实际上不推荐使用wich…)
我更喜欢自定义验证器,因为如果必须在其他地方重新使用表单,就不必重新实现检查xml的逻辑

因此,在sfForm类中,向文件小部件添加自定义验证器:

class MyForm extends sfForm
{
  public function configure()
  {
    // .. other widget / validator configuration

    $this->validatorSchema['file'] = new customXmlFileValidator(array(
      'required'  => true,
    ));
在新的验证器中,位于
/lib/validator/customXmlFileValidator.class.php

// you extend sfValidatorFile, so you keep the basic file validator
class customXmlFileValidator extends sfValidatorFile
{
  protected function configure($options = array(), $messages = array())
  {
    parent::configure($options, $messages);

    // define a custom message for the xml validation
    $this->addMessage('xml_invalid', 'The XML is not valid');
  }

  protected function doClean($value)
  {
    // it returns a sfValidatedFile object
    $value = parent::doClean($value);

    // load xml file
    $doc = new DOMDocument();
    $doc->load($this->getTempName());

    // do what ever you want with the dom to validate your xml
    $xpath = new DOMXPath($doc);
    $xpath->query('/my/xpath');

    // if validation failed, throw an error
    if (true !== $result)
    {
      throw new sfValidatorError($this, 'xml_invalid');
    }

    // otherwise return the sfValidatedFile object from the extended class
    return $value;
  }
}

不要忘了清除缓存
php symfony cc
,它应该是正常的。

您有几个地方可以执行此检查:

  • 在创建窗体的操作中
  • 在表单中使用自定义验证器
  • 在模型类中(实际上不推荐使用wich…)
我更喜欢自定义验证器,因为如果必须在其他地方重新使用表单,就不必重新实现检查xml的逻辑

因此,在sfForm类中,向文件小部件添加自定义验证器:

class MyForm extends sfForm
{
  public function configure()
  {
    // .. other widget / validator configuration

    $this->validatorSchema['file'] = new customXmlFileValidator(array(
      'required'  => true,
    ));
在新的验证器中,位于
/lib/validator/customXmlFileValidator.class.php

// you extend sfValidatorFile, so you keep the basic file validator
class customXmlFileValidator extends sfValidatorFile
{
  protected function configure($options = array(), $messages = array())
  {
    parent::configure($options, $messages);

    // define a custom message for the xml validation
    $this->addMessage('xml_invalid', 'The XML is not valid');
  }

  protected function doClean($value)
  {
    // it returns a sfValidatedFile object
    $value = parent::doClean($value);

    // load xml file
    $doc = new DOMDocument();
    $doc->load($this->getTempName());

    // do what ever you want with the dom to validate your xml
    $xpath = new DOMXPath($doc);
    $xpath->query('/my/xpath');

    // if validation failed, throw an error
    if (true !== $result)
    {
      throw new sfValidatorError($this, 'xml_invalid');
    }

    // otherwise return the sfValidatedFile object from the extended class
    return $value;
  }
}

不要忘记清除缓存
php symfony cc
,它应该可以。

或者您可以根据架构描述进行清除。@1ed当然可以。我没有深入讨论XML验证,我只是想说明如何使用验证器进行验证,或者您可以根据架构描述进行验证。@1ed当然可以。我没有深入讨论XML验证,我只是想展示如何使用验证器来完成验证。您添加到问题中的那些“标记”是什么?您添加到问题中的那些“标记”是什么?