我修改我的部门网站允许用户输入作者和排名。
但是当我在排名文本中键入非整数时,它无法显示任何验证错误消息。
array('dataset_id, author_id, rank', 'numerical', 'integerOnly'=>true),
<?php echo $form->error($model,'rank'); ?>
当我添加
时也在控制器文件中 if ($author['rank'] == $rank && $author['name'] == $name) {
$model->addError("warning", "Duplicate input");
在视图文件中也无法显示任何东西,只需刷新页面空白文本字段
<?php echo $form->errorSummary($model); ?>
这是DatasetAuthor模型规则
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('dataset_id ,rank,author_name', 'required'),
array('dataset_id, author_id, rank', 'numerical', 'integerOnly'=>true),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('id, dataset_id, author_id, doi_search, author_name_search , orcid_search , rank_search', 'safe', 'on'=>'search'),
);
}
控制器功能
public function actionCreate1() {
$model = new DatasetAuthor;
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
//this is fake information
$model->dataset_id = 1;
//update
if (!isset($_SESSION['authors']))
$_SESSION['authors'] = array();
$authors = $_SESSION['authors'];
if (isset($_POST['DatasetAuthor'])) {
$ranks = trim($_POST['DatasetAuthor']['rank']);
$names = trim($_POST['DatasetAuthor']['author_name']);
if (substr($names, -1) == ";")
$names = substr($names, 0, -1);
$valid = true;
$namearray = array();
if ($names != "")
$namearray = explode(";", $names);
// var_dump($namearray);
if ($ranks == "")
$rankarray = array();
else
$rankarray = explode(";", $ranks);
// var_dump($rankarray);
// if (count($namearray) != count($rankarray)) {
// $model->addError("error", "the number of name and rank are different!");
// $valid = false;
// }
//test names
if ($valid) {
foreach ($namearray as $name) {
if ($name == "") {
$model->addError("author_name", "name can't be blank!");
$valid = false;
break;
}
}
}
//test ranks
if ($valid) {
foreach ($rankarray as $rank) {
// if ($rank == "") {
// $model->addError("error", "rank can't be blank!");
// $valid = false;
// break;
// }
if (!is_numeric($rank)) {
$model->addError("rank", "rank should be an integer!");
$valid = false;
break;
}
}
}
// var_dump(count($rankarray)." test");
if ($valid) {
foreach ($namearray as $index => $name) {
if ($index < count($rankarray)) {
$rank = $rankarray[$index];
} else {
$rank = 1;
while (true) {
$found = true;
//find the maximum one in the authors array
foreach ($authors as $author) {
if ($author['rank'] == $rank){
$found = false;
break;
}
}
if($found)
break;
$rank++;
}
}
$valid = true;
//check if there is duplicate input
foreach ($authors as $key => $author) {
if ($author['rank'] == $rank && $author['name'] == $name) {
$model->addError("rank", "Duplicate input");
$valid = false;
break;
}
}
if ($valid) {
//store author dataset
$model->rank = $rank;
$model->author_name = $name;
$id = 0;
if ($this->storeAuthor($model, $id)) {
$newItem = array('rank' => $rank, 'id' => $id, 'name' => $name);
array_push($authors, $newItem);
$_SESSION['authors'] = $authors;
$vars = array('authors');
Dataset::storeSession($vars);
} else {
$model->addError("error", "database operation failure, please log out first and log in again.");
}
}
}
}
}
// $model = new DatasetAuthor;
$author_model = new CArrayDataProvider($authors);
$model = new DatasetAuthor;
$this->render('create1', array(
'model' => $model,
'author_model' => $author_model,
));
}
视图
<?php echo $form->errorSummary($model); ?>
<?= $form->textField($model, 'author_id') ?>
<?php echo $form->error($model, 'author_id'); ?>
<?= $form->textField($model, 'rank',array('size' => 60, 'maxlength' => 200)) ?>
<?php echo $form->error($model, 'rank'); ?>
答案 0 :(得分:1)
CModel::addError()
的第一个参数必须是类属性的名称。在您的情况下,name
和/或rank
都可以。
if ($author['rank'] == $rank && $author['name'] == $name) {
$model->addError("rank", "Duplicate input");
<强>更新强>
从控制器代码中,您没有将表单数据传递到模型中。这就是字段为空的原因。另外,您不会调用实际检查$model->save()
的{{1}}或$model->validate()
。调用这些将不再需要您在控制器中添加的大多数有效性检查。
rules()