プロキシクラスを使用して、さまざまなsfFormにカスタム値を設定できるようにしようとしています。私はこのようにしなければなりません。なぜなら、phpは複数の継承(すべてのsfFormはdoctrineによって作られたBase *を拡張しています)であり、sfForm configure()メソッドには常に同じコードをコピーして貼り付けます。プロキシクラスを作成する
今まで私はクラスを作ったが、それを動作させることはできなかった。私は参照によってオブジェクトを渡さなければならないことを知っているが、私は立ち往生している!ここで
は私が
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 disableCSRFProtection(){
$this->_form->disableCSRFProtection();
}
}
私はプロキシクラスは静的である可能性が知っているが、今のところ、それは同じだ作ったものです。
編集:
私の問題は、私はFormProxyに作ら
$proxy = new FormProxy(new ClientForm(), array(
'formatter' => 'custom',
'CSRFProtection' => false,
));
$form = $proxy->getForm();
変更を行うとき($フォーム変数で)外に適用されていないようだということです。これは、私が$形式の参照を非常にうまく扱っていないからですが、さまざまな方法で悪影響を及ぼしているからです。
すべてのフォームに必要なコードを、すべてのフォームクラスに継承されているBaseFormDoctrineに移動してみませんか? – Dziamid
:私は別のプロジェクトをコピーして再度貼り付けなければならない場合は、同じバージョンをアップグレードすると、私は非常に "エレガントな"それを見つけることはありません。私はこれが世界の終わりではないことを知っていますが、もし私がそれを正しく行うことができれば、私は試してみるでしょう:) – Pabloks