Keep form textfield input on invalid submission

Hello everyone, sorry for what is probably a simple question, but I want my form textfields to remember input on invalid submission. For example, in the following form I want the ‘emails’ textfield not to erase any data the user has entered if the required ‘name’ field has been left blank and a submission is attempted.

Would this sort of validation require AJAX or Javascript?

I am extending CFormModel.

I don’t believe there to be any relevant code in the controller, so I left it out.

Here’s the widget in the view:




<?php $form=$this->beginWidget('CActiveForm', array(

  'id'=>'this-form',

  'enableClientValidation'=>true,

  'clientOptions'=>array(

    'validateOnSubmit'=>true,

    ),

  )); 

?>


<?php echo $form->errorSummary($model); ?>


<div class="row">

  <?php echo $form->error($model,'name'); ?>

  <?php echo $form->labelEx($model,'name (required)'); ?>

  <?php echo $form->textField($model,'name'); ?>

</div>


// takes in comma separated emails

<label for="emails">Emails:</label>

<input type="text" name="emails">


<div class="row buttons">

  <?php echo CHtml::submitButton('Create'); ?>

</div>


<?php $this->endWidget(); ?>



Here’s the relevant model code:




class ThisForm extends CFormModel

{

 public function rules()

 {

  return array(

  // project name is required

  array('name', 'required'),

  );

 }

}



Was it something I said?

On the contrary, we need to see controller’s action.

I figured it out. It did indeed relate to my controller.

Relavant controller snippet:




...

...

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

      "emails"=> isset($_POST['emails']) ? $_POST['emails'] : '',

    ));



And here’s the updated view snippet:




<input type="text" name="emails" value='<?php echo CHtml::encode($emails); ?>'>