开发者

Making a Proxy Class

I'm trying to do a Proxy class that allow me to set custom values to different sfForm's. I have to do it this way because php dosn't have multiple inheritance (all sfForm extends some Base* made by doctrine) and I'm always copy-pasting the same code to sfForm configure() method.

Up to now I made the class but couldn't make it work. I know i have to pass the object by reference but i'm stuck!

Here is what I made

class FormProxy {
    private $_form;
    private $_formatter;

    public function __construct(sfForm &$form, $params = array()) {
        $this->_form = $form;

        if(count($params)>0)
            $this->set ($params);
    }

    public function set($array = array()){
        if (count($array) == 0){
            return;
        }

        if(isset ($array['formatter'])){
            $this->setFormatter($array['formatter']);
        }

        if(isset ($array['CSRFProtection'])){
            $this->disableCSRFProtection();
        }

        return $this;
    }

    public function setForm(sfForm &$form){
        $this->_form = $form;
        return $this;
    }

    public function & getForm(){
        $this->init();
        return $this->_form;
    }

    public function getFormatter(){
        return $this->_formatter;
    }

    public function setFormatter($formatter = null){
        $this->_formatter = $formatter;
        return $this;
    }

    private function init(){
        if($this->_formatter != null){
            $decorator = new sfWidgetFormSchemaFormatterLocal($form->getWidgetSchema(), $form->getValidatorSchema());
            $form->getWidgetSchema()->addFormFormatter($this->_formatter, $decorator);
            $form->getWidgetSchema()->setFormFormatterName($this->_formatter);
        }

    }

    public function disableCSRFProtec开发者_C百科tion(){
        $this->_form->disableCSRFProtection();
    }
}

I know the proxy class could be static, but for now it's the same.

Edit:

My problem is that when I do

   $proxy = new FormProxy(new ClientForm(), array(
        'formatter' => 'custom',
        'CSRFProtection' => false,
    ));

    $form = $proxy->getForm();

the changes made into the FormProxy doesn't seem applied outside (in the $form variable). I think this is because I'm not handling very good the reference of the $form, but tried in different ways with negative outcomes .


Well, not having any specific error to go on, I will wager that your issue is that the variable $form is undefined in the function init. That is the most apparent issue. YOu can fix this by placing $form = $this->_form; after if($this->_formatter != null){

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