Greetings Help me figure it out .. when creating a contact form using ActiveForm in yii2, I decided to change English into Russian, and the following error pops up here:
Invalid Parameter – yii\base\InvalidParamException Attribute name must contain word characters only. 1. in C:\Users\acer\OpenServer\domains\invest\vendor\yiisoft\yii2\helpers\BaseHtml.php at line 2128 2119 * @param Model $model the model object 2120 * @param string $attribute the attribute name or expression 2121 * @return string the generated input name 2122 * @throws InvalidParamException if the attribute name contains non-word characters. 2123 */ 2124 public static function getInputName($model, $attribute) 2125 { 2126 $formName = $model->formName(); 2127 if (!preg_match('/(^|.*\])([\w\.]+)(\[.*|$)/', $attribute, $matches)) { 2128 throw new InvalidParamException('Attribute name must contain word characters only.'); 2129 } 2130 $prefix = $matches[1]; 2131 $attribute = $matches[2]; 2132 $suffix = $matches[3]; 2133 if ($formName === '' && $prefix === '') { 2134 return $attribute . $suffix; 2135 } elseif ($formName !== '') { 2136 return $formName . $prefix . "[$attribute]" . $suffix; 2137 } else { 2. in C:\Users\acer\OpenServer\domains\invest\vendor\yiisoft\yii2\helpers\BaseHtml.php at line 1263 – yii\helpers\BaseHtml::getInputName(app\models\ContactForm, 'ваше имя') 3. in C:\Users\acer\OpenServer\domains\invest\vendor\yiisoft\yii2\helpers\BaseHtml.php at line 1313 – yii\helpers\BaseHtml::activeInput('text', app\models\ContactForm, 'ваше имя', ['class' => 'form-control', 'autofocus' => true]) 4. in C:\Users\acer\OpenServer\domains\invest\vendor\yiisoft\yii2\widgets\ActiveField.php at line 395 – yii\helpers\BaseHtml::activeTextInput(app\models\ContactForm, 'ваше имя', ['class' => 'form-control', 'autofocus' => true]) 5. in C:\Users\acer\OpenServer\domains\invest\views\site\contact.php at line 29 – yii\widgets\ActiveField::textInput(['class' => 'form-control', 'autofocus' => true]) 23 <div class="row-content buffer even clear-after"> 24 <div class="section-title"><h3>Контакты</h3></div> 25 <p>Техническая поддержка проекта Money-day</p> 26 <div class="column nine"> 27 <?php $form = ActiveForm::begin(['id' => 'contact-form']); ?> 28 29 <?= $form->field($model, 'ваше имя')->textInput(['autofocus' => true]) ?> 30 31 <?= $form->field($model, 'ваш Email') ?> 32 33 <?= $form->field($model, 'тема') ?> 34 35 <?= $form->field($model, 'ваше сообщение')->textarea(['rows' => 6]) ?> 6. in C:\Users\acer\OpenServer\domains\invest\vendor\yiisoft\yii2\base\View.php at line 328 – require('C:\Users\acer\OpenServer\domains...') 7. in C:\Users\acer\OpenServer\domains\invest\vendor\yiisoft\yii2\base\View.php at line 250 – yii\base\View::renderPhpFile('C:\Users\acer\OpenServer\domains...', ['model' => app\models\ContactForm]) 8. in C:\Users\acer\OpenServer\domains\invest\vendor\yiisoft\yii2\base\View.php at line 152 – yii\base\View::renderFile('C:\Users\acer\OpenServer\domains...', ['model' => app\models\ContactForm], app\controllers\SiteController) 9. in C:\Users\acer\OpenServer\domains\invest\vendor\yiisoft\yii2\base\Controller.php at line 381 – yii\base\View::render('contact', ['model' => app\models\ContactForm], app\controllers\SiteController) 10. in C:\Users\acer\OpenServer\domains\invest\controllers\SiteController.php at line 180 – yii\base\Controller::render('contact', ['model' => app\models\ContactForm]) 174175176177178179180181182183184185186 174 if ($model->load(Yii::$app->request->post()) && $model->contact(Yii::$app->params['adminEmail'])) { 175 Yii::$app->session->setFlash('contactFormSubmitted'); 176 177 return $this->refresh(); 178 } 179 return $this->render('contact', [ 180 'model' => $model, 181 ]); 182 } 183 184 /** 185 * Displays about page. 186 * 11. app\controllers\SiteController::actionContact() Here is the code from contact.php:
<?php $form = ActiveForm::begin(['id' => 'contact-form']); ?> <?= $form->field($model, 'Ваше имя')->textInput(['autofocus' => true]) ?> <?= $form->field($model, 'Ваш Email') ?> <?= $form->field($model, 'Тема') ?> <?= $form->field($model, 'Ваше сообщение')->textarea(['rows' => 6]) ?> <?= $form->field($model, 'verifyCode')->widget(Captcha::className(), [ 'template' => '<div class="row"><div class="col-lg-3">{image}</div><div class="col-lg-6">{input}</div></div>', ]) ?> <div class="form-group"> <?= Html::submitButton('Отправить', ['class' => 'btn btn-primary', 'name' => 'contact-button']) ?> </div> <?php ActiveForm::end(); ?> Here is the code from web.php:
$config = [ 'id' => 'basic', 'basePath' => dirname(__DIR__), 'bootstrap' => ['log'], 'language' => 'ru-RU', 'sourceLanguage' => 'en-US', 'timeZone' => 'Europe/Moscow', Here is the code from ContactForm.php:
class ContactForm extends Model { public $name; public $email; public $subject; public $body; public $verifyCode; /** * @return array the validation rules. */ public function rules() { return [ // name, email, subject and body are required [['Ваше имя', 'Ваш Email', 'Тема', 'Ваше сообщение'], 'required'], // email has to be a valid email address ['email', 'email'], // verifyCode needs to be entered correctly ['verifyCode', 'captcha'], ]; } /** * @return array customized attribute labels */ public function attributeLabels() { return [ 'verifyCode' => 'Введите следующий код безопасности', ]; } /** * Sends an email to the specified email address using the information collected by this model. * @param string $email the target email address * @return bool whether the model passes validation */ public function contact($email) { if ($this->validate()) { Yii::$app->mailer->compose() ->setTo($email) ->setFrom([$this->email => $this->name]) ->setSubject($this->subject) ->setTextBody($this->body) ->send(); return true; } return false; } }
[['Ваше имя', 'Ваш Email', 'Тема', 'Ваше сообщение'], 'required'],,? - user216615