RSS

Search Engine

Saturday, May 15, 2010

Data Validation

Cake goes a long way in taking the monotony out of form input validation. Everyone hates coding up endless forms and their validation routines. CakePHP makes it easier and faster.

To take advantage of the validation features, you'll need to use Cake's FormHelper in your views. The FormHelper is available by default to all views at $form.

Here's our add view:

Plain Text View
	


Add Post


echo $form->create('Post');
echo $form->input('title');
echo $form->input('body', array('rows' => '3'));
echo $form->end('Save Post');
?>

  1. Add Post

  2. echo $form->create('Post');
  3. echo $form->input('title');
  4. echo $form->input('body', array('rows' => '3'));
  5. echo $form->end('Save Post');
  6. ?>

Here, we use the FormHelper to generate the opening tag for an HTML form. Here's the HTML that $form->create() generates:

Plain Text View

  1. <form id="PostAddForm" method="post" action="/posts/add">

If create() is called with no parameters supplied, it assumes you are building a form that submits to the current controller's add() action (or edit() action when id is included in the form data), via POST.

The $form->input() method is used to create form elements of the same name. The first parameter tells CakePHP which field they correspond to, and the second parameter allows you to specify a wide array of options - in this case, the number of rows for the textarea. There's a bit of introspection and automagic here: input() will output different form elements based on the model field specified.

The $form->end() call generates a submit button and ends the form. If a string is supplied as the first parameter to end(), the FormHelper outputs a submit button named accordingly along with the closing form tag. Again, refer to Chapter "Built-in Helpers" for more on helpers.

Now let's go back and update our /app/views/posts/index.ctp view to include a new "Add Post" link. Before the

, add the following line:

Plain Text View
link('Add Post',array('controller' => 'posts', 'action' => 'add'))?>
  1. echo $html->link('Add Post',array('controller' => 'posts', 'action' => 'add'))?>

You may be wondering: how do I tell CakePHP about my validation requirements? Validation rules are defined in the model. Let's look back at our Post model and make a few adjustments:

Plain Text View
class Post extends AppModel

{
var $name = 'Post';

var $validate = array(
'title' => array(
'rule' => 'notEmpty'
),
'body' => array(
'rule' => 'notEmpty'
)
);
}
?>
  1. class Post extends AppModel
  2. {
  3. var $name = 'Post';
  4. var $validate = array(
  5. 'title' => array(
  6. 'rule' => 'notEmpty'
  7. ),
  8. 'body' => array(
  9. 'rule' => 'notEmpty'
  10. )
  11. );
  12. }
  13. ?>

The $validate array tells CakePHP how to validate your data when the save() method is called. Here, I've specified that both the body and title fields must not be empty. CakePHP's validation engine is strong, with a number of pre-built rules (credit card numbers, email addresses, etc.) and flexibility for adding your own validation rules. For more information on that setup, check the Data Validation chapter.

Now that you have your validation rules in place, use the app to try to add a post with an empty title or body to see how it works. Since we've used the input() method of the FormHelper to create our form elements, our validation error messages will be shown automatically.

0 comments:

Post a Comment