在CakePHP中的无模型表单中找不到类ContactForm

时间:2018-02-02 13:56:06

标签: php cakephp cakephp-3.0

我在我的应用程序中使用了Modelless Form,我遵循CakePHP Modelless guid,但我有一个错误。 我尝试了不同的方法来找出问题,但没有取得成功,请帮助我。

  

Class' ContactForm'没找到。

在Controller \ ContactController.php第20行显示错误

的src /窗体/ ContactForm.php

<?php

use Cake\Form\Form;
use Cake\Form\Schema;
use Cake\Mailer\Email;
use Cake\Validation\Validator;


class ContactForm extends Form{

    /**
     * _buildSchema is used to define which fields going to use HTML Form
     * created  by FormHelper
     * @param Schema $schema
     * @return $this|Schema
     */
    protected function _buildSchema(Schema $schema)
    {
        return $schema->addField('name','string')
            ->addField('email',['type'=>'string'])
            ->addField('body',['type'=>'text']);
    }

    /**
     *_buildValidator is used to validate rules for our fields
     * to show error to user
     * @param Validator $validator
     * @return $this|Validator
     */
    protected function _buildValidator(Validator $validator)
    {
        return $validator->add('name','length',[
            'rule' => ['minLength', 3],
            'message' => 'Please enter your name'
        ])->add('email','lenght',[
            'rule' => ['minLength',10],
            'message' => 'Please enter your Email id'
        ]);
    }

    /**_email() will send email to us and
     * $email is a boject of email class and we are tell it to use default
     * that we setup in app.php
     * @param array $data
     * @return bool|void
     */
    protected function _execute(array $data)
    {
        $email = new Email();
        $email->setProfile('default');

        $email->setFrom([$data['email']])
            ->setTo('creative.rihan@gmail.com')
            ->setSubject('Sended from Contact us Form')
            ->send([$data['body']]);

        return true;


    }
}

SRC /控制器/ ContactController.php

<?php
namespace App\Controller;


use App\Controller\AppController;
use App\Form\ContactForm;

/**
 * Class ContactController should be the same name as form name
 */

class ContactController extends AppController
{
    /**
     *
     */
    public function index()
    {
        $contact = new ContactForm();  // This is line no 20

        if($this->request->is('post'))
        {
            if($contact->execute($this->request->getData()))    //here execute() will _execute() in ContactForm
            {
                $this->Flash->success('Thank for Contacting us.');

                $this->request->getData('name');
                $this->request->getData('email');
                $this->request->getData('body');
            } else{
                $this->Flash->error('Sorry your message could not be send');
            }

        }
        $this->set('contact',$contact);
    }
}

的src /模板/联系/ index.ctp

<?php

?>


<div class="contact form large-12 medium-12 columns content">
    echo $this->Form->create($contact);

    <legend><?= __('Contact Us') ?></legend>
    <fieldset>
        <?php
        echo $this->Form->control('name');
        echo $this->Form->contorl('email');
        echo $this->Form->control('body');
        ?>
    </fieldset>
    <?= $this->Form->button(__('Submit')) ?>
    <?= $this->Form->end(); ?>
</div>

1 个答案:

答案 0 :(得分:3)

您忘记为表单指定命名空间。
您的文件src/Form/ContactForm.php应该像这样开始:

<?php
namespace App\Form;

use Cake\Form\Form;
use Cake\Form\Schema;
use Cake\Mailer\Email;
use Cake\Validation\Validator;


class ContactForm extends Form {