# Validation using Controller

**URL:** https://discourse.cakephp.org/t/validation-using-controller/2883
**Category:** Need Help
**Created:** [July 12, 2017, 6:13am UTC](https://discourse.cakephp.org/t/validation-using-controller/2883 "2017-07-12T06:13:36Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Amol\_Chavan](https://avatars.discourse-cdn.com/v4/letter/a/ee59a6/32.png) [@Amol\_Chavan](https://discourse.cakephp.org/u/Amol_Chavan)
#### Post date: [July 12, 2017, 6:13am UTC](https://discourse.cakephp.org/t/validation-using-controller/2883/1 "2017-07-12T06:13:36Z")

</div>

I have to validate data in controller.  
and display the validator message in view.

I have not used default cakephp functions like save(), patchEntity().

How can I do this?

---

<div class="post-metadata">

### Author: ![lorenzoshake](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/lorenzoshake/32/711_2.png) [@lorenzoshake](https://discourse.cakephp.org/u/lorenzoshake)
#### Post date: [July 12, 2017, 8:33am UTC](https://discourse.cakephp.org/t/validation-using-controller/2883/2 "2017-07-12T08:33:54Z")

</div>

try this:  
[https://book.cakephp.org/3.0/en/core-libraries/form.html](https://book.cakephp.org/3.0/en/core-libraries/form.html)

---

<div class="post-metadata">

### Author: ![Marcel](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/marcel/32/329_2.png) [@Marcel](https://discourse.cakephp.org/u/Marcel)
#### Post date: [July 12, 2017, 8:55am UTC](https://discourse.cakephp.org/t/validation-using-controller/2883/3 "2017-07-12T08:55:18Z")

</div>

Bad idea.  
You will not be able to validate data of same model in other controllers without code duplication.

---

<div class="post-metadata">

### Author: ![syed](https://avatars.discourse-cdn.com/v4/letter/s/a88e57/32.png) [@syed](https://discourse.cakephp.org/u/syed)
#### Post date: [July 12, 2017, 11:10pm UTC](https://discourse.cakephp.org/t/validation-using-controller/2883/4 "2017-07-12T23:10:19Z")

</div>

> Your query lack explanation.

Answer to your 2nd query for custom message display.

There is this thing called Flash located in:  
`src/Template/Element/Flash.`

create yourfunctionName.ctp file in above directory, and copy the following code in it.

```
<?php
    $message = h($message);
?>
<div class="message success" onclick="this.classList.add('hidden')"><?= $message ?></div>

```

**Usage:**  
Then use the following code:

`$this->Flash->yourfunctionName('This is custom message.');`

in your Controller’s method.  
For e.g.

```
public function functionName(){
 if( /* If some condition is true/false */ ){
    //print or Flash this message
   $this->Flash->yourfunctionName('This is custom message.'); 
 }
}

```
