yii2-multiple-input icon indicating copy to clipboard operation
yii2-multiple-input copied to clipboard

Clone column with dropdown selection

Open CanadaDream opened this issue 6 years ago • 5 comments

I have several dropdown menu columns in my form and I would like to clone the selected input.

Is that possible?

I would like to have this after I have cloned the results: image

But I get: image

CanadaDream avatar Jul 25 '19 23:07 CanadaDream

post an example of the code to reproduce the issue

unclead avatar Jul 26 '19 08:07 unclead

My model.php file:

<?php

`namespace app\models;`

use Yii;

/**
* This is the model class for table "{{%sample_information}}".
*
* @property int $collection_site
* @property string $crpc
* @property string $priority
*


* @property CollectionSiteInformation $collectionSite

 */
class SampleInformation extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return '{{%sample_information}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['collection_site''], 'integer'],
            [['crpc', 'priority'], 'string', 'max' => 265],
            [['collection_site'], 'exist', 'skipOnError' => true, 'targetClass' => CollectionSiteInformation::className(), 'targetAttribute' => ['collection_site' => 'id'], 'message'=>Yii::t('app',"This collection site doesn`t exist.")]
        ];

/**
* {@inheritdoc}
*/
public function attributeLabels()
    {
        return [
            'collection_site' => 'Collection Site',
            'crpc' => 'CRPC',
            'priority' => 'Priority',
        ];
    }

    /**
    * @return \yii\db\ActiveQuery
    */
    public function getCollectionSite() 
    { 
        return $this->hasOne(CollectionSiteInformation::className(), ['id' => 'collection_site']); 
    } 
    }

class ExampleModel extends SampleInformation

{
    /**
    * @var array
    */
    public $samples;

    public function init()
    {
        parent::init();

        $this->samples = [
            [
                'collection_site' => '',
                'crpc' => '',
                'priority' => '',
            ],
        ];
    }

    
    public function rules()
    {
        return [
            ['samples', 'required'],
        ];
    }

    public function attributes()
    {
        return [
            'samples',
        ];
    }

    public function scenarios()
    {
       return [
            self::SCENARIO_DEFAULT => $this->attributes()
        ];
    }
}

My controller.php file:

<?php

namespace app\controllers;

use Yii;
use app\models\Sampleinformation;
use app\models\SampleinformationSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;

/**
     * Creates a new Sampleinformation model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new Sampleinformation();
     
        if (isset($_POST['SampleInformation'])) {
            $p = Yii::$app->request->post();
            $allFormData = $p['SampleInformation'];
            $allFormData2 = $allFormData['samples'];

            foreach($allFormData2 as $item) {
                $collection_site_arr = $item['collection_site'];
                $crpc_arr = $item['crpc'];
                $priority_arr = $item['priority'];
            
                $sampleData = array('SampleInformation'=>array(
                    'collection_site' => $collection_site_arr,
                    'crpc' =>  $crpc_arr,
                    'priority' => $priority_arr,
                ));

                $model = new Sampleinformation();
                $model->load($sampleData);
                $model->save();
            }

                return $this->redirect(['index', 'id' => $model->sample_id]);
            }
            
            return $this->render('create', [
                'model' => $model,
            ]);
        }}

My form.php file:

<?php

use yii\helpers\Html;
use yii\helpers\ArrayHelper;
use app\models\CollectionSiteInformation;
use kartik\date\DatePicker;
use kartik\select2\Select2;
use kartik\form\ActiveForm;
use kartik\form\ActiveField;
use unclead\multipleinput\MultipleInput;


/* @var $this yii\web\View */
/* @var $model app\models\Sampleinformation */
/* @var $form yii\widgets\ActiveForm */

?>


<div class="sampleinformation-form">

    <?php $form = ActiveForm::begin(); ?>
 
    <p>
    Please note that all fields marked with an asterisk (<font color="red">*</font>) are required.
    </p>
 
    <?= $form->field($model,'samples')->widget(MultipleInput::class,[
        'max' => 32,
        'cloneButton' => true,
        'allowEmptyList'=> false,
        'columns' => [

            [
            'name'  => 'collection_site',
            'type' => Select2::class,
            'options' => [
                'initValueText' => ArrayHelper::map(CollectionSiteInformation::find()->all(),'id','collection_site'),],
            'title' => 'Collection site <font color="red">*</font>',
            ],
            [
            'name'  => 'crpc',
            'type'  => Select2::classname(),
            'title' => 'CRPC <font color="red">*</font>',
            'options' => [
                'data' => [1 => 'Yes', 2 => 'No', 3 => 'N/A', 4 => 'Not yet clarified'],
            ]],
            [
            'name'  => 'priority',
            'type'  => Select2::classname(),
            'title' => 'Priority <font color="red">*</font>',
            'options' => [
                //'pluginOptions' => ['placeholder' => 'Please select...'],
                'data' => [1 => 'Priority', 2 => 'High', 3 => 'Low', 4 => 'No action at present', 5 => 'Not yet determined'],
            ]],
        ]
        ])
        ->label(false);
    ?>
    
    <div class="form-group">
        <?= Html::submitButton('Save', ['class' => 'btn btn-success']) ?>
    </div>

    <?php ActiveForm::end(); ?>

</div>

Is there also an option to use a placeholder like "Please select..." and not the first item of the dropdown list as the "default value".

I really appreciate any idea how I could solve the issue posted above.

Thanks so much :)

CanadaDream avatar Jul 26 '19 16:07 CanadaDream

Have the same issue, did you solve it @CanadaDream ?

jrguevara avatar Nov 08 '19 15:11 jrguevara

Yes, I could solve the issue @jrguevara.

I changed the code in form.php from this:

<?= $form->field($model,'samples')->widget(MultipleInput::class,[
        'max' => 32,
        'cloneButton' => true,
        'allowEmptyList'=> false,
        'columns' => [

            [
            'name'  => 'collection_site',
            'type' => Select2::class,
            'options' => [
                'initValueText' => ArrayHelper::map(CollectionSiteInformation::find()->all(),'id','collection_site'),],
            'title' => 'Collection site <font color="red">*</font>',
            ],
        ]
    ])
    ->label(false);
?>

to that:

    <?= $form->field($model,'samples')->widget(MultipleInput::class,[
        'max' => 16,
        'cloneButton' => true,
        'allowEmptyList'=> false,
        'columns' => [

                [
                    'name'  => 'patient',
                    'type' => 'dropDownList',
                    'title' => 'Patient <font color="red">*</font>',
                    'items' => ArrayHelper::map(PatientInformation::find()->all(),'id','patient_id'),
                    'options' => [
                        'prompt'=>'Please select...'
                    ],
                ],
            ]
        ])
        ->label(false);
    ?>

Now it works perfectly.

However, I am now facing another issue. The error messages are not displayed if the validation rule fails. Do you have the same issue? If so, I am more than happy if you could help me solving this problem.

Thanks

CanadaDream avatar Nov 12 '19 20:11 CanadaDream

In regards to this issue, I still would like to know if there is any way to make a row copy with Select2 input? It works perfectly with a dropDownList, but not with Select2 or DepDrops::TYPE_SELECT2.

Maxi-M avatar Feb 14 '20 13:02 Maxi-M