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'),
);
}
}