phone_field.php 4.37 KB
<?php
    use yii\helpers\Html;

?>
<fieldset>

    <div class="field_list">

        <?php $t = 0;

            $label = 0; // add this var

            for($i = 1; $i <= count($model); $i++):
                $row = $i;
                ?>
                <?= Html::beginTag('div', [
                'class' => 'form-group form-group-phone-' . ( isset( $model[ $i ][ 'parent_key' ] ) ? $model[ $i ][ 'parent_key' ] : 0 ),
                'id'    => isset( $model[ $i ][ 'parent_key' ] ) ? $model[ $i ][ 'parent_key' ] : 0,
            ]) ?>
                <div class="input-blocks">
                    <label for="cont-phone-<?= ++$label ?>">Телефон</label>
                    <input id="cont-phone-<?= $label ?>" type="tel" placeholder="+xx(xxx)xxx-xx-xx" class="form-control custom-input-2" value="<?= isset( $model[ $t ][ 'value' ] ) ? $model[ $t ][ 'value' ] : '' ?>" name="Fields[phone][<?= $row ?>][0][phone]"/>
                </div>
                <div class="help-block"></div>
                <span data-id="<?= isset( $model[ $i ][ 'parent_key' ] ) ? $model[ $i ][ 'parent_key' ] : 0 ?>" title="<?= Yii::t('app', 'add') ?>" class="glyphicon glyphicon-trash delete-field-item"></span>
                <?php
                $js = "
                        var id = 'cont-phone-" . $label . "';
                        $('#w0').yiiActiveForm('add', {
                            container: '.form-group-phone-" . ( isset( $model[ $i ][ 'parent_key' ] ) ? $model[ $i ][ 'parent_key' ] : 0 ) . "',
                            error: '.help-block',
                            id: id,
                            input: '#'+id,
                            name: 'Fields[phone][" . $row . "][0][phone]',
                            validate: function(attribute, value, messages, deferred) {yii.validation.regularExpression(value, messages, {'pattern':/^\+?(?:\d{0,3})?[\(\s]?\d{0,5}[\)\s]?\d{3}[-\s]?\d{2}[-\s]?\d{2}$/,'not':false,'message':'Телефон введен неверно . ','skipOnEmpty':1});}
                        });
                    ";
                $this->registerJs($js, $this::POS_LOAD);
                ?>
                <?= Html::endTag('div') ?>
                <?php $i = ++$t; ?>
            <?php endfor; ?>

    </div>
    <p class="btn btn-success add_field_<?= $this->context->id ?>">Добавить еще</p>

</fieldset>
<?php
    $js = "
            var start_i_" . $this->context->id . " = " . $i . ";
            var start_label_" . $this->context->id . " = " . $label . ";
            $(document).on('click', '.add_field_" . $this->context->id . "', function() {
                var block = $(this)
                    .siblings('.field_list');
                    var block_id = $(this).parent('fieldset');
                    ++start_label_" . $this->context->id . ";
                    var sub_block = '<div class=\"form-group form-group-phone-'+start_label_" . $this->context->id . "+'\" >' + '<div class=\"input-blocks\">' + '<label for=\"cont-phone-' + start_label_" . $this->context->id . " + '\">Телефон</label>' + '<input id=\"cont-phone-' + start_label_" . $this->context->id . " + '\" type=\"tel\" placeholder=\"+xx(xxx)xxx-xx-xx\" class=\"form-control custom-input-2\" value=\"\" name=\"Fields[phone][' + start_i_" . $this->context->id . "++ + '][0][phone]\" />' + '</div><div class=\"help-block\"></div><span class=\"glyphicon glyphicon-trash delete-field-item custom-remove-ico\"></span>' + '<div>';
                    block.append(sub_block);
                    $('#w0').yiiActiveForm('add', {
                        container: '.form-group-phone-'+start_label_" . $this->context->id . ",
                        error: '.help-block',
                        id: 'cont-phone-'+start_label_" . $this->context->id . ",
                        input: '#cont-phone-'+start_label_" . $this->context->id . ",
                        name: 'Fields[phone][' + start_label_" . $this->context->id . "++ + '][0][phone]',
                        validate: function(attribute, value, messages, deferred) {yii.validation.regularExpression(value, messages, {'pattern':/^\+?(?:\d{0,3})?[\(\s]?\d{0,5}[\)\s]?\d{3}[-\s]?\d{2}[-\s]?\d{2}$/,'not':false,'message':'Телефон введен неверно . ','skipOnEmpty':1});}
                    });
            });
            ";
    $this->registerJs($js, $this::POS_LOAD);
?>