Textarea value not saved

LS.

This is probably something well known regarding textarea in HTML but currently I am running around in circles. Hopefully you can help!

In my form I have created a textarea:


<?php echo $form->textArea($model1,'Totalview',array('rows'=>'28','cols'=>'56')); ?> 

Totalview is a property of $model1 and resides in the MySQL database. (Any thoughts on which data type is best?)

Somewhere in my Javascript (using jQuery) I fill the textarea using:


$('#Offer_Totalview').val(textForOverview); 

This seems to be working fine. I see the text in the textarea in my HTML form. However when I save my model to the database, the value of my textarea is not saved. Before saving, I loop over the model to see what data the fields hold. And indeed $model1->Totalview is empty.

When I give $model1->Totalview a value manually, the value is saved to the database without problems.

My guess is that I shouldn’t use $(’#Offer_Totalview’).val(textForOverview) but what then? I have also used $(’#Offer_Totalview’).html(textForOverview)which does show the text but nothing in the database.

Thank you in advance!

To be clear: when I use


$('#Offer_Totalview').html(textForOverview);

and read the value of the property when saving, I do see that $model1->Totalview has the value that was set using jQuery. Nothing in the database however…

I don’t think textareas have values. Their content is set between the <textarea></textarea> tags. However, this conversation implies that jQuery will handle that for you.

I haven’t tested it, but try


$("#Offer_Totalview").text(textForOverview);

Matt

Can you post your controller code??

Is your Totalview attribute in the safe array (in your Offer model)?

Thank you all for replying! Currently I am not able to get at my code but

  • I will post my controller code in 14 hours …

  • Totalview is not in the safe array

  • I will try the jQuery text method, however, I have seen that the Totalview property of my model has the value as entered in the form…

To be continued…

Well it should be in order to massively assign the attributes to the model instance you’re probably creating in your controller.

Besides, it’s normal that jQuery returns the value entered in the form, since it’s DOM manipulation.

My bad, totalview is in the ‘safe’ array. The code of my controller basically comes down to this:


$valid=$model1->validate();

if($valid){

  if($model1->save(false)){

    $this->saveResponsesPrint($model1);  

    $this->saveResponsesOnline($model1);

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

  }

}



So like I said before, I do see the value of the variable of my $model1 when I loop over it like so:

$myFile = "C:\\aaa.txt";

$fh = fopen($myFile, ‘w’) or die(“can’t open file”);

foreach($_POST[‘Offer’] as $myField => $myValue){

fwrite(&#036;fh, &quot;Field: &quot;.&#036;myField.&quot; &#092;tValue:&quot; . &#036;myValue.&quot;&#092;n&quot;); // 

fclose($fh);

Output:

Field: Totalview Value:1234789

Something must go wrong when writing to the database, however I cannot find an error message in the mysql_error.log …

I think I am dead-ended here … I am going for an alternate route.

Thank you all for helping though!

Where are you assigning the POST values to the $model?




$model = new Model();


if (isset($_POST('ModelName')))

{


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

    

    if ($model->save())

    {

          //

    }

}


echo $model->fieldToCheck;

Matt

Hi Matt, thanks for taking the time! Indeed I was:


if(isset($_POST['Offer'], ))

{

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

}

The weirdest thing… I have create a temporary value/property and given it the value of the textarea. That was saved to the database. When opening I reverse this. And everything works fine now. :blink: Don’t ask me why it works but it does. Maybe when I have more time I will try to figure out why.

I had the same problem. I can see it in POST dump, but can’t assign it to the model.

I think the problem is in unavailability of the rules for these fields. I’ve solved it by adding textarea to required.




	public function rules()

	{

		return array(

			array('description, short_description', 'required'),

			// some rules

                        array('id, description, short_description', 'safe', 'on'=>'search'),

		);

	}



But I think there is more correct solution.

As bennouna have mentioned before, you can use ‘safe’ validation rule (if you don’t need this field to be required one).

Unfortunately it doesn’t work in my case. I’ll try to find the reason.

So I did it. The problem was in my poor understanding of safe validation rules.I found an explanation here.

I had to define a scenario for the rules array. Like this:




public function rules() {

  return array(

    array('name, code', 'required'),				

    // some rules

    array('id, description, short_description', 'safe', 'on'=>'edit'),

  );

}