Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clone column with dropdown selection #304

Open
CanadaDream opened this issue Jul 25, 2019 · 5 comments
Open

Clone column with dropdown selection #304

CanadaDream opened this issue Jul 25, 2019 · 5 comments
Milestone

Comments

@CanadaDream
Copy link

CanadaDream commented Jul 25, 2019

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

@unclead
Copy link
Owner

unclead commented Jul 26, 2019

post an example of the code to reproduce the issue

@CanadaDream
Copy link
Author

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 :)

@jrguevara
Copy link

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

@CanadaDream
Copy link
Author

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

@Maxi-M
Copy link

Maxi-M commented Feb 14, 2020

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.

@unclead unclead added this to the 3.0 milestone Jun 26, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants