phpyiiyii-cformmodel

How to create a new form model on Yii?


I am just a stupid started to the yii and the whole mvc thing. I wanted to know if it's possible to create a form, that will just send me an e-mail with all the fields filled. No database storage, no major rules.

I never had a problem using the pre-made models like the login and the contact form one. Those work fine. So now I am trying a test form to see if I can create an working model but It doesn't seem to work.

View:

<form name="testform" id="testform" action="/default/apply" method="POST">
<input type="text" value="" id="test1" name="test1" />
<input type="submit" name="test_sbm" id="test_sbm" value="submit"/>
</form>

Model:

<?php


class ApplicationForm extends CFormModel {

public $test1;


public function rules() {
    return array(
        array('test1', 'required'),

    );
}

}

Default Controller:

public function actionApply() {
    $this->layout = "home";
    $this->pageTitle = "Apply Now";

    $applymodel = new ApplicationForm;


    if(isset($_POST['testform'])){
         $applymodel->attributes=$_POST['testform'];
         $to = 'myemail@myserver.com';
            $subject = 'Test Form';
            $message = $applymodel->test1;
            $headers = 'From: test@test.com' . " " .
                    'Reply-To: tesy@test.com' . " " .
                    'X-Mailer: PHP/' . phpversion();
            mail($to, $subject, $message, $headers);
    }

   $this->render('apply', array('model' => $applymodel));
}

I am probably missing tons of stuff, but I've been researching and can't find my way to just get the form info. If anybody can show me the right direction, I would appreciate.

I just want to get the form values and send them by e-mail, for now. Thanks


Solution

  • Change your input element from

    <input type="text" value="" id="test1" name="test1" />
    

    to

    <input type="text" value="" id="test1" name="testform[test1]" />