Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cakephp 2.x form validation with model but without database

Tags:

cakephp

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');
?>
like image 796
sreeni Avatar asked Mar 31 '26 12:03

sreeni


1 Answers

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:

  1. Before the controller class declaration

    App::uses('Validation', 'Utility');

  2. In the action with a $code param

    function checkcode($code) { ... Validation::alphaNumeric($code); ... }

like image 140
alex.ac Avatar answered Apr 03 '26 15:04

alex.ac



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!