my view file code is
"
Book Create
<?php echo $this->Html->link(
"< Book" ,
"/Book" ,
[
"class"=>"btn btn-info pull-right" ,
"style"=>"margin-top :-6px"
]
);
?>
<?php
echo $this->Form->create(
null ,
[
"url"=> ["action"=>"save"] ,
"class"=>"form-horizontal"
]
);
?>
Book Name:
Author Name :
<div class="form-group">
<label class="control-label col-sm-2" for="email">Email:</label>
<div class="col-sm-10">
<input type="email" class="form-control" id="email" name="email" placeholder="Enter email">
</div>
</div>
Description :
Submit
controller
code :"
<?php
namespace App\Controller;
use App\Controller\AppController;
use App\View\AppView ;
use Cake\View\Helper;
class BookController extends AppController {
public function initialize()
{
parent::initialize() ;
$this->loadModel("Books");
$this->viewBuilder()->layout("booklayout");
$this->loadComponent('Csrf');
}
public function index()
{
$books = $this->Books->find("all" ,["order"=>["book_id"=>"desc"]]);
$this->set("title" , "Book List");
$this->set("books" , $books);
}
public function create()
{
$this->set("title" , "Book create");
}
public function save()
{
//print_r($this->request->getParam('_csrfToken'));
//die();
$this->autoRender = false ;
$validation = $this->Books->newEntity($this->request->data); // $this->request->data
$validationErrors = $validation->errors();
if(!empty($validationErrors))
{
// error
print_r($this->request->data);
$this->Flash->set($validationErrors,[
"element"=>"book_error"]);
}
else
{
$form_data = $this->request->data ;
$book->name = $form_data['name'];
$book->email = $form_data['email'];
$book->author = $form_data['author'];
$book->description = $form_data['description'];
$this->Books->save($book);
$this->Flash->set("data has been saved",["element"=>"book_success"]);
echo "Sucessfuly passed" ;
}
"
model file code :slight_smile:
"
<?php
namespace App\Model\Table ;
use Cake\ORM\Table ;
use Cake\Validation\Validator;
class BooksTable extends Table{
public function validationDefault(Validator $validator)
{
$validator
->requirePresence("name" , "create" , "Name filed must be needed")
->add("name", [
"length"=>[
"rule"=>["minlength",6],
"message"=>"Book Name should be 6"
]
])
->requirePresence("author" , "create" , "author filed must be needed")
->requirePresence("email" , "create" , "email filed must be needed")
->add("email",[
"valid_email" => [
"rule"=>["email"],
"message"=>"invalid email address"
]
])
->add("email" , [
"unique_email"=> [
"rule"=>["validateunique"] ,
"provider"=>"table" ,
"message"=> "email alredy exist "
]
])
->requirePresence("description" , "create" , "description filed must be needed");
return $validator ;
}
}
?>
"