E
E
EVOSandru62015-10-07 13:35:37
Yii
EVOSandru6, 2015-10-07 13:35:37

How to validate elements of an array of a linked model in yii?

Good afternoon,
In the form of creating a hotel, you need to fill it with numbers. It is desirable that when adding each number item, client validation takes place according to the contents of its fields. There are 2
classes : Hotels and Rooms
. _ _ _ _ _ _ _ _ _ _ _
'rooms' => [self::HAS_MANY, 'Rooms','hotel_id'],

[['rooms'],'safe']

public $rooms;

<?php
    $this->widget('ext.jqrelcopy.JQRelcopy',array(
        //the id of the 'Copy' link in the view, see below.
        'id' => 'add_room',
        //add a icon image tag instead of the text
        //leave empty to disable removing
        'removeText' => 'Удалить',
        //htmlOptions of the remove link
        'removeHtmlOptions' => array('style'=>'color:red'),
        //options of the plugin, see http://www.andresvidal.com/labs/relcopy.html
        'options' => array(
            //A class to attach to each copy
            'copyClass'=>'newcopy',
            // The number of allowed copies. Default: 0 is unlimited
            'limit'=>5,
            //Option to clear each copies text input fields or textarea
            'clearInputs'=>true,
            //A jQuery selector used to exclude an element and its children
            'excludeSelector'=>'.skipcopy',
            //Additional HTML to attach at the end of each copy.
            'append'=>CHtml::tag('span',array('class'=>'hint'),'Вы можете удалить эту строку'),
        )
    ));?>

    <button type="button" class="btn btn-danger" data-toggle="collapse" data-target="#demo">
        <?php echo $form->labelEx($model, 'rooms'); ?>
    </button>

    <div  id="demo" class="collapse">
        <div class="add-room">
            <table class="table">
                <tr>
                    <td>
                        <?php echo CHtml::label('Название',''); ?>
                    </td>
                    <td>
                        <?php echo CHtml::label('Кол-во комнат <span class="badge">шт.</span>',''); ?>
                    </td>
                    <td>
                        <?php echo CHtml::label('Стоимость от отеля <span class="badge">тг.</span>',''); ?>
                    </td>
                    <td>
                        <?php echo CHtml::label('Стоимость от фирмы <span class="badge">тг.</span>',''); ?>
                    </td>
                    <td>
                        <?php echo CHtml::label('Свободная продажа',''); ?>
                    </td>
                    <td>
                        <?php echo CHtml::label('Завтрак',''); ?>
                    </td>
                    <td>
                        <?php echo CHtml::label('Рацион',''); ?>
                    </td>
                </tr>
                <tr>
                    <td>
                        <?php echo CHtml::textField('Hotels[rooms][name][]',''); ?>
                    </td>
                    <td>
                        <?php echo CHtml::numberField('Hotels[rooms][beds_count][]', '',
                            [
                                'placeholder'=>'шт. '
                            ]); ?>
                    </td>
                    <td>
                        <?php echo CHtml::numberField('Hotels[rooms][price][]', '',
                            [
                                'placeholder'=>'тг. '
                            ]); ?>
                    </td>


                    <td>
                        <?php echo CHtml::numberField('Hotels[rooms][price_rr][]', '',
                                [
                                    'placeholder'=>'тг. '
                                ]
                            ); ?>
                    </td>
                    <td>
                        <?php echo CHtml::checkBox('Hotels[rooms][is_freesale][]', ''); ?>
                    </td>
                    <td>
                        <?php echo CHtml::checkBox('Hotels[rooms][is_breakfast][]', ''); ?>
                    </td>
                    <td>
                        <?php echo CHtml::dropDownList('Hotels[rooms][food_id][]', '', CHtml::listData($food, 'id', 'name')); ?>

                    </td>

                </tr>


            </table>
        </div>
        <a id="add_room" href="#" class="btn btn-primary btn-lg" rel=".add-room">+</a>
    </div>

This widget is needed to add homogeneous entities, then, when the action pulls up $_POST , the related data is saved where necessary ( numbers in your table).
The problem is that the validation is not pulled from the Rooms
class . How can this be fixed?

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
V
vyachin, 2016-06-13
@EVOSandru6

replace [['rooms'],'safe'] with something like ['rooms', 'validateRooms']
public function validateRooms()
{
foreach($this->rooms as $room) {
$model = new Room();
$model->load($room, '');
if (!$model->validate())
{
$this->addErrors($model->getErrors());
}
}
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question