我对独特字段使用ajax验证,但它无效。 在我的用户控制器中它可以工作,但在这个站点控制器中没有。
任何人都可以帮助我吗?
这是我的控制器
<?php
namespace frontend\models;
use Yii;
/**
* This is the model class for table "user".
*
* @property integer $id
* @property string $username
* @property string $name
* @property string $lastname
* @property string $email
* @property integer $phone
* @property string $notes
* @property string $company
* @property string $password_hash
* @property string $auth_key
* @property integer $status
* @property integer $created_at
* @property integer $updated_at
* @property string $country
* @property string $state
* @property string $city
* @property string $language
* @property integer $salary
* @property string $hiredate
* @property string $birthday
* @property string $address
* @property string $dismission
*/
class User extends \yii\db\ActiveRecord
{
/**
* @inheritdoc
*/
public static function tableName()
{
return 'user';
}
public function fields()
{
return [
'id','status'
];
}
/**
* @inheritdoc
*/
public function rules()
{
return [
['username', 'unique'],
[['username', 'name', 'lastname', 'email', 'phone', 'password_hash'], 'required'],
[['phone', 'status', 'created_at', 'updated_at', 'salary'], 'integer'],
[['hiredate', 'birthday', 'dismission'], 'safe'],
[['username', 'name', 'lastname', 'email', 'notes', 'company', 'password_hash', 'auth_key', 'country', 'state', 'city', 'language', 'address'], 'string', 'max' => 100],
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'username' => 'Username',
'name' => 'Name',
'lastname' => 'Lastname',
'email' => 'Email',
'phone' => 'Phone',
'notes' => 'Notes',
'company' => 'Company',
'password_hash' => 'Password Hash',
'auth_key' => 'Auth Key',
'status' => 'Status',
'created_at' => 'Created At',
'updated_at' => 'Updated At',
'country' => 'Country',
'state' => 'State',
'city' => 'City',
'language' => 'Language',
'salary' => 'Salary',
'hiredate' => 'Hiredate',
'birthday' => 'Birthday',
'address' => 'Address',
'dismission' => 'Dismission',
];
}
}
这是我的模特
<?php
/* @var $this yii\web\View */
/* @var $form yii\bootstrap\ActiveForm */
/* @var $model \frontend\models\SignupForm */
use yii\helpers\Html;
use yii\bootstrap\ActiveForm;
use yii\helpers\ArrayHelper;
use frontend\models\Countries;
use frontend\models\States;
use frontend\models\Cities;
use yii\helpers\Url;
$this->title = 'Signup';
$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, 'validationUrl' => Url::toRoute('site/validation')]); ?>
<?= $form->field($model, 'username')->textInput(['autofocus' => true]) ?>
<?= $form->field($model, 'name')->textInput(['autofocus' => true]) ?>
<?= $form->field($model, 'lastname')->textInput(['autofocus' => true]) ?>
<?= $form->field($model, 'email') ?>
<?= $form->field($model, 'phone')->textInput(['autofocus' => true]) ?>
<?= $form->field($model, 'company')->textInput(['autofocus' => true]) ?>
<?= $form->field($model, 'password_hash')->passwordInput()->label('Password') ?>
<?= $form->field($model, 'country')->dropDownList(ArrayHelper::map(Countries::find()->all(),'id','name'),
[
'prompt' => 'Страна',
'onchange' => '
$.post( "../states/lists?id='.'"+$(this).val(), function( data ) {
$( "select#signupform-state" ).html( data );
});'
]); ?>
<?= $form->field($model, 'state')->dropDownList([],
[
'prompt' => 'Регион',
'onchange' => '
$.post( "../cities/lists?id='.'"+$(this).val(), function( data ) {
$( "select#signupform-city" ).html( data );
});'
]); ?>
<?= $form->field($model, 'city')->dropDownList([],[ 'prompt' => 'Город' ]); ?>
<?= $form->field($model, 'language')->dropDownList([
'1' => 'Русский',
'2' => 'English',
'3' => 'Turkce',
]); ?>
<div class="form-group">
<?= Html::submitButton('Signup', ['class' => 'btn btn-primary', 'name' => 'signup-button']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>
</div>
</div>
这是我的观点
func Print() -> Bool{
print(userName)
print(password)
var statusValue:Bool = False
let request: Request = Request()
let body: NSMutableDictionary = NSMutableDictionary()
do{
try request.post(url: url, body: body, completionHandler: { data, response, error in
if error != nil{
print(error?.localizedDescription)
return
}
let statuscode = response as? HTTPURLResponse
print("error:",error)
print("status code:",statuscode?.statusCode)
if statuscode?.statusCode == 200 {
print("Success")
statusValue = True
}
else{
print("Fail")
}
})
}
catch{
print("Not me error")
}
print("Printing status value outside:",statusValue)
return statusValue
}
答案 0 :(得分:0)
<强> SiteController.php 强>
public function actionSignup()
{
$model = new SignupForm();
if ($model->load(Yii::$app->request->post())) {
if (Yii::$app->request->isAjax) {
Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
return \yii\bootstrap\ActiveForm::validate($model);
}
if ($user = $model->signup()) {
if (Yii::$app->getUser()->login($user)) {
return $this->goHome();
}
}
}
return $this->render('signup', [
'model' => $model,
]);
}
<强> SignUpForm.php 强>
public function rules()
{
return [
['username', 'filter', 'filter' => 'trim'],
['username', 'required'],
['username', 'unique', 'targetClass' => '\common\models\User', 'message' => 'This username has already been taken.'],
['username', 'string', 'min' => 2, 'max' => 255],
['email', 'filter', 'filter' => 'trim'],
['email', 'required'],
['email', 'email'],
['email', 'string', 'max' => 255],
['email', 'unique', 'targetClass' => '\common\models\User', 'message' => 'This email address has already been taken.'],
['password', 'required'],
['password', 'string', 'min' => 6],
];
}
/**
* Signs user up.
*
* @return User|null the saved model or null if saving fails
*/
public function signup()
{
if (!$this->validate()) {
return null;
}
$user = new User();
$user->username = $this->username;
$user->email = $this->email;
$user->setPassword($this->password);
$user->generateAuthKey();
return $user->save() ? $user : null;
}
<强> signup.php 强>
<?php $form = ActiveForm::begin(['id' => 'form-signup', 'enableAjaxValidation' => true]); ?>