2011-02-08 45 views
1

我瘋狂的設計師想一個領域內的消息「必選」顯示(紅色),如果表單已經提交,這是因爲一個空場的無效。Symfony的形式:在錯誤設置默認/價值?

有問題的形式是登錄提示,我使用擴展sfGuardFormSignin

我已經成功地設定的值,並添加一類具有自定義類..

$this->widgetSchema['username']->setAttribute('class','red'); 
$this->widgetSchema['username']->setDefault('Required');

。 。但是,只有當用戶名字段無效並且由於所需的錯誤,我該如何做到這一點?

我想這是對密碼字段一樣嗎?

提前感謝

編輯:

感謝您的意見greg0ire。我已經與但sfWidgetFormSchemaFormatter的formatRow方法似乎並沒有被擊中一齣戲。這是因爲我的表單擴展了sfGuardFormSignin並使用了sfGuardAuth插件?

class FrontendsfGuardFormSignin extends sfGuardFormSignin 
{ 
    public function configure() 
    { 
    parent::configure(); 

    // This works! 
    $this->widgetSchema['username']->setLabel('Email'); 

    // I copied this from the link you pasted 
    $decorator = new myWidgetFormSchemaFormatterCustom($this->getWidgetSchema()); 
    $this->widgetSchema->addFormFormatter('custom', $decorator); 
    $this->widgetSchema->setFormFormatterName('custom'); 
    } 
}

/lib/widget/myWidgetFormSchemaFormatterCustom.class.php

class myWidgetFormSchemaFormatterCustom extends sfWidgetFormSchemaFormatter 
{ 

    public function __construct(sfWidgetFormSchema $widgetSchema) 
    { 
    parent::__construct($widgetSchema); 
    } 

    public function formatRow($label, $field, $errors = array(), $help = '', $hiddenFields = null) 
    { 
    // Nothing happens!? 
    var_dump($errors); 
    die(); 
    parent::formatRow($label, $field, $errors, $help, $hiddenFields); 
    } 

}

回答

2
$widget->render(array('value' => $widget->getError())); 
+0

感謝丹,你是對的。我可以在模板中處理我需要的內容,而不是創建自定義小部件。 –

1

設計師有這樣瘋狂的想法...

你必須write a custom schema formatter做到這一點。您可能必須重寫才能實現此目的。

分析這種方法的$errors陣列參數,如果你發現它的「必需」的錯誤,然後做你的特別的東西。您不需要使用您在問題中發佈的代碼。