创建逆向形式时出错:Call to a member function isAttributeRequired() on null?
An error occurred while creating the inverse form: Call to a member function isAttributeRequired() on null?
请帮我处理这个问题,在yii2上创建逆向形式后,出现了这个错误:
Call to a member function isAttributeRequired() on null
1. in C:\Users\acer\OSPanel\domains\medicalyii2\vendor\yiisoft\yii2\widgets\ActiveField.php at line
/**
* Adds aria attributes to the input options.
* @param $options array input options
* @since 2.0.11
*/
protected function addAriaAttributes(&$options)
{
if ($this->addAriaAttributes) {
if (!isset($options['aria-required']) && $this->model->isAttributeRequired($this->attribute)) {
$options['aria-required'] = 'true';
}
if (!isset($options['aria-invalid'])) {
if ($this->model->hasErrors($this->attribute)) {
$options['aria-invalid'] = 'true';
}
}
}
}
2. yii\base\ErrorHandler::handleFatalError()
我不明白这个问题与什么有关....这是我的表单在视图中的样子:
<?php
$form = ActiveForm::begin([
'id' => 'appointment_form',
'fieldConfig' => [
'options' => [
'tag' => 'span',
'class' => 'input input--kohana'
],
'template' => '{input}{label}',
'inputOptions' => ['class' => 'input__field input__field--kohana'],
'labelOptions' => [
'class' => 'input__label input__label--kohana',
]
]
]);
?>
<?= $form->field($model, 'name')->textInput(['inputOptions' => ['id' => 'input-29']])->label("<i class=\"icon-phone5 icon icon--kohana\"></i><span class=\"input__label-content input__label-content--kohana\">" . $model->getAttributeLabel('Your Name') . "</span>") ?>
<?= $form->field($model, 'email')->textInput(['inputOptions' => ['id' => 'input-30']])->label("<i class=\"icon-dollar icon icon--kohana\"></i><span class=\"input__label-content input__label-content--kohana\">" . $model->getAttributeLabel('Email Address') . "</span>") ?>
<span class="last"><?= $form->field($model, 'phone')->textInput(['inputOptions' => ['id' => 'input-31']])->label("<i class=\"icon-phone5 icon icon--kohana\"></i><span class=\"input__label-content input__label-content--kohana\">" . $model->getAttributeLabel('Phone Number') . "</span>") ?></span>
<?= $form->field($model, 'date')->textInput(['inputOptions' => ['id' => 'datepicker']])->label(false) ?>
<!-- ОСТАЛЬНЫЕ ПОЛЯ ФОРМЫ -->
<?= $form->field($model, 'body')->textInput(['inputOptions' => ['id' => 'texterea']])->label("<i class=\"icon-new-message icon icon--kohana\"></i><span class=\"input__label-content input__label-content--kohana\">" . $model->getAttributeLabel('Message') . "</span>") ?>
<?= Html::submitButton('Submit'); ?>
<?php
ActiveForm::end();
?>
控制器:
public function actionIndex()
{
/* Создаем экземпляр класса */
$model = new AppointmentForm();
/* получаем данные из формы и запускаем функцию отправки contact, если все хорошо, выводим сообщение об удачной отправке сообщения на почту */
if ($model->load(Yii::$app->request->post()) && $model->appointment(Yii::$app->params['adminEmail'])) {
Yii::$app->session->setFlash('contactFormSubmitted');
return $this->refresh();
/* иначе выводим форму обратной связи */
} else {
return $this->render('index');
}
}
AppointmentForm.php 型号
class AppointmentForm extends Model
{
public $name;
public $email;
public $phone;
public $date;
public $body;
/**
* @return array the validation rules.
*/
public function rules()
{
return [
// name, email, subject and body are required
[['name', 'email', 'phone', 'date', 'body'], 'required'],
// email has to be a valid email address
['email', 'email'],
];
}
/**
* 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 appointment($email)
{
$content = "<p>Email: " . $this->email . "</p>";
$content .= "<p>Name: " . $this->name . "</p>";
$content .= "<p>Phone: " . $this->phone . "</p>";
$content .= "<p>Date: " . $this->date . "</p>";
$content .= "<p>Body: " . $this->body . "</p>";
if ($this->validate()) {
Yii::$app->mailer->compose("@app/mail/layouts/html", ["content" => $content])
//->setTo($email)
->setTo('swallowsveta97@yandex.ru')
->setFrom([\Yii::$app->params['supportEmail'] => $this->name])
//->setFrom([$this->email => $this->name])
//->setFrom('swallowsveta97@yandex.ru')
//->setFrom([\Yii::$app->params['supportEmail'] => $this->name])
->setPhone($this->phone)
->setDate($this->date)
->setTextBody($this->body)
->send();
return true;
}
return false;
}
}
我在互联网上搜索了2天,但一直没有找到。
请告诉我这个错误是否可能相关?
您应该将模型传递给视图
return $this->render('index', ['model'=> $model ,]);
例如:
public function actionIndex()
{
/* Создаем экземпляр класса */
$model = new AppointmentForm();
/* получаем данные из формы и запускаем функцию отправки contact, если все хорошо, выводим сообщение об удачной отправке сообщения на почту */
if ($model->load(Yii::$app->request->post()) && $model->appointment(Yii::$app->params['adminEmail'])) {
Yii::$app->session->setFlash('contactFormSubmitted');
return $this->refresh();
/* иначе выводим форму обратной связи */
} else {
return $this->render('index', ['model'=> $model,]);
}
}
请帮我处理这个问题,在yii2上创建逆向形式后,出现了这个错误:
Call to a member function isAttributeRequired() on null
1. in C:\Users\acer\OSPanel\domains\medicalyii2\vendor\yiisoft\yii2\widgets\ActiveField.php at line
/**
* Adds aria attributes to the input options.
* @param $options array input options
* @since 2.0.11
*/
protected function addAriaAttributes(&$options)
{
if ($this->addAriaAttributes) {
if (!isset($options['aria-required']) && $this->model->isAttributeRequired($this->attribute)) {
$options['aria-required'] = 'true';
}
if (!isset($options['aria-invalid'])) {
if ($this->model->hasErrors($this->attribute)) {
$options['aria-invalid'] = 'true';
}
}
}
}
2. yii\base\ErrorHandler::handleFatalError()
我不明白这个问题与什么有关....这是我的表单在视图中的样子:
<?php
$form = ActiveForm::begin([
'id' => 'appointment_form',
'fieldConfig' => [
'options' => [
'tag' => 'span',
'class' => 'input input--kohana'
],
'template' => '{input}{label}',
'inputOptions' => ['class' => 'input__field input__field--kohana'],
'labelOptions' => [
'class' => 'input__label input__label--kohana',
]
]
]);
?>
<?= $form->field($model, 'name')->textInput(['inputOptions' => ['id' => 'input-29']])->label("<i class=\"icon-phone5 icon icon--kohana\"></i><span class=\"input__label-content input__label-content--kohana\">" . $model->getAttributeLabel('Your Name') . "</span>") ?>
<?= $form->field($model, 'email')->textInput(['inputOptions' => ['id' => 'input-30']])->label("<i class=\"icon-dollar icon icon--kohana\"></i><span class=\"input__label-content input__label-content--kohana\">" . $model->getAttributeLabel('Email Address') . "</span>") ?>
<span class="last"><?= $form->field($model, 'phone')->textInput(['inputOptions' => ['id' => 'input-31']])->label("<i class=\"icon-phone5 icon icon--kohana\"></i><span class=\"input__label-content input__label-content--kohana\">" . $model->getAttributeLabel('Phone Number') . "</span>") ?></span>
<?= $form->field($model, 'date')->textInput(['inputOptions' => ['id' => 'datepicker']])->label(false) ?>
<!-- ОСТАЛЬНЫЕ ПОЛЯ ФОРМЫ -->
<?= $form->field($model, 'body')->textInput(['inputOptions' => ['id' => 'texterea']])->label("<i class=\"icon-new-message icon icon--kohana\"></i><span class=\"input__label-content input__label-content--kohana\">" . $model->getAttributeLabel('Message') . "</span>") ?>
<?= Html::submitButton('Submit'); ?>
<?php
ActiveForm::end();
?>
控制器:
public function actionIndex()
{
/* Создаем экземпляр класса */
$model = new AppointmentForm();
/* получаем данные из формы и запускаем функцию отправки contact, если все хорошо, выводим сообщение об удачной отправке сообщения на почту */
if ($model->load(Yii::$app->request->post()) && $model->appointment(Yii::$app->params['adminEmail'])) {
Yii::$app->session->setFlash('contactFormSubmitted');
return $this->refresh();
/* иначе выводим форму обратной связи */
} else {
return $this->render('index');
}
}
AppointmentForm.php 型号
class AppointmentForm extends Model
{
public $name;
public $email;
public $phone;
public $date;
public $body;
/**
* @return array the validation rules.
*/
public function rules()
{
return [
// name, email, subject and body are required
[['name', 'email', 'phone', 'date', 'body'], 'required'],
// email has to be a valid email address
['email', 'email'],
];
}
/**
* 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 appointment($email)
{
$content = "<p>Email: " . $this->email . "</p>";
$content .= "<p>Name: " . $this->name . "</p>";
$content .= "<p>Phone: " . $this->phone . "</p>";
$content .= "<p>Date: " . $this->date . "</p>";
$content .= "<p>Body: " . $this->body . "</p>";
if ($this->validate()) {
Yii::$app->mailer->compose("@app/mail/layouts/html", ["content" => $content])
//->setTo($email)
->setTo('swallowsveta97@yandex.ru')
->setFrom([\Yii::$app->params['supportEmail'] => $this->name])
//->setFrom([$this->email => $this->name])
//->setFrom('swallowsveta97@yandex.ru')
//->setFrom([\Yii::$app->params['supportEmail'] => $this->name])
->setPhone($this->phone)
->setDate($this->date)
->setTextBody($this->body)
->send();
return true;
}
return false;
}
}
我在互联网上搜索了2天,但一直没有找到。 请告诉我这个错误是否可能相关?
您应该将模型传递给视图
return $this->render('index', ['model'=> $model ,]);
例如:
public function actionIndex()
{
/* Создаем экземпляр класса */
$model = new AppointmentForm();
/* получаем данные из формы и запускаем функцию отправки contact, если все хорошо, выводим сообщение об удачной отправке сообщения на почту */
if ($model->load(Yii::$app->request->post()) && $model->appointment(Yii::$app->params['adminEmail'])) {
Yii::$app->session->setFlash('contactFormSubmitted');
return $this->refresh();
/* иначе выводим форму обратной связи */
} else {
return $this->render('index', ['model'=> $model,]);
}
}