Multiple values in textArea

Say I have attribute "produce" and then I have a form with a textArea with this attribute.

Is it possible to enter "watermelon" in the textArea, then separate it by a comma or something (line break?) and enter "apple".

And then save the form and I now have 2 new entries in my database.

This is just a simple example, is this possible and can anyone point me into the right direction because I’m sure this will require alot of work.

I also understand there is tabular input, etc. But tabular input uses multiple text fields. I only want to use a textArea.

Thanks.

Whatever is entered in the textarea you can "process" and do whatever you need…

For example if you use




$form->textarea($model,'produce');

In $model->produce you get whatever is entered in the textarea.

Now it’s up to you to process that information…

If you want to get all the values that are separated by a comma you can use something like




$allproduces = explode(',', $this->produce);

foreach($allproduces as $oneproduce)

   ...



Well the problem with that is (using Yii CRUD generated files here), if I enter in the textArea:

Watermelon

Apple

Both of them get put into the same ID. How does one make them both 2 new entries/2 new IDs without using impload?

The reason I need this, is if I need to update “Apple” down the road and I don’t want to update Watermelon.

Check again my previous post… you just save the data in the foreach loop…

Thank you. Now, this goes into my _form correct?

You really need to read the Definitive Guide to Yii - http://www.yiiframework.com/doc/guide/

And learn about the MVC…

As for that code… it goes where you want to save the data… so it goes in the controller… the _form file is the view… there is the code that shows the form on the page…

Thank you. Yii is the first MVC framework that I’ve actually spent alot of time on and I’m really liking it!

I was busy and couldn’t experiment with this. Now I’m trying it and I can’t seem to get the “other” values to save. Only the first one.

So I got my controller code like this:


public function actionCreate()

	{

    	$model=new Names;


    	// Uncomment the following line if AJAX validation is needed

    	// $this->performAjaxValidation($model);


    	if(isset($_POST['Names']))

    	{

            $model->attributes=$_POST['Names'];

    	

      	$name = explode('/n', $model->name);  

          foreach($name as $model->name)


      	

          

            if($model->save())


 	

    	$this->redirect(array('view', 'id'=>$model->id));

    	}


    	$this->render('create',array(

            'model'=>$model,

            

    	));

	} 

Can anyone point the error in my code?

The /n is for a new line (pressing enter on keyboard)

So if I enter in textArea:

Apple

Orange

Only Apple will save and Orange isn’t even passed/saved. Not sure why.





public function actionCreate()

        {

        $model=new Names;


        // Uncomment the following line if AJAX validation is needed

        // $this->performAjaxValidation($model);


        if(isset($_POST['Names']))

        {

            $model->attributes=$_POST['Names'];

        

          $names = explode('/n', $model->name);  

          

          $allSaved = true;


          foreach($names as $name) 

          if(!empty($name)) // don't save empty lines

          {

            $newName=new Names; //That's what you want: an extra model for each name

            $newName->name = $name;

            

            if(!$newName->save()) 

            {

              $allSaved = false;

              break; //abort on first error (or continue here if you want to save other models)

            }

          }


          if($allSaved) 

            $this->redirect(array('view', 'id'=><img src='http://www.yiiframework.com/forum/public/style_emoticons/default/huh.gif' class='bbc_emoticon' alt='???' />?)); //what model(s) do you want to show here: allnew, the latest, the first <img src='http://www.yiiframework.com/forum/public/style_emoticons/default/huh.gif' class='bbc_emoticon' alt='???' />

        }


        $this->render('create',array(

            'model'=>$model,

            

        ));

        } 




But there is more work:

What do you want to show on ‘view’ afterwards?

->All new models or the one with the collected names?

You have to use a ‘groupId’ or ‘flag’ to show all new inserted.

What do you want to show on update?

->Maybe you have to implement the onAfterFind() to collect multiple names into one textarea

The problem is, your db-model should not be the same as your form-model.

You should implement an extra ‘FormModel’ for the actionCreate (with the textarea) and work with (maybe master/detail) in the db (has_many relation).

Thank you Joblo, this works! I understand what you’re saying. I am using something similar to your multimodel form setup with Groups, etc.

Other concern… when using “/n” it doesn’t work. I can use comma ",’ but no newline. Any thoughts on this?

As referenced here: http://php.net/manua...nction.trim.php

EDIT: Nevermind, I have used "\r\n" and it works.

carriage return first then newline.

EDIT2: For those who are curious on how to add more then just "name":

Add new explode…

$lastname = explode("\r\n", $model->lastname);

Define a key, in this case "name": (replace current foreach with this)


 foreach($names as $key => $name) 

Then below $newName->name = $name;

add

$newName->lastname= $lastname[$key];

Hi,

how can i use update Model in "field[ ]" field

inside of textArea

??




<?php

foreach ($POST  as $field) {

  $id = Test::Model()->find($field->id);

  $model->field = $id->field;

  echo $form->textArea($model, "field[]");

}

?>



basically it would be the problem…