英文:
Yii2 Model Rules are not Firing on Post
问题
你的 Yii2 表单似乎出现了问题,字段未在提交时触发验证规则,也没有显示必填字段的错误消息。你可以尝试以下步骤来解决问题:
- 确保在视图中正确渲染表单,并且 ActiveForm 的属性配置正确。你的代码看起来应该是这样的:
$form = ActiveForm::begin(['id' => 'paperadduserform']);
-
检查模型类
PaperAddUserForm
中的验证规则方法rules()
是否正确设置。你的规则看起来是正确的。 -
确保在控制器动作
actionAddReviewer
中,你正确创建了模型实例并加载了 POST 数据。你可以添加一些日志或调试语句来检查是否成功加载数据,例如:
if ($registerModel->load(Yii::$app->request->post())) {
// 在这里加入日志或调试语句,检查是否成功加载数据
Yii::info($registerModel->attributes, 'form-data');
if ($registerModel->paperAddReviewer()) {
return $this->redirect(['/paper/reviewer/reviewer']);
}
}
- 确保视图中的输入字段的
name
属性与模型的属性名称匹配。在你的代码中,这些字段看起来应该是正确的。
如果问题仍然存在,可能需要更多的调试和检查,确保数据正确加载和验证。另外,检查 Yii2 的日志文件以查看是否有任何与验证相关的错误消息。
英文:
I have quite a simple form using Yii2 but for some reason my model rules are nor firing when I hit the submit button. Below is my UI form.
<?php
$form = ActiveForm::begin(['id' => 'paperadduserform',])
?>
<?= $form->field($registerModel, 'userFirstname')->textInput(
[placeholder' => 'Enter first name',
class'=>'form-control form-control-sm border-primary',
maxlength'=>'40'])->label(false); ?>
<?= $form->field($registerModel, 'userLastname')->textInput(
[placeholder' => 'Enter last name',
class'=>'form-control form-control-sm border-primary',
maxlength'=>'40'])->label(false); ?>
<?= Html::submitButton('Add Reviewer', ['id'=>'registerBtn','class' => 'btn btn-primary btn-xl']) ?>
<?php ActiveForm::end() ?>
This is my Model class which contains the Rules.
class PaperAddUserForm extends Model
{
public $userFirstname;
public $userLastname;
public function rules()
{
return [
['userFirstname', 'trim'],
['userFirstname', 'required'],
['userFirstname', 'string', 'min' => 1, 'max' => 40],
['userLastname', 'trim'],
['userLastname', 'required'],
['userLastname', 'string', 'min' => 1, 'max' => 40],
];
}
private function addUser($role)
{
try {
$user = new User();
$user->user_firstname = $this->userFirstname;
$user->user_lastname = $this->userLastname;
$user->save(false);
$auth = \Yii::$app->authManager;
$authorRole = $auth->getRole($role);
$auth->assign($authorRole, $user->getId());
} catch (Exception $e) {
throw $e;
}
}
public function paperAddReviewer()
{
$this->addUser(AuthRolesEnum::OC_REVIEWER);
return true;
}
}
This is part of my Controller class.
public function actionAddReviewer()
{
$registerModel = new PaperAddUserForm();
if ($registerModel->load(Yii::$app->request->post()) && $registerModel->paperAddReviewer()) {
return $this->redirect(['/paper/reviewer/reviewer']);
}
return $this->render('/paper/reviewer/add-reviewer',
[
'registerModel' => $registerModel,
]);
}
But when I run the application, the fields are not marked as required when I leave them empty. When I hit the submit button it tries to save to the DB, it does not give me the 'Field is Required' error message.
答案1
得分: 2
你没有在任何地方调用 yii\base\Model::validate()
方法。如果只是定义规则而不触发验证,这些规则不会生效。规则的定义仅在验证触发时使用。
如果你的模型扩展了 yii\db\ActiveRecord
,那么在其 save()
方法中会调用 validate()
方法,除非你在其 $runValidation
参数中传递了 false
。
但如果你的模型直接扩展了 yii\base\Model
,则必须显式调用 validate()
方法。
在你的情况下,你可以像这样在控制器中添加 validate()
调用:
if (
$registerModel->load(Yii::$app->request->post())
&& $registerModel->validate()
&& $registerModel->paperAddReviewer()
) {
return $this->redirect(['/paper/reviewer/reviewer']);
}
或者你可以将其作为 PaperAddUserForm::paperAddReviewer()
方法的一部分,像这样:
public function paperAddReviewer()
{
if (!$this->validate()) {
return false;
}
$this->addUser(AuthRolesEnum::OC_REVIEWER);
return true;
}
英文:
You are not calling yii\base\Model::validate()
method anywhere. The rules are not applied if you just define them. The definition is used when validation is triggered.
If your model extends yii\db\ActiveRecord
then the validate()
method is called during its save()
method unless you've passed false
in its $runValidation
parameter.
But in case your model extends yii\base\Model
directly, you have to call the validate()
method explicitly.
In your case you can add the validate()
call in your controller like this:
if (
$registerModel->load(Yii::$app->request->post())
&& $registerModel->validate()
&& $registerModel->paperAddReviewer()
) {
return $this->redirect(['/paper/reviewer/reviewer']);
}
Or you can make it part of your PaperAddUserForm::paperAddReviewer()
method like this:
public function paperAddReviewer()
{
if (!$this->validate()) {
return false;
}
$this->addUser(AuthRolesEnum::OC_REVIEWER);
return true;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论