Now I do a website with users invitation system. Administrator sends an invitation and once it’s done, user can register by link in the email he received. That is not important Just an intro…
So, I’ve done a form with appropriate fields and I want it to work with AJAX. So, I’ve placed CHtml::ajaxSubmitButton
Everything works, but what if I want to update a couple of blocks before and after the form? How can I do that? Also, if something is entered wrong (e-mail does not pass validation (CEmailValidation)) I’d like to highlight misstaken fields… How can I do it?
In controller (/admin/users/invite):
$invite = new invite;
$invite->attributes = $_POST['invite'];
$mailer = Yii::app()->mailer;
$mailer->AddAddress('does not matter', 'does not matter');
$mailer->Subject = "does not matter";
$mailer->Body = "does not matter";
echo "Could not send e-mail";
print "<br /> SUCCESS";
else print CHtml::errorSummary($invite);
In the form:
<?php echo CHtml::ajaxSubmitButton('Send', array('/admin/users/invite'), array('update'=>'#tx')); ?>
<p id='tx'></p><p id='px'></p>
So, now I update just #tx… But I would like to update #px as well… Can I do it using only Yii mechanisms? If not, I will write an extension for my beloved XAJAX.
It’s not Yii, but you can use the jquery plugin version of Taconite to do multiple updates from a single ajax call.
You just renderPartial a view that contains the updates in XML with PHP substitution and you’re done. Very easy and robust as it can support anything you can do with jQuery itself in XML.
Here’s an example view file called updates.php:
<?php header('Content-type: text/xml'); ?>
<?php foreach($files as $file): ?>
<?php echo $file->name; ?>
<?php endforeach; ?>
and in your action…
public function actionGetFiles()
$result = $this->renderPartial('updates', array('files'=>$files));
The view file above (updates.php) updates 3 things in the app; it populates #fileList with the list, hides #dirChooser using slideUp effect, and then fades in #fileList. Any content is allowed between the XML tags.
Edit - Oops, forgot to mention that taconite allows you to add, change or remove classes from markup, so you could use it to highlight invalid form fields as you require.
I agree with drech in that you probably need to drop out of Yii. I’d usually just respond to the jQuery ajax callback to do this kind of thing, but I hadn’t seen Taconite before, which looks very useful for DOM manipulation callbacks. It tightly couples the ajax call to the client, but that’s often the case anyway, and in Yii, by using partials, it lloks good. I’m going to take a closer look at this. Thanks.