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?
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'
);
}
?>
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