2012-06-07 42 views
1

我只是想表明一種形式using form builder顯示一個無辜的形式,但字段不顯示。這是代碼:警予:剛嘗試使用表單生成

register.php

entered here 
<div class="form"> 
    <?php echo $form?> 
</div> 

RegisterForm.php

class RegisterForm extends CFormModel { 

    public $name; 
} 

MyController.php

public function actionRegister() 
{ 
    $model = new RegisterForm(); 

    $config=array(
      'elements'=>array(
       'name'=>array(
        'type'=>'text', 
        ), 
       ), 
      ); 

    $form = new CForm($config, $model); 

    $this->render('register', array('form' => $form)); 
} 

EDIT 1:

我是缺少在RegisterForm.php規則..

public function rules() 
{ 
    return array(
     array('name', 'required'), 
    ); 
} 

所以,規則是強制性??? 反正林興趣也顯示separatedly領域,所以我想這:

<div class="form"> 
<?php echo CHtml::beginForm(); ?> 
    <?php echo CHtml::activeTextField($form,'name') ?> 
    <?php echo CHtml::endForm(); ?> 
</div> 

,但我得到這個錯誤:

CForm and its behaviors do not have a method or closure named "getValidators". 

/home/javiergarcia/programacion/libs/yii/framework/base/CComponent.php(266) 

254  public function __call($name,$parameters) 
255  { 
256   if($this->_m!==null) 
257   { 
258    foreach($this->_m as $object) 
259    { 
260     if($object->getEnabled() && method_exists($object,$name)) 
261      return call_user_func_array(array($object,$name),$parameters); 
262    } 
263   } 
264   if(class_exists('Closure', false) && $this->canGetProperty($name) && $this->$name instanceof Closure) 
265    return call_user_func_array($this->$name, $parameters); 
266   throw new CException(Yii::t('yii','{class} and its behaviors do not have a method or closure named "{name}".', 
267    array('{class}'=>get_class($this), '{name}'=>$name))); 
268  } 

編輯2:

的評論後dInGd0nG:

