I
I
ImPuuLsE2015-03-09 11:44:04
Yii
ImPuuLsE, 2015-03-09 11:44:04

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
        )

)

Here is the model:
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 )
            ),

How to write this validateAttr() validator correctly?
Here's the form just in case:
<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>

And the controller itself:
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

4 answer(s)
A
Alexander Makarov, 2015-03-23
@SamDark

The documentation describes: yiiframework.ru/doc/guide/ru/form.model

public function validateAttr($attribute,$params)
{
    $value = $this->$attribute;
    if (всё плохо) {
         $this->addError($attribute,'Всё плохо!');
    }
}

E
Egor Zhivagin, 2017-12-22
@Zombie42679

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?

L
lavezzi1, 2017-12-22
@lavezzi1

https://masonry.desandro.com/

A
Alexander, 2017-12-22
@Pantifik13

I would do something like this

.cont{
  display: flex;
  flex-wrap: wrap;
  flex-direction: column;

  height: 100px;
}
.item{
  flex: 1 1 25%;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question