i am very new to cakephp i trying to validate contact forms.i need a validation without database tables in models.but it is not working.code which i used shown below
folllowing is the code :ContactsController.php
<?php
class ContactsController extends AppController {
var $uses='Contact';
public function index() {
// Placeholder for index. No actual action here, everything is submitted to the send function.
}
public function send() {
$this->Contact->set($this->data);
if($this->Contact->validates()) {
echo "hiiii";
}
}
}
----------------------------Model-----------------------
<?php
App::uses('AppModel', 'Model');
class ContactModel extends AppModel {
var $name = 'Contact';
var $useTable = false;
var $validate = array(
'name' => array(
'rule' => 'notEmpty',
'required' => true
),
'email' => array(
'rule' => 'email',
'required' => true
),
'message' => array(
'rule' => 'notEmpty',
'required' => true
)
);
}
-----------------------in view/Contacts/index.ctp-----------------------------
<?php
echo $this->Form->create('Contact', array('action' => 'Contacts/send'));
echo $this->Form->input('name');
echo $this->Form->input('email');
echo $this->Form->input('message',array('rows' => 3));
echo $this->Form->submit('Submit');
?>
There is a way to validate without using models, which can also be used for forms, at least in CakePHP2.
For example, I had to validate params and I saw no point in creating models for each action that requires params.
So, I used this:
Before the controller class declaration
App::uses('Validation', 'Utility');
In the action with a $code param
function checkcode($code) { ... Validation::alphaNumeric($code); ... }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With