Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Performing validations for the form

Tags:

jquery

cakephp

I am using Jquery and cakephp for my application.

I am having a form created in my page using $form->create. The form contains many fields which the user will fill 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 an alert or a message asking the user to fill the appropriate field. How do I do that?

One more suggestion needed: which would be better, validation on the client side or the server side?

like image 983
useranon Avatar asked Nov 24 '25 22:11

useranon


1 Answers

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'
    );
}
?>
like image 98
andygeers Avatar answered Nov 26 '25 16:11

andygeers