仅接收ZF2中的已修改字段

时间:2013-11-08 12:12:31

标签: php forms zend-framework2 extjs4

前端

Ext Js 4.2

Ext.data.writer.Writer的配置,writeAllFields设置为false时仅发送已修改的字段。 http://docs.sencha.com/extjs/4.2.1/#!/api/Ext.data.writer.Writer-cfg-writeAllFields

后端

Zend Framework 2.2

class SomeForm extends Zend\Form\Form {

    public function __construct($name = null, $options = array()) {
        parent::__construct($name, $options);

        $this->add(array(
            'name' => 'id',
            'type' => 'Zend\Form\Element\Hidden',
        ));

        $this->add(array(
            'name' => 'field_foo',
            'type' => 'Zend\Form\Element\Text',
        ));

        $this->add(array(
            'name' => 'field_bar',
            'type' => 'Zend\Form\Element\Text',
        ));        
    }
}

class SomeFormFilter extends Zend\InputFilter\InputFilter {

    public function __construct() {    
        $this->add(array(
            'name' => 'id',
            'required' => false
        ));

        $this->add(array(
            'name' => 'field_foo',
            'required' => true
        ));

        $this->add(array(
            'name' => 'field_bar',
            'required' => true
        ));
    }
}

在服务层,我们检查发送的数据是否有效

http://framework.zend.com/apidoc/2.2/classes/Zend.Form.Form.html#isValid

$form->isValid()

当后端收到

  1. 修改了所有字段:请求方法:PUT表单数据: {“field_foo”:“value string”,“field_bar”:“value string”,“id”:“22”} 那是正确的.field_foo和field_bar是必需的并且返回true。

  2. 仅修改,field_foo:请求方法:PUT表单数据:     {“field_foo”:“value string”,“id”:“22”} isValid()返回false     因为field_bar是必需的。

  3. 解决方案的哪种方式?

    删除字段? http://framework.zend.com/apidoc/2.2/classes/Zend.Form.Form.html#remove

    或者

2 个答案:

答案 0 :(得分:0)

为更新操作提供不同的inputFilter

writeAllFields设置为true并发送并保存所有值

答案 1 :(得分:0)

仅验证感兴趣的字段。在这种情况下,只有请求中发送的字段。

public function foo($rawData) // array('id' => 1, 'field_foo' => 'value')
{ 
    $form = $this->getForm(); // @var Zend\Form\Form

    $group = array_keys($rawData); // array(0 => 'id', 1 => 'field_foo')
    $form->setValidationGroup($group);

    $form->setData($rawData); // array('id' => 1, 'field_foo' => 'value')

    $isValid = $form->isValid();

    // some code       
}

https://framework.zend.com/manual/2.0/en/modules/zend.form.quick-start.html#validation-groups

相关问题