object(CForm)#35 (17) { 
    ["title"]=> 
    NULL 
    ["description"]=> 
    NULL 
    ["method"]=> 
    string(4) "post" 
    ["action"]=> 
    string(0) "" 
    ["inputElementClass"]=> 
    string(17) "CFormInputElement" 
    ["buttonElementClass"]=> 
    string(18) "CFormButtonElement" 
    ["attributes"]=> 
    array(0) { 
    } 
    ["showErrorSummary"]=> 
    bool(false) 
    ["activeForm"]=> 
    array(1) { 
    ["class"]=> 
    string(11) "CActiveForm" 
    } 
    ["_model":"CForm":private]=> 
    object(my-firm\web\models\forms\RegisterOrganizationForm)#34 (9) { 
    ["organization_name"]=> 
    NULL 
    ["email"]=> 
    NULL 
    ["password"]=> 
    NULL 
    ["repeatPassword"]=> 
    NULL 
    ["_errors":"CModel":private]=> 
    array(0) { 
    } 
    ["_validators":"CModel":private]=> 
    NULL 
    ["_scenario":"CModel":private]=> 
    string(0) "" 
    ["_e":"CComponent":private]=> 
    NULL 
    ["_m":"CComponent":private]=> 
    NULL 
    } 
    ["_elements":"CForm":private]=> 
    object(CFormElementCollection)#36 (6) { 
    ["_form":"CFormElementCollection":private]=> 
    *RECURSION* 
    ["_forButtons":"CFormElementCollection":private]=> 
    bool(false) 
    ["_d":"CMap":private]=> 
    array(1) { 
     ["organization_name"]=> 
     object(CFormInputElement)#37 (15) { 
     ["type"]=> 
     string(4) "text" 
     ["name"]=> 
     string(17) "organization_name" 
     ["hint"]=> 
     NULL 
     ["items"]=> 
     array(0) { 
     } 
     ["errorOptions"]=> 
     array(0) { 
     } 
     ["enableAjaxValidation"]=> 
     bool(true) 
     ["enableClientValidation"]=> 
     bool(true) 
     ["layout"]=> 
     string(30) "{label} 
{input} 
{hint} 
{error}" 
     ["_label":"CFormInputElement":private]=> 
     NULL 
     ["_required":"CFormInputElement":private]=> 
     NULL 
     ["attributes"]=> 
     array(0) { 
     } 
     ["_parent":"CFormElement":private]=> 
     *RECURSION* 
     ["_visible":"CFormElement":private]=> 
     NULL 
     ["_e":"CComponent":private]=> 
     NULL 
     ["_m":"CComponent":private]=> 
     NULL 
     } 
    } 
    ["_r":"CMap":private]=> 
    bool(false) 
    ["_e":"CComponent":private]=> 
    NULL 
    ["_m":"CComponent":private]=> 
    NULL 
    } 
    ["_buttons":"CForm":private]=> 
    NULL 
    ["_activeForm":"CForm":private]=> 
    NULL 
    ["_parent":"CFormElement":private]=> 
    object(SiteController)#24 (19) { 
    ["layout"]=> 
    string(14) "//layouts/main" 
    ["menu"]=> 
    array(0) { 
    } 
    ["breadcrumbs"]=> 
    array(0) { 
    } 
    ["baseUrl"]=> 
    string(15) "//my-firm.local" 
    ["assetsUrl"]=> 
    string(16) "/assets/e876fe71" 
    ["bodyClass"]=> 
    string(9) "sidebar-3" 
    ["_viewVars":"my-firm\core\Controller":private]=> 
    array(2) { 
     ["model"]=> 
     object(my-firm\web\models\forms\LoginForm)#31 (9) { 
     ["email"]=> 
     NULL 
     ["pass"]=> 
     NULL 
     ["rememberMe"]=> 
     NULL 
     ["_identity":"my-firm\web\models\forms\LoginForm":private]=> 
     NULL 
     ["_errors":"CModel":private]=> 
     array(0) { 
     } 
     ["_validators":"CModel":private]=> 
     NULL 
     ["_scenario":"CModel":private]=> 
     string(0) "" 
     ["_e":"CComponent":private]=> 
     NULL 
     ["_m":"CComponent":private]=> 
     NULL 
     } 
     ["linkSchools"]=> 
     string(39) "https://my-firm.local/escoles/espana/76" 
    } 
    ["defaultAction"]=> 
    string(5) "index" 
    ["_id":"CController":private]=> 
    string(4) "site" 
    ["_action":"CController":private]=> 
    object(CInlineAction)#32 (4) { 
     ["_id":"CAction":private]=> 
     string(20) "RegisterOrganization" 
     ["_controller":"CAction":private]=> 
     *RECURSION* 
     ["_e":"CComponent":private]=> 
     NULL 
     ["_m":"CComponent":private]=> 
     NULL 
    } 
    ["_pageTitle":"CController":private]=> 
    NULL 
    ["_cachingStack":"CController":private]=> 
    NULL 
    ["_clips":"CController":private]=> 
    NULL 
    ["_dynamicOutput":"CController":private]=> 
    NULL 
    ["_pageStates":"CController":private]=> 
    NULL 
    ["_module":"CController":private]=> 
    NULL 
    ["_widgetStack":"CBaseController":private]=> 
    array(0) { 
    } 
    ["_e":"CComponent":private]=> 
    NULL 
    ["_m":"CComponent":private]=> 
    NULL 
    } 
    ["_visible":"CFormElement":private]=> 
    NULL 
    ["_e":"CComponent":private]=> 
    NULL 
    ["_m":"CComponent":private]=> 
    NULL 
} 

Ja VIER

+0

嘗試'的var_dump($形式)'在之前myController的'$這個 - >渲染()'和粘貼O/p這裏 – dInGd0nG

+0

完成---------- – user1077220

+0

我認爲你的錯誤出現在你想分開顯示這些字段的部分,你到底在做什麼在那裏? –

回答

0

要渲染的形式不同,你可以使用以下命令:

<div class="form"> 
<?php echo $form->renderBegin(); ?> 
<?php echo $form['name']; ?> 
<?php echo $form->renderEnd(); ?> 
</div> 

可以使用Active幫手,但是他們需要一個CModel派生類,不是的CForm派生,因此你需要這樣做(access the model through CForm) :

<div class="form"> 
<?php echo CHtml::beginForm(); ?> 
<?php echo CHtml::activeTextField($form->model,'name') ?> 
<?php echo CHtml::endForm(); ?> 
</div> 
+0

對不起,你可以使用主動助手,但通過訪問'CForm'的'模型'屬性 –

+0

謝謝,第二段代碼正在工作! – user1077220

+0

好的,很高興幫助。但是,從下一次開始,嘗試和評論一下,讓回答者知道,如果您有疑問,那麼可以更快地解決問題。 –