Building a website using Yii1.1 and trying to use the Form Builder for all the forms. I can’t however seem to find any example of how to use the built-in CCaptcha in a CForm. All examples relate to the 1.0-way using CHtml. Could somebody please explain how to do this?
Also figured out why working with a Captcha is different from other views: The CCaptcha widget only renders an image and JQuery object (generate new image). You even need to add the input field related to the Captcha ($verifyCode in this case) yourself. Guess this means it can’t be part of the form definition, because no rendering should take place inside the form class definition (according to MVC logic).
This still feels kind of developer-unfriendly. I would like to support a captcha widget that can be used straight in a CForm (something like ‘type’=>‘zii.widgets.captcha’) and takes care of the image, refresh-image button and input field (perhaps even fully client-sided?) so we can configure this completely through the CForm and just put <?php echo $form; ?> in the view.
Been doing some more tweaking to my previous solution. I couldn’t find a function in the API reference that allows me to render the field label and the actual field seperately. This means I can only do a $element->render(); which means the Captcha image is located above/before the label of the field it is related to.
Three solutions to this problem:
[list=1][]Turn the layout of the form into two columns using CSS, with the Captcha aligned with the input field. It is however visually still located higher than the related label.[]Place $this->widget(’[color="#008000"]CCaptcha[/color]’); after the $element->render(); of its input field. This seems unintuitive for the user though.[*]Go back to the old fashioned CHtml-based form functions for the Captcha part. This means removing the [color="#008000"]‘verifyCode’=>array(‘type’=>‘text’,),[/color] part of the CForm and changing the view into:
<div class="form">
<?php
echo $form->renderBegin();
echo $form->renderElements();
// Add Captcha
echo CHtml::activeLabel($model,'verifyCode');
echo '<div>'; $this->widget('CCaptcha'); echo '</div>';
echo CHtml::activeTextField($model,'verifyCode');
echo '<p class="hint">'.yii::t('core','Please enter the letters as they are shown in the image above.<br/>Letters are not case-sensitive.').'</p>';
echo $form->renderButtons();
echo $form->renderEnd();
?>
</div>
[/list]I guess I’ll use the third option for now. Still, combining CHtml labels and fields with CForm feels messy. I think this makes the addition of a completely CForm compatible/based widget to Yii even more important.
Actually I decided to render input field inside of MyCaptcha widget:
<?php
class MyCaptcha extends CCaptcha
{
public $model;
public $attribute;
public function run()
{
parent::run();
echo CHtml::activeTextField($this->model, $this->attribute);
}
}