有没有办法从 Zend_Form 生成视图? (只读)

发布于 2024-08-22 01:22:12 字数 162 浏览 13 评论 0原文

我想知道在处理 CRUD 时是否有一种简单的方法可以从表单对象生成视图。

我的意思是,当我们有这些选择时:查看|编辑 |删除 我希望我的“查看”选项像“编辑”选项一样,但没有表单元素,只有值。

这将最大限度地减少创建这些视图所花费的时间。

有人知道类似的事情吗?

I was wondering if is there an easy way to generate views from form objects when dealing with CRUDs.

I mean, when we have these options: VIEW | EDIT | DELETE
I want my VIEW option like EDIT option, but without form elements, just the values.

This will minimize so much the time spent to create these views.

Someone knowks something like that?

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

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

发布评论

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

评论(5

冷情妓 2024-08-29 01:22:12

在我的上一个项目中,我也遇到了这个困境。我的解决方案可能不是最优雅的,但它完成了工作。请注意,我使用表单视图脚本装饰器而不是完整的装饰器生成的元素。但我认为您可以调整此示例以使用装饰器。我所展示的是一个非常基本的示例,旨在为您提供一个总体概念。这就是我所做的:

class Cms_Form_Page extends Zend_Form
{
  const FOR_CREATE = 'forCreate';
  const FOR_READ   = 'forRead';
  const FOR_UPDATE = 'forUpdate';
  const FOR_DELETE = 'forDelete';

  protected $_name = 'page';

  private $_for;

  private $_viewScripts = array(
    self::FOR_CREATE => 'page-manager/partials/form-page-create.phtml',
    self::FOR_READ => 'page-manager/partials/form-page-read.phtml',
    self::FOR_UPDATE => 'page-manager/partials/form-page-update.phtml',
    self::FOR_DELETE => 'page-manager/partials/form-page-delete.phtml'
  );

  public function __construct( $for = self::FOR_CREATE, $options = null )
  {
    $this->_for = $for;

    parent::__construct( $options );
  }

  public function init()
  {
    $this->setName( $this->_name )
        ->setAttribs( array( 'accept-charset' => 'utf-8' ) )
        ->setDecorators( array(
            'PrepareElements',
            array( 'ViewScript', array( 'viewScript' => $this->_viewScripts[ $this->_for ] ) ),
            'Form'
        ) );

    $elements = array();

    swith( $this->_for )
    {
      case self::FOR_CREATE:
        $title = new Zend_Form_Element_Text( 'title' );
        $elements[] = $title;
        break
      case self::FOR_READ:
        $id = new Zend_Form_Element_Hidden( 'id' );
        $elements[] = $id;
        break;
      case self::FOR_UPDATE:
        $id = new Zend_Form_Element_Hidden( 'id' );
        $elements[] = $id;
        $title = new Zend_Form_Element_Text( 'title' );
        $elements[] = $title;
      break;
        case self::FOR_DELETE:
        $id = new Zend_Form_Element_Hidden( 'id' );
        $elements[] = $id;
        break;
      default:
        throw new Exception( 'invalid Form type' );
    }

    $submit = new Zend_Form_Element_Button( 'submit' );
    $elements[] = $submit;

    $this->addElements( $elements );
  }
}

所以,基本上,我将类常量之一传递给它的构造函数。根据该值,我确定表单需要哪些元素以及应如何呈现这些元素。

例如,对于创建,您可以有一个选择下拉表单字段,您可以在其中选择一个区域设置,对于删除,这将是一个隐藏字段(顺便说一句,在我的示例中未显示)。

希望这能给您一些想法。

PS:
在选定的视图脚本之一中,您可以简单地显示元素的值(也可以渲染隐藏元素),例如:

<?
  $form = $this->element;
?>

... some html

// let's presume id and locale are hidden form fields for current form type 
// (Cms_Form_Page::FOR_UPDATE for instance)
<?= $form->id->renderViewHelper(); ?>
<?= $form->locale->renderViewHelper(); ?>

// and here we simply output the current locale value
// of course, you should have populated the values in the form somewhere first
<dt>Current locale:</dt>
<dd><?= $form->locale->getValue(); ?></dd>

...etc

所以,我认为您最好使用表单的视图脚本装饰器,或者您可以滚动您自己的表单元素装饰器来呈现隐藏字段(如果需要)并简单地在某些 html 标记中显示它的值。

In my last project I had this dilemma too. My solution may not be the most elegant, but it did the job. Mind you, I use a Form viewscript decorator in stead of a full decorator generated elements. But you could adjust this example to use decorators I presume. What I'm showing is a very basic example, to give you a general idea. Here's what I did:

class Cms_Form_Page extends Zend_Form
{
  const FOR_CREATE = 'forCreate';
  const FOR_READ   = 'forRead';
  const FOR_UPDATE = 'forUpdate';
  const FOR_DELETE = 'forDelete';

  protected $_name = 'page';

  private $_for;

  private $_viewScripts = array(
    self::FOR_CREATE => 'page-manager/partials/form-page-create.phtml',
    self::FOR_READ => 'page-manager/partials/form-page-read.phtml',
    self::FOR_UPDATE => 'page-manager/partials/form-page-update.phtml',
    self::FOR_DELETE => 'page-manager/partials/form-page-delete.phtml'
  );

  public function __construct( $for = self::FOR_CREATE, $options = null )
  {
    $this->_for = $for;

    parent::__construct( $options );
  }

  public function init()
  {
    $this->setName( $this->_name )
        ->setAttribs( array( 'accept-charset' => 'utf-8' ) )
        ->setDecorators( array(
            'PrepareElements',
            array( 'ViewScript', array( 'viewScript' => $this->_viewScripts[ $this->_for ] ) ),
            'Form'
        ) );

    $elements = array();

    swith( $this->_for )
    {
      case self::FOR_CREATE:
        $title = new Zend_Form_Element_Text( 'title' );
        $elements[] = $title;
        break
      case self::FOR_READ:
        $id = new Zend_Form_Element_Hidden( 'id' );
        $elements[] = $id;
        break;
      case self::FOR_UPDATE:
        $id = new Zend_Form_Element_Hidden( 'id' );
        $elements[] = $id;
        $title = new Zend_Form_Element_Text( 'title' );
        $elements[] = $title;
      break;
        case self::FOR_DELETE:
        $id = new Zend_Form_Element_Hidden( 'id' );
        $elements[] = $id;
        break;
      default:
        throw new Exception( 'invalid Form type' );
    }

    $submit = new Zend_Form_Element_Button( 'submit' );
    $elements[] = $submit;

    $this->addElements( $elements );
  }
}

So, basically, I pass one of the class constants to it's constructor. And based on that value, I determine what elements are needed for the form, and how the elements should be rendered.

For instance, for create you could have a select dropdown form field where you would choose a Locale, where for delete this would be a hidden field (not shown in my example btw).

Hope this has given you some ideas.

PS:
In one of the selected viewscripts you could then simply show the value of an element (along with rendering the hidden element too), with something like:

<?
  $form = $this->element;
?>

... some html

// let's presume id and locale are hidden form fields for current form type 
// (Cms_Form_Page::FOR_UPDATE for instance)
<?= $form->id->renderViewHelper(); ?>
<?= $form->locale->renderViewHelper(); ?>

// and here we simply output the current locale value
// of course, you should have populated the values in the form somewhere first
<dt>Current locale:</dt>
<dd><?= $form->locale->getValue(); ?></dd>

...etc

So, I think you'ld be best of with using viewscript decorators for the form, or you could roll your own form element decorator that renders the hidden field (if neccesary) and simply shows it's value in some html tag.

放飞的风筝 2024-08-29 01:22:12

来自 Nabble 的 Hector,

class Default_View_Helper_FormView extends Zend_View_Helper_Abstract
{
    public function formView(Zend_Form $form)
    {
        $html = "<dl>";
        foreach ($form->getElements as $element) {
            $html .= "<dt>{$element->getLabel()}</dt>";
            $html .= "<dd>{$element->getValue()}</dd>";
        }
        $html .= "</dl>";
        return $html;
    }
}

Hector from Nabble, show me this, which seems to be the best way:

class Default_View_Helper_FormView extends Zend_View_Helper_Abstract
{
    public function formView(Zend_Form $form)
    {
        $html = "<dl>";
        foreach ($form->getElements as $element) {
            $html .= "<dt>{$element->getLabel()}</dt>";
            $html .= "<dd>{$element->getValue()}</dd>";
        }
        $html .= "</dl>";
        return $html;
    }
}
醉生梦死 2024-08-29 01:22:12

我不确定我是否理解,但我认为对于视图选项,您可以从模型中获取数据。无需通过 Zend_Form 访问它们。

但是,如果您希望使表单只读,则可以向元素添加 readonly (setAttrib('readonly', 'readonly')) 属性。

I'm not sure if I understand, but I think that for the view option you can just fetch the data from your model. No need to access them through Zend_Form.

But if you want the make your form read-only, you can add readonly (setAttrib('readonly', 'readonly')) attribute to your elements.

叫思念不要吵 2024-08-29 01:22:12

对已接受的答案做了一些小的补充,以涵盖可能是特殊情况的常见元素:

class Default_View_Helper_FormView extends Zend_View_Helper_Abstract
{
  public function formView( Zend_Form $form )
  {
    $html = '<dl>';

    foreach ( $form->getElements() as $element ) {

      if( $element instanceof Zend_Form_Element_Submit ) {
        continue;
      }

      $html .= '<dt>' . $element->getLabel() . '</dt>';

      $value = $element->getValue();

      if( $element instanceof Zend_Form_Element_Checkbox ) {
        $value = ($value) ? 'Yes' : 'No';
      }
      else if( $element instanceof Zend_Form_Element_Select ) {
        $value = $element->getMultiOption($value);
      }

      $html .= '<dd>' . $value . '</dd>';
    }

    $html .= '</dl>';

    return $html;
  }
}

Made a couple minor additions to the accepted answer to cover common elements that may be special cases:

class Default_View_Helper_FormView extends Zend_View_Helper_Abstract
{
  public function formView( Zend_Form $form )
  {
    $html = '<dl>';

    foreach ( $form->getElements() as $element ) {

      if( $element instanceof Zend_Form_Element_Submit ) {
        continue;
      }

      $html .= '<dt>' . $element->getLabel() . '</dt>';

      $value = $element->getValue();

      if( $element instanceof Zend_Form_Element_Checkbox ) {
        $value = ($value) ? 'Yes' : 'No';
      }
      else if( $element instanceof Zend_Form_Element_Select ) {
        $value = $element->getMultiOption($value);
      }

      $html .= '<dd>' . $value . '</dd>';
    }

    $html .= '</dl>';

    return $html;
  }
}
独夜无伴 2024-08-29 01:22:12

接受的答案的唯一问题是您正在创建所有元素,然后忽略它们。

使用 fireeyedboy 的答案中的控制逻辑,您可以将所有元素切换到执行相同操作的 Zend_View_Helper_FormNote

只是取决于优化是否重要。

The only problem with the accepted answer is that you're creating all the elements and then ignoring them.

Using the control logic from fireeyedboy's answer, you could instead switch all the elements to Zend_View_Helper_FormNote which does the same thing.

Just depends on if the optimization matters.

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