Answer the question
In order to leave comments, you need to log in
How to validate an array of data in Yii?
Hello everybody! How to properly validate such an array of data:
Array
(
[firstName] =>
[phone] =>
[email] =>
[productCode] => Array
(
[0] => 123
[1] => 456
)
)
array(
'productCode,
firstName,
email,
phone', 'required', 'on' => self::getScenarioName( self::FORM_TYPE_REQUET_AVAILABILITY )
),
array(
'productCode', 'validateAttr', 'on' => self::getScenarioName( self::FORM_TYPE_REQUET_AVAILABILITY )
),
<div class="row">
<?php echo CHtml::activeLabel($model,'firstName'); ?>
<?php echo CHtml::activeTextField($model,'firstName'); ?>
<?php echo CHtml::error($model,'firstName'); ?>
</div>
<div class="row">
<?php echo CHtml::activeLabel($model,'phone'); ?>
<?php echo CHtml::activeTextField($model,'phone'); ?>
<?php echo CHtml::error($model,'phone'); ?>
</div>
<div class="row">
<?php echo CHtml::activeLabel($model,'email'); ?>
<?php echo CHtml::activeTextField($model,'email'); ?>
<?php echo CHtml::error($model,'email'); ?>
</div>
<table>
<tr><th>Код товара</th><th>Количество</th><th>Замена</th></tr>
<?php for($i =0; $i<2; $i++):?>
<tr>
<td>
<?php echo CHtml::activeTextField($model,"productCode[$i]"); ?>
<?php echo CHtml::error($model,"productCode[$i]"); ?>
</td>
</tr>
<?php endfor; ?>
</table>
if( Yii::app()->request->getParam('Questions') ){
$count = count($_POST['Questions']['productCode']);
for($i = 0; $i<$count; $i++){
$model->firstName = $_POST['Questions']['firstName'];
$model->email = $_POST['Questions']['email'];
$model->phone = $_POST['Questions']['phone'];
$model->productCode = $_POST['Questions']['productCode'][$i];
$model->attributes=$_POST['Questions']['productCode'][$i];
if( $model->validate() && $model->save() ){
$model->send();
echo 1;
Yii::app()->end();
}
}
}
Answer the question
In order to leave comments, you need to log in
The documentation describes: yiiframework.ru/doc/guide/ru/form.model
public function validateAttr($attribute,$params)
{
$value = $this->$attribute;
if (всё плохо) {
$this->addError($attribute,'Всё плохо!');
}
}
Masonry is the most obvious solution.
I hate this design feature, because you have to make it with your hands and you have to install fucking Masonry. Why not put them exactly, with one size?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question