如何在zend框架中添加自定义属性2使用zend from


How add custom attribute in zend framework 2 using zend from

我想在zend框架项目中使用angularJS,在这个项目中,表单是使用zend表单生成的。我怎么能添加角指令,如"ng-model"在表单元素,但每当我试图添加这个自定义属性在zend-form元素(输入,选择等)在视图我没有得到这个属性----

这是我的lead form

类LeadForm扩展Form {

public function __construct() {
    parent::__construct('lead_form');
    $this->setAttributes(array(
        'action' => '',
        'method' => 'post',
        'name' => 'lead_form',
        'id' => 'lead_form',
        'class' => 'smart-form',
        'role' => 'form',
        'novalidate' => 'novalidate'
    ));
    $this->add(array(
        'name' => 'first_name',
        'type' => 'text',
        'options' => array(
            'label' => 'First Name',
        ),
        'attributes' => array(
            'class' => 'form-control validate[required,custom[onlyLetterSp]]',                
            'placeholder' => 'First name',                
            **'ng-model' => "first_name", // this attribute is not generating in view**
        ),            
    ));
}

}

这是我的控制器它调用这个表单并发送给视图以显示

$createLeadForm = new 'Admin'Form'LeadForm();
return new ViewModel(array(
            'form' => $createLeadForm,
));

下面是显示元素

的代码
<?php echo $this->formInput($this->form->get('first_name')); ?>

但是在打印这个表单元素之后,我在输入元素

中没有看到"ng-model"
<input type="text" value="" placeholder="First name" class="form-control validate[required,custom[onlyLetterSp]]" name="first_name" >

我已经设置了一个属性"ng-model",但这个属性没有出现在视图

我希望这个元素像(使用zend形式)-

<input type="text" ng-model="first_name" value="" placeholder="First name" class="form-control validate[required,custom[onlyLetterSp]]" name="first_name" >

我怎么能做到这一点,使用zend形式和什么需要改变在这个形式,为什么我不能添加自定义属性?请帮帮我。

Thanks in Advance

你总是可以使用data-ng-*:

$this->add(array(
    // ...
    'attributes' => array(
        // ...
        'data-ng-model' => 'first_name',
    ),            
));

Zend不支持data-*以外的其他属性,所以要为ng-*添加属性,我必须修改View Helper类

在这个命名空间下(Zend'Form'View'Helper)在主库中,你会发现一个名为"AbstractHelper"的类,在这个类中,你可以得到一个名为"prepareAttributes"的方法,在这里你必须改变以下行-

if (!isset($this->validGlobalAttributes[$attribute])
   && !isset($this->validTagAttributes[$attribute])
   && 'data-' != substr($attribute, 0, 5)
) 

if (!isset($this->validGlobalAttributes[$attribute])
   && !isset($this->validTagAttributes[$attribute])
   && 'data-' != substr($attribute, 0, 5)
   && 'ng-' != substr($attribute, 0, 3)
) 

看这里我添加了

&& 'ng-' != substr($attribute, 0, 3)

以便可以使用zend form

添加ng-*属性

再次感谢

这对我很有效。

class FooForm extends Form
{
    public function __construct(LookupService $lookupService)
    {
        parent::__construct('fooForm');
        $this->setAttribute('novalidate', true);
    }
...
}

Try this: (data attribute)

ZF2:

public function __construct() {
    parent::__construct('lead_form');
    $this->setAttributes(array(
        'action' => '',
        'method' => 'post',
        'name' => 'lead_form',
        'id' => 'lead_form',
        'class' => 'smart-form',
        'role' => 'form',
        'novalidate' => 'novalidate'
    ));
    $this->add(array(
        'name' => 'first_name',
        'type' => 'text',
        'options' => array(
            'label' => 'First Name',
        ),
        'attributes' => array(
            'class' => 'form-control validate[required,custom[onlyLetterSp]]',
            'placeholder' => 'First name',
            'data-ng'     => json_encode(
                array(
                    'ng-model' => 'first_name',
                    'ng-123'   => '123',
                ), true
            )
        ),
    ));
}

在HTML-Manipulation之后调用这个JS函数:

/**
 * ZF2 supports no custom attributs
 * this function converts data-ng attributes to ng attributes
 */
function addNgAttributes() {
    $('[data-ng]').each(function () {
        var ngdata = $(this).data('ng');
        var _this = this;
        $.each(ngdata, function (key, value) {
            $(_this).attr(key, value)
        });
    })
}

你应该扩展Zend'Form'View'Helper'FormFile并通过factory:

    namespace YourNamespace;    
    use Zend'Form'View'Helper'FormFile;
    class FormFile extends FormFile {
        /**
         * Attributes valid for the input tag type="file"
         *
         * @var array
         */
        protected $validTagAttributes = [
            'name' => true,
            'accept' => true,
            'autofocus' => true,
            'disabled' => true,
            'form' => true,
            'multiple' => true,
            'required' => true,
            'type' => true,
            'yourCustomAttribute' => true, //<--- here add Your attribute
        ];        
    }

和在module.php中:

    public function getViewHelperConfig() {
    return array(
        'factories' => array(
            'Zend'Form'View'Helper'FormFile' => function($sm) {
                $helper = new 'YourNamespace'FormFile();
                return $helper;
            }
        )
    );
}