doudeng3008 2009-08-30 15:52
浏览 26
已采纳

Zend框架形式非理性行为

Let's start this off with a short code snippet I will use to demonstrate my opinion:

$title = new Zend_Form_Element_Text('title', array(
    'label' => 'Title',
    'required' => false,
    'filters' => array(
        'StringTrim',
        'HtmlEntities'
    ),
    'validators' => array(
        array('StringLength', false, array(3, 100))
    ),
));

This important line is:

'required' => false,

Which means that the input field is not required and you can submit the form without filling it. However, this also means that any filters and validators won't apply to it if you choose to fill in this field.

Common sense tells me that is an irrational behavior. The way I understand the word 'required' in relation with HTML input fields: an input field that is not required should return NULL if it is not filled in but if user decides to fill it both filters and validators should apply to it. That's what makes sense to me. Do you agree with me or is my common sense not so common?

Now more practical question, because this is how Zend_Form behaves, how can I achieve not required fields which would work as I described above (if nothing is typed in by user it returns NULL otherwise filters and validators normally apply).

  • 写回答

2条回答 默认 最新

  • dongxun2903 2009-08-30 18:20
    关注

    Not really a complete answer to your question, but since comments don't have syntax formatting; here's a filter you can use to make your field values null if empty.

    class My_Filter_NullIfEmpty implements Zend_Filter_Interface
    {
        public function filter( $value )
        {
              // maybe you need to expand the conditions here
            if( 0 == strlen( $value ) )
            {
                return null;
            }
            return $value;
        }
    }
    

    About the required part: I'm not sure really. You could try to search the ZF mailinglists on Nabble:

    http://www.nabble.com/Zend-Framework-Community-f16154.html

    Or subscribe to their mailinglist, and ask them the question. Either through Nabble, or directly via the addresses on framework.zend.com: http://tinyurl.com/y4f9lz

    Edit: Ok, so now I've done some tests myself, cause what you said all sounded counter intuitive to me. Your example works fine with me. This is what I've used:

    <?php
    
    class Form extends Zend_Form
    {
        public function init()
        {
    
            $title = new Zend_Form_Element_Text('title', array(
                    'label' => 'Title',
                    'required' => false,
                    'filters' => array(
                        'StringTrim',
                        'HtmlEntities',
                        'NullIfEmpty' // be sure this one is available
                    ),
                    'validators' => array(
                        array('StringLength', false, array(3, 100))
                    ),
                ));
    
            $this->addElement( $title );
        }
    }
    
    $form = new Form();
    
    $postValues = array( 'title' => '' ); // or
    $postValues = array( 'title' => '        ' ); // or
    $postValues = array( 'title' => 'ab' ); // or
    $postValues = array( 'title' => ' ab ' ); // or
    $postValues = array( 'title' => '<abc>' ); // all work perfectly fine with me
    
    // validate the form (which automatically sets the values in the form object)
    if( $form->isValid( $postValues ) )
    {
        // retrieve the relevant value
        var_dump( $form->getValue( 'title' ) );
    }
    else
    {
        echo 'form invalid';
    }
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探