yx 2015-06-05 09:59:54 15147次浏览 7条评论 42 16 0

视图signup.php代码:

<?php
use yii\helpers\Html;
use yii\bootstrap\ActiveForm;

/* @var $this yii\web\View */
/* @var $form yii\bootstrap\ActiveForm */
/* @var $model \frontend\models\SignupForm */

$this->title = '注册';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="site-signup">
    <h1><?= Html::encode($this->title) ?></h1>

    <p>Please fill out the following fields to signup:</p>

    <div class="row">
        <div class="col-lg-5">
            <?php $form = ActiveForm::begin([
                'id' => 'form-signup',
                'enableAjaxValidation' => true,
                'enableClientValidation' => true,
            ]); ?>
                
                <?= $form->field($model, 'username') ?>
                <?= $form->field($model, 'email') ?>
                <?= $form->field($model, 'password')->passwordInput() ?>
                <?= $form->field($model, 'password_compare')->passwordInput() ?>
                
                <div class="form-group">
                    <?= Html::submitButton('Signup', ['class' => 'btn btn-primary', 'name' => 'signup-button']) ?>
                </div>
                
            <?php ActiveForm::end(); ?>
        </div>
    </div>
</div>

控制器SiteController.php

    public function actionSignup()
    {
        $model = new SignupForm();
        
        $model->load($_POST);
        if (Yii::$app->request->isAjax) {
            Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
            return \yii\bootstrap\ActiveForm::validate($model);
        }
        
        if ($model->load(Yii::$app->request->post())) {
            if ($user = $model->signup()) {
                if (Yii::$app->getUser()->login($user)) {
                    return $this->goHome();
                }
            }
        }

        return $this->render('signup', [
            'model' => $model,
        ]);
    }

模型SignupForm.php

use common\models\User;
use yii\base\Model;
use Yii;

/**
 * Signup form
 */
class SignupForm extends Model
{
    public $username;
    public $email;
    public $password;
    public $password_compare;

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            ['username', 'filter', 'filter' => 'trim'],
            ['username', 'required'],
            ['username', 'unique', 'targetClass' => '\common\models\User', 'message' => '用户名已存在.'],
            ['username', 'string', 'min' => 2, 'max' => 255],

            ['email', 'filter', 'filter' => 'trim'],
            ['email', 'required'],
            ['email', 'email'],
            ['email', 'unique', 'targetClass' => '\common\models\User', 'message' => '邮箱名已存在.'],

            [['password', 'password_compare'], 'required'],
            [['password', 'password_compare'], 'string', 'min' => 6, 'max' => 16, 'message' => '{attribute}是6-16位数字或字母'],
            ['password_compare', 'compare', 'compareAttribute' => 'password', 'message' => '两次密码不一致'],
        ];
    }

    /**
     * Signs user up.
     *
     * @return User|null the saved model or null if saving fails
     */
    public function signup()
    {
        if ($this->validate()) {
            $user = new User();
            $user->username = $this->username;
            $user->email = $this->email;
            $user->setPassword($this->password);
            $user->generateAuthKey();
            if ($user->save()) {
                return $user;
            }
        }

        return null;
    }
}
觉得很赞
  • 评论于 2016-03-30 14:48 举报

    在别的控制器里写是不是改下控制器名称就可以啊?

  • 评论于 2016-04-01 16:49 举报

    不错,赞个

    1 条回复
    评论于 2016-04-08 17:30 回复

    嗯,是的啊

  • 评论于 2016-07-08 18:00 举报

    不错,赞个

  • 评论于 2016-08-12 10:02 举报

    'enableAjaxValidation' => true,
    'enableClientValidation' => true,
    这两句放上去就报错,这是为什么?
    PHP Parse Error – yii\base\ErrorException
    syntax error, unexpected ''enableAjaxValidation'' (T_CONSTANT_ENCAPSED_STRING), expecting ']'

    1. in D:\WWW\Yii\basic2.09\views\site-one\signup.php at line 22
    13141516171819202122232425262728293031
    
    <div class="site-signup">
        <h1><?= Html::encode($this->title) ?></h1>
     
        <p>Please fill out the following fields to signup:</p>
     
        <div class="row">
            <div class="col-lg-5">
                <?php $form = ActiveForm::begin([
                		'id' => 'form-signup'
              			'enableAjaxValidation' => true,
                    	'enableClientValidation' => true,
     
                ]); ?>
    
    1 条回复
    评论于 2016-09-26 17:08 回复

    'id' => 'form-signup' 后边少逗号

    觉得很赞
  • 评论于 2016-09-26 17:14 举报

    用到了,感谢

  • 评论于 2017-03-09 16:34 举报

    是粘贴直接就能使吗?

  • 评论于 2017-03-09 19:37 举报

    if (Yii::$app->request->isAjax) {

            Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
            return \yii\bootstrap\ActiveForm::validate($model);
        }
    

    <?php $form = ActiveForm::begin([

                'id' => 'form-signup',
                'enableAjaxValidation' => true,
                'enableClientValidation' => true,
            ]); ?>
    

    这两段是用来验证ajax的么?

    1 条回复
    评论于 2017-03-14 19:48 回复

    enableAjaxValidation是用来控制是否开启 ajax验证的

您需要登录后才可以评论。登录 | 立即注册