yii:使用名称空间时只检索表单名称类

yii:使用名称空间时只检索表单名称类,yii,Yii,我有一个类似于此的代码来处理表单提交: use \my-project\web\models\forms\RegisterOrganizationForm; ... var_dump($_POST);die(); $model=new LoginForm; if(isset($_POST['LoginForm'])) $model->attributes=$_POST['LoginForm']; 该var_转储的输出是(好的,只是关于表单值的部分): [“\my project\

我有一个类似于此的代码来处理表单提交:

use \my-project\web\models\forms\RegisterOrganizationForm;
...
var_dump($_POST);die();
$model=new LoginForm;
if(isset($_POST['LoginForm']))
    $model->attributes=$_POST['LoginForm'];
该var_转储的输出是(好的,只是关于表单值的部分):

[“\my project\web\models\forms\LoginForm”]=>数组(1){

正如您所看到的,名称空间已经被添加(我没有预料到这一点…),那么,当vardumping时,我如何才能得到下面这样的内容呢??:

["LoginForm"]=> array(1) {...
Javier

在将模型属性设置为表单接收的数据之前,首先调用die()

其次,您正在执行
var\u dump
var\u POST
而不是执行
var\u dump
$\u POST['LoginForm']


第三,如果您想查看表单返回的内容,为什么要执行var_转储?请使用FireFox的firebug或chrome中的开发者工具之类的工具。我和您有相同的问题

修补以下文件:

/path/to/yii/framework/web/helpers/CHtml.php

查找方法“resolveName”,将其替换为以下内容:

/**
 * Resolves a class name, removing namespaces.
 */
public static function resolveClassName($model){
    return end(explode('\\',get_class($model)));
}

/**
 * Generates input name for a model attribute.
 * Note, the attribute name may be modified after calling this method if the name
 * contains square brackets (mainly used in tabular input) before the real attribute name.
 * @param CModel $model the data model
 * @param string $attribute the attribute
 * @return string the input name
 */
public static function resolveName($model,&$attribute)
{
    if(($pos=strpos($attribute,'['))!==false)
    {
        if($pos!==0)  // e.g. name[a][b]
            return self::resolveClassName($model).'['.substr($attribute,0,$pos).']'.substr($attribute,$pos);
        if(($pos=strrpos($attribute,']'))!==false && $pos!==strlen($attribute)-1)  // e.g. [a][b]name
        {
            $sub=substr($attribute,0,$pos+1);
            $attribute=substr($attribute,$pos+1);
            return self::resolveClassName($model).$sub.'['.$attribute.']';
        }
        if(preg_match('/\](\w+\[.*)$/',$attribute,$matches))
        {
            $name=self::resolveClassName($model).'['.str_replace(']','][',trim(strtr($attribute,array(']['=>']','['=>']')),']')).']';
            $attribute=$matches[1];
            return $name;
        }
    }
    return self::resolveClassName($model).'['.$attribute.']';
}
希望这有帮助


另外,对于那些认为这很愚蠢或不必要的人来说,客户端验证器不起作用,因为您不能使用JavaScript(或至少使用jQuery)通过反斜杠按ID选择元素。

好吧,我添加了var_dump(),因为var_dump($_POST['LoginForm'])返回“未定义的索引”。这只是我调试的方式,无论如何,谢谢,下次我将使用FB或类似工具。您对我的问题有什么解决方案吗?如果您只是使用它进行调试,那么获得完整路径并不重要!