Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to validate associated models in cakephp
    primarykey
    data
    text
    <p>Hello Could please tell me. How to validate associated models in cakephp? i am using multiple model in one controller and controller name is 'AdminsController'. These are Model in AdminsController </p> <pre><code>$uses=array('index','Static_page_view','Admin','Categories','Products', 'contact','User','Rams');' </code></pre> <p>now i want to validate 'Categories'(Model). i have defined set of validation rules. But not ale to validate. While when i use 'Admin'(MODEL) it works perfectly. </p> <pre><code>&lt;?php class AdminsController extends AppController { public $uses=array('index','Static_page_view','Admin','Categories','Products', 'contact','User','Rams'); public function index() { if(!empty($this-&gt;request-&gt;data)) //this checks if the form is being submitted and is not empty { if($this-&gt;Admin-&gt;save($this-&gt;request-&gt;data))//this saves the data from the form and also return true or false { $this-&gt;Session-&gt;setFlash('The post was successfully added!'); $this-&gt;redirect(array('action'=&gt;'index')); } else { $this-&gt;Session-&gt;setFlash('The post was not saved, please try again'); } </code></pre> <p>}</p> <pre><code>public function Add_Category() { if(!empty($this-&gt;request-&gt;data)) //this checks if the form is being submitted and is not empty { if($this-&gt;Rams-&gt;save($this-&gt;request-&gt;data))//this saves the data from the form and also return true or false { $this-&gt;Session-&gt;setFlash('The post was successfully added!'); $this-&gt;redirect(array('action'=&gt;'Add_Category')); } else { $this-&gt;Session-&gt;setFlash('The post was not saved, please try again'); } } } } </code></pre> <p>\app\Model\Admin</p> <pre><code> &lt;?php class Admin extends AppModel { public $validate = array( ['Admin'] =&gt; array( 'name'=&gt;array( 'title_must_not_be_blank'=&gt;array( 'rule'=&gt;'notEmpty', 'message'=&gt;'Please Enter your Name!' ) ), 'email'=&gt;array( 'body_must_not_be_blank'=&gt;array( 'rule'=&gt;'notEmpty', 'message'=&gt;'Please Enter your Email!' ) ), 'phone'=&gt;array( 'body_must_not_be_blank'=&gt;array( 'rule'=&gt;'notEmpty', 'message'=&gt;'Please Enter your phone!' ) ), 'query'=&gt;array( 'body_must_not_be_blank'=&gt;array( 'rule'=&gt;'notEmpty', 'message'=&gt;'Please Enter your Query!' ) ) )); } \app\Model\categories &lt;?php class Categories extends AppModel { public $validate = array( 'name'=&gt;array( 'title_must_not_be_blank'=&gt;array( 'rule'=&gt;'notEmpty', 'message'=&gt;'Please Enter your Name!' ) ), 'email'=&gt;array( 'body_must_not_be_blank'=&gt;array( 'rule'=&gt;'notEmpty', 'message'=&gt;'Please Enter your Email!' ) ), 'phone'=&gt;array( 'body_must_not_be_blank'=&gt;array( 'rule'=&gt;'notEmpty', 'message'=&gt;'Please Enter your phone!' ) ), 'query'=&gt;array( 'body_must_not_be_blank'=&gt;array( 'rule'=&gt;'notEmpty', 'message'=&gt;'Please Enter your Query!' ) ) ); } \app\View\admins\index.ctp &lt;h2&gt;Add a Post&lt;/h2&gt; &lt;?php echo json_encode($this-&gt;validationErrors); //&lt;!--create the form 2parameter:the post model and the 2nd is the form is submitted to which action--&gt; echo $this-&gt;Form-&gt;create('Admin', array('action'=&gt;'index')); echo $this-&gt;Form-&gt;input('name');//&lt;!--We have not specified the field so type becomes text as the according to the database field type--&gt; echo $this-&gt;Form-&gt;input('email'); echo $this-&gt;Form-&gt;input('phone'); echo $this-&gt;Form-&gt;input('query');//&lt;!--We have not specified the field so type becomes textarea as the according to the database field type--&gt; echo $this-&gt;Form-&gt;end('Create a Post');//&lt;!--ends the form and create the text on button the same as specified--&gt; ?&gt; \app\View\admins\category.ctp &lt;head&gt; &lt;title&gt;Admin Panel&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;div id="container"&gt; &lt;?php echo $this-&gt;element("header"); ?&gt; &lt;div id="content"&gt; &lt;?php echo $this-&gt;element("left-content"); ?&gt; &lt;div id="right-content"&gt; &lt;div id="upper"&gt; &lt;h3&gt;Add SubCategory&lt;/h3&gt; &lt;/div&gt;&lt;!---upper end---&gt; &lt;?php echo $this-&gt;Form-&gt;create('Admin', array('class' =&gt; "righform")); $options = array(); ?&gt; &lt;fieldset class="textbox"&gt; &lt;label class="cate"&gt;&lt;span&gt;Main Category :&lt;/span&gt; &lt;select name="parentId"&gt; &lt;?php foreach($name as $row){?&gt; &lt;option value="&lt;?php echo $row['Categories']['id'];?&gt;"&gt;&lt;?php echo $row['Categories']['name']; ?&gt;&lt;/option&gt; &lt;?php } ?&gt; &lt;/select&gt; &lt;/label&gt; &lt;br /&gt;&lt;br /&gt;&lt;br /&gt; &lt;label class="cate"&gt; &lt;?php echo $this-&gt;Form-&gt;input('name'); ?&gt; &lt;br /&gt;&lt;br /&gt;&lt;br /&gt; &lt;label class="cate1"&gt; &lt;?php echo $this-&gt;Form-&gt;input('Description'); ?&gt; &lt;br /&gt;&lt;br /&gt;&lt;br /&gt; &lt;br /&gt;&lt;br /&gt;&lt;br /&gt; &lt;td&gt;&lt;button class="button" type="submit"&gt;Submit&lt;/button&gt;&lt;/td&gt; &lt;/fieldset&gt; &lt;/form&gt; &lt;/div&gt; &lt;/div&gt;&lt;!---main content end---&gt; &lt;/div&gt;&lt;!----content end----&gt; &lt;?php echo $this-&gt;element("footer"); ?&gt; &lt;/div&gt;&lt;!---container end----&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>Thanks in Advance.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload