【问题标题】:Yii framework: check if the user has filled the required profile fieldsYii 框架:检查用户是否填写了所需的个人资料字段
【发布时间】:2014-11-19 06:06:49
【问题描述】:

我有一些个人资料字段是必需的,但在注册页面中没有询问。

我如何在 themefolder\views\layout\tpl_navigation.php 中检查他们是否已完成个人资料,以显示警报(您必须按顺序完成个人资料到...)如果他们没有?

【问题讨论】:

    标签: php yii


    【解决方案1】:

    尽管不清楚,但建议如下:

    我假设您可以通过以下方式获取登录用户 ID:

    $uid=Yii::app()->user->id;
    

    另外,用户的模型被命名为User

    然后,在您的控制器中:

    public function beforeAction($action) {
        //getting user id
        $uid=Yii::app()->user->id;
        //finding user 
        $user=User::model()->findByPk($uid);
        //checking if address or mobile is empty
        if(empty($user->address) || empty($user->mobile)){//and othe fields
            Yii::app()->user->setFlash('PROFILE_IS_NOT_COMPLETED',TRUE);
        }
        parent::beforeAction($action);
    }
    

    然后在你的模板中:

    if (Yii::app()->user->hasFlash('PROFILE_IS_NOT_COMPLETED') && Yii::app()->user->getFlash('PROFILE_IS_NOT_COMPLETED',FALSE)===TRUE) {
            echo CHtml::tag('span', array('style' => 'color:red'), "Please complete your profile"); //Just an example!
            //you absolutely need customization here
        }
    

    【讨论】:

      【解决方案2】:

      解决了,很简单:

      $profile = Yii::app()->getModule('user')->user()->profile;
      $validate =  $profile->validate();
      
      if(!$validate) 
          echo '<div class="alert alert-error">Complete your profile...</div>';
      

      【讨论】:

        猜你喜欢
        • 2012-06-12
        • 1970-01-01
        • 1970-01-01
        • 2012-11-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-10-11
        相关资源
        最近更新 更多