tags:

views:

338

answers:

1

hi, I am using Jquery and cakephp for my application .

 I am having  a form created in my page using $form->create of cakephp .THe form contains many fields which the user will fill it on and on submitting ,the data is saved by



 function submit($id = null)
  {

foreach ($_POST as $key => $value):


  $this->data['Result']['form_id']=$id;//form id
  $this->data['Result']['label']=$key;//this is the fieldname
  $this->data['Result']['value']=$value;//fieldvalue



endforeach;

    $this->Session->setFlash('Your entry has been submitted.');


  }

Now i want to perform validations in the Form like whether all the field values have been filled .If not filled , it must show a alert or a message showing to fill the appropriate field. How to do so..Please suggest me.....

One more suggestion needed which would be better to validate whether on the Clientside or server side..

+2  A: 

CakePHP has built in validation: check out the documentation here. You basically tell it the various conditions each attribute of your model must satisfy ("is not empty", etc.). These are then checked automatically when you call the save method - so be sure to check for the return value of that function call.

An example from the linked documentation page:

<?php
class User extends AppModel {
    var $name = 'User';
    var $validate = array(
        'login' => 'alphaNumeric',
        'email' => 'email',
        'born' => 'date'
    );
}
?>
andygeers
Ya but the form fields will change for every form , SO in tat sense how can i keep like email,login and all.
Aruna
Use the beforeValidate() method in the model to dynamically change $validates accordingly. Set a hidden field (or use Session) to tell beforeValidate how to change validates for different forms.http://book.cakephp.org/view/682/beforeValidate
Darren Newton