Validation using Controller


#1

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?


#2

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


#3

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


#4

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.'); 
 }
}