yii:只是尝试使用表单生成器显示无辜的表单

时间:2012-06-07 08:53:47

标签: php yii formbuilder yii-cformmodel yii-cform

我只是想显示一个表单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));
 }  

编辑1:

我错过了RegisterForm.php中的规则..

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

所以,规则是强制性的??? 无论如何我也有兴趣分开显示字段,所以我尝试了这个:

<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
}

哈维尔

1 个答案:

答案 0 :(得分:0)

要以不同方式呈现表单,您可以使用以下内容:

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

您可以使用活动帮助程序,但它们需要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>