{"id":2789,"date":"2022-08-30T15:27:47","date_gmt":"2022-08-30T15:27:47","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/02\/16\/stucked-with-yii-framework-form-tutorial-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:27:47","modified_gmt":"2022-08-30T15:27:47","slug":"stucked-with-yii-framework-form-tutorial-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/stucked-with-yii-framework-form-tutorial-collection-of-common-programming-errors\/","title":{"rendered":"Stucked with Yii Framework Form tutorial-Collection of common programming errors"},"content":{"rendered":"<p>I keep on reading this tutorial of Yii framework over and over again Yii Framework &#8211; Working with Form<\/p>\n<p>I already created my Model with this following codes<\/p>\n<pre><code>class LoginForm extends CFormModel{\n    public $username;\n    public $password;\n    public $rememberMe = false;\n\n    private $_identity;\n\n    public function rules(){\n        return array(\n            \/* array(,,)\n            * functions required and boolean are built-in validators of the yii framework.\n            * you can invoke your own function by defining your own function\n            *\/\n            array('username','password','required'),\n            array('rememberMe','boolean'),\n            array('password','authenticate'),\n        );\n    }\n\n    public function authenticate(){\n        $this-&gt;_identity = new UserIdentity($this-&gt;username,$this-&gt;password);\n        if(!$this-&gt;_identity-&gt;authenticate()){\n            $this-&gt;addError(\"password\",\"Incorrect Username or Password\");\n        }\n    }\n\n    public function attributeLabels(){\n        return array(\n            'username'=&gt;\"Username\",\n            'password'=&gt;\"Password\",\n            'rememberMe'=&gt;\"Remember Me\",\n        );\n    }\n}\n<\/code><\/pre>\n<p>and my Action function with this codes in my controller<\/p>\n<pre><code> public function actionLogin(){\n        \/\/calls the Login Model that will be used in this action\n        $model = new LoginForm;\n        if(isset($_POST[\"LoginForm\"])){\n            \/\/collects user input\n            $model-&gt;attributes = $_POST[\"LoginForm\"];\n            \/\/validates user input using the model rules and redirects back to\n            \/\/previous page when user input is invalid\n            if($model-&gt;validate()){\n               $this-&gt;redirect(Yii::app()-&gt;user-&gt;returnUrl);   \n            }\n            \/\/redisplay the login form\n            $this-&gt;render('login',array('loginModel'=&gt;$model));\n        }\n    }\n<\/code><\/pre>\n<p>and lastly in my View<\/p>\n<pre><code>\n\n       \n       \n       \n       \n       \n       \n       \n       \n       \n       \n       \n       \n\n\n<\/code><\/pre>\n<p>and i always came out with this error in my view D:\\xampp\\htdocs\\wiltalk\\protected\\views\\sandbox\\index.php(11)<\/p>\n<blockquote>\n<p>Undefined variable: model<\/p>\n<\/blockquote>\n<p>do i miss something? please let me know&#8230; I know this is kinda simple but I am a first-timer in using such component-based MVC frameworks&#8230;. Thanks<\/p>\n<ol>\n<li>\n<pre><code>public function actionLogin(){\n        \/\/calls the Login Model that will be used in this action\n        $model = new LoginForm;\n        if(isset($_POST[\"LoginForm\"])){\n            \/\/collects user input\n            $model-&gt;attributes = $_POST[\"LoginForm\"];\n            \/\/validates user input using the model rules and redirects back to\n            \/\/previous page when user input is invalid\n            if($model-&gt;validate()){\n               $this-&gt;redirect(Yii::app()-&gt;user-&gt;returnUrl);   \n            }            \n        }\n        \/\/redisplay the login form\n        $this-&gt;render('login',array('model'=&gt;$model));\n    }\n<\/code><\/pre>\n<p>Your code was not correct. Make these changes to your code.<\/p>\n<ol>\n<li>You are using model variable and passing loginModel variable.<\/li>\n<li>You are rendering view file on POST.<\/li>\n<\/ol>\n<\/li>\n<li>\n<p>This is just a shot in the dark&#8230;<\/p>\n<p>For your controller <code>public function actionLogin(){<\/code> add <code>return $model;<\/code> at the end.<\/p>\n<p>\nAdd to the top of your view.<\/p>\n<p>The issue is that you&#8217;re not setting <code>$model<\/code> anywhere in your view, but your control is setting it. You must find some way to pass that <code>$model<\/code> that&#8217;s being set in your control back your your view.<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2014-02-16 14:30:40. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I keep on reading this tutorial of Yii framework over and over again Yii Framework &#8211; Working with Form I already created my Model with this following codes class LoginForm extends CFormModel{ public $username; public $password; public $rememberMe = false; private $_identity; public function rules(){ return array( \/* array(,,) * functions required and boolean are [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-2789","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2789","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=2789"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2789\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=2789"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=2789"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=2789"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}