autogenerated id in ajax validation

Hello,

If I create a CActiveForm textfield and specify an id in the htmlOptions parameter, how can I generate the correct js for the ajax validation??

I have


$form->textField($model, 'tidstart', array('id'=>$id));

but the id in the js is autogenerated and does not match $id.

It’s not good enough for me to let textField autogenerate the id, because I have many of the same inputfields.

Specifically I have a calendar and for each day I have some code with inputfields that should open in a popup

I hope I make myself clear.

Regards Mads

Your code should words, I just checked Yii code, if ‘id’ in htmlOptions is set, then code will not assign autogenerated id to this element.

So, maybe your $id variable is empty, and that could be reason why are you getting autogenerated ids.

If ‘id’ is a field in your table… you maybe wanted to use $model->id ?

Hello,

Thanks for your replies. $id is just a variable I generate. I just make sure it’s unique for each textField.

The id of the textField works fine, but it’s a problem in the autogenerated javascript for ajax validation.

Specifically a have a bunch of div-blocks each containing a textField with a unique id:




<div class="holder">

    <label for="RegistrationForm_tidstart" class="required">Fra kl. <span class="required">*</span</label>

	<span class="text">

  	      <input id="regform_tidstart_1314655200" name="RegistrationForm[tidstart]" type="text" value="" />

              <div id="regform_tidstart_1314655200" class="errorMessage" style="display:none"></div>

        </span>

</div>



And here is some of the javascript for validation:




$('#dreg-form').yiiactiveform({'attributes':[{'id':'RegistrationForm_tidstart','inputID':'RegistrationForm_tidstart','errorID':'regform_tidstart_1314741600','model':'RegistrationForm','name':'RegistrationForm[tidstart]','enableAjaxValidation':true,'clientValidation':function(value, messages, attribute) {


if($.trim(value)=='') {

	messages.push("Fra kl. cannot be blank.");

}



As I can see the id in the javascript doesn’t match the id in the input-tag, because I have my own id as a parameter in $form->textField.

Regards Mads

OK, it seems to work when you give your form a unique id also.




$form = $this->beginWidget('CActiveForm', array('id'=>'dreg-form_' . $id,

						'action'=>'/negr/registration/dreg',

						'enableAjaxValidation'=>true,

						'enableClientValidation'=>true,

						'focus'=>array($model,'tidstart'),)); 



I had the same problem here. Your title is a bit more direct to the issue. I was trying to manually set the id to prevent multiple forms with duplicate id’s.

I found that not using the autogenerated id breaks the ajax validation since it expects a specific field name and doesn’t adjust the js to look for the specified id name. Somehow the ajax validation response would need to know the field name, or at least be able to translate the response to the proper field name. There should some mapping array so specified field names can be used.

Does anyone have a solution for this? Or is there a bug report filed for it that I couldn’t find?