phpyiiformbuilderyii-cformmodelyii-cform

yii: just trying to show an innocent form using form builder


I'm just trying to show a form using form builder, but the fields are not showed.. This is the code:

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:

I was missing the rules in RegisterForm.php..

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

so, the rules are mandatory??? Anyway Im interested also in showing the fields separatedly, so I tried this:

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

but I'm getting this error:

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     }

EDIT 2:

After the comment of 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
}

Javier


Solution

  • To render the form differently you can use the following:

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

    You can use active helpers, but they need a CModel derived class, not CForm derived, hence you need to do this (access the model through CForm):

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