Reputation: 1422
i have the following code in yii2 but the captcha image doesn't show !
controller:
public function actions() {
return [
'captcha' => [
'class' => 'yii\captcha\CaptchaAction',
'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
'foreColor' => 0xF9AF21,
'maxLength' => 5,
'minLength' => 3,
'padding' => 5,
'offset' => 1,
'transparent' => true,
'height' => 40
],
'error' => [
'class' => 'yii\web\ErrorAction',
],
];
}
model :(rules)
['verifyCode', 'captcha',],
view:
$form->field($model, 'verifyCode')->widget(Captcha::className()])
Upvotes: 1
Views: 1802
Reputation: 153
In SiteController look for behaviors() function, it might look like in the example bellow.
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::className(),
'only' => ['logout', 'signup'],
'rules' => [
[
'actions' => ['signup'],
'allow' => true,
'roles' => ['?'],
],
[
'actions' => ['logout'],
'allow' => true,
'roles' => ['@'],
],
],
],
];
}
You will not see captcha image, if in your behaviour() function wouldn't be specified 'only'
actions, like this: 'only' => ['logout', 'signup'],
. This line says that apply access rules only to this actions. If you don't want to add rules to specific actions, you can add 'captcha'
action to your rules, like in example below.
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::className(),
'rules' => [
[
'actions' => ['signup', 'captcha'],
'allow' => true,
'roles' => ['?'],
],
[
'actions' => ['logout'],
'allow' => true,
'roles' => ['@'],
],
],
],
];
}
Upvotes: 6
Reputation: 1422
remove this function in site controller and the problems solved:
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::className(),
'rules' => [
[
'actions' => ['login', 'error'],
'allow' => true,
],
[
'actions' => ['logout', 'index'],
'allow' => true,
'roles' => ['@'],
],
],
],
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'logout' => ['post'],
],
],
];
}
Upvotes: 0
Reputation: 4261
Controller:
public function actions()
{
return [
'error' => [
'class' => 'yii\web\ErrorAction',
],
'captcha' => [
'class' => 'yii\captcha\CaptchaAction',
'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
'foreColor' => 115006,
'backColor' => 333333,
'height' => 30,
'maxLength' => 4,
'minLength' => 4,
'offset' => 2,
'testLimit' => 1,
],
];
}
Model:
public function rules()
{
return [
['verifyCode', 'captcha'],
];
}
View:
use yii\captcha\Captcha;
<?= $form->field($model, 'verifyCode')->widget(Captcha::classname()) ?>
Upvotes: 0