Yii2 创建反向表单时出错:调用成员函数isAttributeRequired()是否为空?
请帮助我处理此问题,在yii2上创建反向表单后,出现以下错误:Yii2 创建反向表单时出错:调用成员函数isAttributeRequired()是否为空?,yii2,active-form,Yii2,Active Form,请帮助我处理此问题,在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 $o
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();
?>
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;
}
}
类任命表单扩展模型
{
公共名称;
公费邮件;
公共电话;
公费$日期;
公共机构;
/**
*@return数组验证规则。
*/
公共职能规则()
{
返回[
//姓名、电子邮件、主题和正文是必填项
[[“姓名”、“电子邮件”、“电话”、“日期”、“正文”]、“必填”],
//电子邮件必须是有效的电子邮件地址
['email','email'],
];
}
/**
*使用此模型收集的信息向指定的电子邮件地址发送电子邮件。
*@param string$通过电子邮件发送目标电子邮件地址
*@return bool模型是否通过验证
*/
公众活动预约(电邮)
{
$content=“电子邮件:”.$this->Email。“”;
$content.=“Name:”.$this->Name.”;
$content.=“Phone:”.$this->Phone.”;
$content.=“日期:”.$this->Date.”;
$content.=“Body:”.$this->Body.“”;
如果($this->validate()){
Yii::$app->mailer->compose(@app/mail/layouts/html),[“content”=>$content])
//->setTo(电子邮件)
->设置为('swallowsveta97@yandex.ru')
->setFrom([\Yii::$app->params['supportEmail']=>$this->name])
//->setFrom([$this->email=>$this->name])
//->设置自('swallowsveta97@yandex.ru')
//->setFrom([\Yii::$app->params['supportEmail']=>$this->name])
->设置电话($this->phone)
->设置日期($this->date)
->setTextBody($this->body)
->send();
返回true;
}
返回false;
}
}
我在网上搜索了两天,但一直没有找到。
请告诉我是否与此错误有关?您应该将模型传递给视图
return $this->render('index', ['model'=> $model ,]);
例如:
在ActivationIndex中,您不会将模型传递给表单。view的代码是针对view index.php的。。或者您发布了不相关的代码?更好地解释
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,]);
}
}