В форме serial[] является массивом(serial).
Пишу правило
Код: Выделить всё
['serial', 'each', 'rule' => ['string', 'max' => 255]],
Код: Выделить всё
[['Id_matetial ', 'serial'],'unique', 'targetAttribute' => ['Id_matetial ', 'serial']]
Код: Выделить всё
['serial', 'each', 'rule' => ['string', 'max' => 255]],
Код: Выделить всё
[['Id_matetial ', 'serial'],'unique', 'targetAttribute' => ['Id_matetial ', 'serial']]
Код: Выделить всё
['serial','each','rules'=>['unique','targetAttribute'=>['serial','id_material']]]
Код: Выделить всё
class TestController extends \yii\web\Controller
{
public function actionIndex()
{
$model= new \frontend\models\material\TestModel();
if ($model->load(Yii::$app->request->post()) && $model->act()) {
$model=$model->act();
}
return $this->render('index', [
'model' => $model,
]);
}
}
Код: Выделить всё
namespace frontend\models\material;
use Yii;
/**
* This is the model class for table "test".
*
* @property int $id
* @property int $number
* @property string $name
*/
class TestModel extends \yii\db\ActiveRecord
{
/**
* {@inheritdoc}
*/
public static function tableName()
{
return 'test';
}
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['number', 'name'], 'required'],
[['number'], 'integer'],
//[['name'], 'string', 'max' => 80],
//[['number', 'name'], 'unique', 'targetAttribute' => ['number', 'name']],
['name', 'each','rule'=>['string']],
['name','my'],
['name','each','rule'=>['unique', 'targetAttribute' => ['number', 'name']]],
];
}
/**
* {@inheritdoc}
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'number' => 'Number',
'name' => 'Name',
];
}
public function my()
{
$error=array();
$as=array();
foreach ($this->name as $key => $value) {
if(array_search($value, $as)===false){
$as[]=$value;
}
else {
$error[]=$key;//пишем ключи у каких полей повторения
}
}
$this->addErrors(['name'=>$error]);
}
public function act()
{
if (!$this->validate()) {
return null;
}
return $this;// фиг знает что возврощать
}
}
Код: Выделить всё
use yii\helpers\Html;
use yii\widgets\ActiveForm;
/* @var $this yii\web\View */
?>
<h1>test/index</h1>
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'number')->input('number')?>
<?php
for ($index = 0; $index < 3; $index++) {
echo $form->field($model, 'name[]')->textInput();
}
?>
<?= Html::submitButton('Создать', ['class' => 'btn btn-success','id'=>'create-bt']) ?>
<?php ActiveForm::end(); ?>
Код: Выделить всё
array{
0=>[
p1=>1,
p2=>1,
number=>2,
name=>'a'
}
1=>[
p1=>1,
p2=>1,
number=>1,
name=>'a'
}
1=>[
p1=>1,
p2=>1,
number=>1,
name=>'v'
}
}
Код: Выделить всё
select * from test where (number='' and name='') or (number='' and name='') or....
А что там много писать? Получаете из БД массив имён-дубликатов и на них же ругаетесь. Сравнивать с моделью уже не надо, вы же поисковый запрос для этого и составляете.потом циклы писать на сравнение массива модели и на вывод запроса
Код: Выделить всё
createCommand()->batchInsert()
Код: Выделить всё
for ($index = 0; $index < 3; $index++) {
echo $form->field($model, 'number[]')->textInput()
echo $form->field($model, 'name[]')->textInput();
}
Код: Выделить всё
for ($index = 0; $index < 3; $index++) {
$val="";
if($model->name){ // то есть если не пусто значит был Post
$val=$model->name[$index];
}
echo $form->field($model, 'name[]')->textInput(['value'=>$val]);
}
Код: Выделить всё
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
/* @var $this yii\web\View */
?>
<h1>test/index</h1>
<?php
echo '<pre>';
echo \yii\helpers\VarDumper::dump($model->errors);
echo '</pre>';
$form = ActiveForm::begin();?>
<?= $form->field($model, 'number')->textInput()?>
<?php
for ($index = 0; $index < 3; $index++) {
echo $form->field($model, 'name['.$index.']',)->textInput();
}
?>
<?= Html::submitButton('Создать', ['class' => 'btn btn-success','id'=>'create-bt']) ?>
<?php ActiveForm::end(); ?>
<?php ?>
Код: Выделить всё
public function rules()
{
return [
['name', 'each','rule'=>['string']],
['name', 'each','rule'=>['required']],
];
}
Note: EachValidator is meant to be used only in basic cases, you should consider usage of tabular input, using several models for the more complex case.