1

我只是想使用表单生成器显示表单,但未显示字段。这是代码:

注册.php

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

注册表格.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
}

哈维尔

4

1 回答 1

0

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

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

您可以使用主动助手,但它们需要 CModel 派生类,而不是 CForm 派生类,因此您需要这样做(通过 CForm 访问模型):

<div class="form">
 <?php echo CHtml::beginForm(); ?>
 <?php echo CHtml::activeTextField($form->model,'name') ?>
 <?php echo CHtml::endForm(); ?>
</div>
于 2012-06-07T11:54:49.837 回答