php - 表单返回错误请求:缺少必需参数:id
问题描述
我是 yii2 的新手,我想使用 yii2 basic 制作注册表单也失踪了。这是我的用户模型:
<?php
namespace app\models;
use Yii;
/**
* This is the model class for table "user".
*
* @property int $id
* @property string $username
* @property string $name
* @property string $password
* @property string $authKey
* @property int $id_level
* @property string $accessToken
*
* @property MasterLevel $level
*/
class User extends \yii\db\ActiveRecord implements \yii\web\IdentityInterface
{
/**
* {@inheritdoc}
*/
public static function tableName()
{
return 'user';
}
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['username', 'name', 'password', 'authKey', 'id_level', 'accessToken'], 'required'],
[['id_level'], 'integer'],
[['username'], 'string', 'max' => 30],
[['name'], 'string', 'max' => 50],
[['password', 'authKey'], 'string', 'max' => 20],
[['accessToken'], 'string', 'max' => 10],
[['id_level'], 'exist', 'skipOnError' => true, 'targetClass' => MasterLevel::className(), 'targetAttribute' => ['id_level' => 'id_level']],
];
}
/**
* {@inheritdoc}
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'username' => 'Username',
'name' => 'Name',
'password' => 'Password',
'authKey' => 'Auth Key',
'id_level' => 'Id Level',
'accessToken' => 'Access Token',
];
}
/**
* Gets query for [[Level]].
*
* @return \yii\db\ActiveQuery
*/
public function getLevel()
{
return $this->hasOne(MasterLevel::className(), ['id_level' => 'id_level']);
}
public $id;
public $username;
public $password;
public $authKey;
public $accessToken;
/**
* {@inheritdoc}
*/
public static function findIdentity($id)
{
return isset(self::$users[$id]) ? new static(self::$users[$id]) : null;
}
/**
* {@inheritdoc}
*/
public static function findIdentityByAccessToken($token, $type = null)
{
foreach (self::$users as $user) {
if ($user['accessToken'] === $token) {
return new static($user);
}
}
return null;
}
/**
* Finds user by username
*
* @param string $username
* @return static|null
*/
public static function findByUsername($username)
{
foreach (self::$users as $user) {
if (strcasecmp($user['username'], $username) === 0) {
return new static($user);
}
}
return null;
}
/**
* {@inheritdoc}
*/
public function getId()
{
return $this->id;
}
/**
* {@inheritdoc}
*/
public function getAuthKey()
{
return $this->authKey;
}
/**
* {@inheritdoc}
*/
public function validateAuthKey($authKey)
{
return $this->authKey === $authKey;
}
/**
* Validates password
*
* @param string $password password to validate
* @return bool if password provided is valid for current user
*/
public function validatePassword($password)
{
return $this->password === $password;
}
}
我在用户控制器中的 actionCreate
public function actionCreate()
{
$model = new User();
if ($model->load(Yii::$app->request->post()) && $model->save(false)) {
return $this->redirect(['view', 'id' => $model->id]);
}
return $this->render('create', [
'model' => $model,
]);
}
风景
<?php
use yii\helpers\Html;
/* @var $this yii\web\View */
/* @var $model app\models\User */
$this->title = 'Create User';
$this->params['breadcrumbs'][] = ['label' => 'Users', 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="user-create">
<h1><?= Html::encode($this->title) ?></h1>
<?= $this->render('_form', [
'model' => $model,
]) ?>
</div>
我找不到我的代码错误的地方请帮助我
解决方案
save(false)
方法不会验证您的数据!
这就是为什么有些数据可能不包含在数据库中的原因。
考虑以下:
1-您的rules()
方法的数据必须与数据库匹配。(模型文件中的规则() )
2-在行动中使用以下内容:
if ($model->load(\Yii::$app->request->post()) && $model->save()){
#Code ...
或者:
if ($model->load(Yii::$app->request->post()) && $model->validate()){
#Code ...
if ($model->save(false)){
#Code ...
推荐阅读
- python - 使用 gammu 发送短信失败(错误 500)
- excel - Excel 的默认日期聚合
- bash - bash: cd: 参数太多 [GITSCRIPT]
- python - 使用代码忽略 IE 保护模式设置时 driver.quit() 不起作用
- .net-core - .NET Core Identity - 如何从路径/连接/令牌以外的其他地方生成令牌
- r - 如何使用多个列作为不同的字符串条件执行连接?
- c# - 程序集“Ninject.Extensions.Interception.DynamicProxy”中的方法没有实现
- python - 选择索引数据框中的列
- windows-10 - Access/MySQL - 向后兼容的迁移;链接表
- httprequest - Microsoft Teams webhook 有效负载包含不可用的 Teams ID