I have got to be missing something (or many things) here. I can never get the built-in Ajax functionality to work in Yii. I always end up having to add my own jQuery to handle things which seems to defeat the purpose. So, I figure I’m missing something.
In this case, I’m trying to create a VERY simple form for submitting a new email address. I want to send the data via Ajax, so I’m using the ajaxSubmitButton. Based on samples and tutorials I’ve looked at, I was expecting the ajaxId to be passed along so that I can check against it in the controller. But I am only getting the text input for the email address, and no validation has been happening, no matter what I do to set those options in the code.
Here is my form:
<?php
$emailModel = new Email;
$form = $this->beginWidget('CActiveForm', array(
'id'=>'email-form',
'action'=>'site/signup',
'enableAjaxValidation'=>false,
'enableClientValidation'=>false,
'clientOptions'=>array('validateOnSubmit'=>true, 'ajaxVar'=>'ajax'),
'htmlOptions'=>array('onsubmit'=>'return false'),
));
echo $form->errorSummary($emailModel);
echo $form->labelEx($emailModel, 'address');
echo $form->textField($emailModel, 'address');
echo $form->error($emailModel, 'address');
echo CHtml::ajaxSubmitButton('count me in!',
$this->createUrl('site/signup'),
array('type'=>'post'),
array('class'=>'yes')
);
$this->endWidget();
?>
For now, I’ve set any validation to false, just to try to get things working. Here is my controller function:
public function actionSignup() {
if(isset($_POST['ajax'])) {
echo "Ajax!";
} else {
echo "Nope";
}
}
As you can see, I’m simply checking for that $_POST[‘ajax’] value. I’m not even interested, right now, in the response. Truth be told, it’s not utterly necessary that I have that value, but when I saw that validation wasn’t working, I got suspicious about that ID being missing, as well. I’ve looked at the JavaScript that is produced in the source code by the ajaxSubmitButton, and I really don’t see anything that would send along that ID. I guess I’ll worry about validation later, too, unless someone sees something terribly wrong, here (besides setting validation to false).
BTW, in my rules, I’ve included:
array('address', 'required')
which is the only thing I’m checking, right now.
Anybody understand how that $_POST[‘ajax’] value gets sent?
Thanks!