سؤال

I have created a Controller, a model and a view for this. I want to make a form in CakePHP. But this is not working and till now I cannot understand the reason why is this happening...

My code for the controller is:

class MlistsController extends AppController {

    public $helpers = array('Html','Form');

    public function create() {
        if ($this->request->is('post')) {
            if ($this->Mlist->save($this->request->data)) {
                $this->Session->setFlash(__('okay..'));
                $this->redirect('action' => 'index');
            }
        }
    }    
}

My Model is:

App::uses('AuthComponent', 'Controller/Component');

class MList extends AppModel {

    public function beforeSave($options = array()) {
        if (isset($this->data[$this->alias]['password'])) {
            $this->data[$this->alias]['password'] = AuthComponent::password($this->data[$this->alias]['password']);
        }
        return true;
    }

    public $validate = array(
        'listname' => array(
            'required' => array(
                'rule' => array('notEmpty'),
                'message' => 'A listname '
            )
        ),
        'replyto' => array(
            'required' => array(
                'rule' => array('notEmpty'),
                'email' => 'email'
            )
        ),
        'fromName' => array(
            'required' => array(
                'rule' => array('notEmpty'),
                'message' => 'Your name'
            )
        ),
        'subject' => array(
            'required' => array(
                'rule' => array('notEmpty'),
                'message' => 'A subject '
            )
        ),
        'reminder' => array(
            'required' => array(
                'rule' => array('notEmpty'),
                'message' => 'A reminder '
            )
        ),
        'contactsfile' => array(
            'required' => array(
                'rule' => array('notEmpty'),
                'message' => 'custom message'
            )
        ));

}

And my view file create.ctp is:

<h2>Create new list</h2>

<?php
    $this->Form->create('Mlist');
    echo $this->Form->input('listname',array('label' => 'Your ListName:'));;
    echo $this->Form->input('replyto',array('label' => 'Reply To email:'));
    echo $this->Form->input('fromName',array('label' => 'From Name:'));
    echo $this->Form->input('subject',array('label' => 'mail subject:'));
    echo $this->Form->input('reminder',array('label' => 'Reminder'));
    echo $this->Form->input('contactsfile',array('label' => 'Upload your file','type' => 'file'));
    echo '<br />';
    echo $this->Form->end('submit');

Finally, the Submit button of the form is not even green, but grey and does not function when I click it. Also the star symbol (*) is not showing apart the form labels where the fields are required...

Can you help me with this issue?

هل كانت مفيدة؟

المحلول

You're kind of bypassing Cake's 'Convention over configuration' paradigm. In order for these conventions to work Cake uses the Inflector Class which handles the plural forms of English words.

So when you use an appropriate (to this convention) naming policy it will work for you "out of the box". Otherwise you will have to configure the Model, Controller and any Helpers/Components/Behaviours you're using. They all have configuration parameters for this, but there is not a big point in doing this static configuration with Cake since you will also have to go through it again if you rename a DB table for example. I's just against the idea of Cake. If you need to do this just use some other configuration based framework.

نصائح أخرى

I believe the problem is in your view. You also have to echo the starting of the form:

    echo $this->Form->create('Mlist');

The naming convention is ok. Cake doesn't have any database of real english words, only few irregularities (see arrays in http://api.cakephp.org/2.3/source-class-Inflector.html) so anything like Mlist is just pluralized with adding "s" at the end.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top