I just thought I'd note how interesting it is that our minds can figure out something so quickly one moment, and then have so much difficulty with the same problem in the future. This is probably attributed to many factors like mood, environment (place, sounds, people around you), nourishment, etc.
Thursday, December 11, 2008
Time and time again?
I was at a co-worker's place today and his stove/oven's time was an hour off because of Daylight Saving's Time. Now this is something that happens (or will happen!) to all of us eventually - we do something extremely simple, then later we come back to it, and can't figure out how in the world we did it. The same thing happened to me when I had to change my car's clock - one time change I did it without a problem, I looked at the choices and figured it out almost instantly. The next time change, I couldn't figure it out for the life of me.
Wednesday, December 3, 2008
CakePHP: Difference in form elements
(CakePHP 1.2)
Like many programming languages, there are always more than one way to do the same thing. Some give you more control, some help you develop quicker.(Note: This post is directed toward new CakePHP users, or those unfamiliar with the difference between $form->input() and the form specific functions)
After using CakePHP for a short while, I found two different ways of doing form elements. There's "$form->input()" and the form specific functions. But what's the difference between these two? $form->input() has more "magic" but means you might have less control over it...
Let's look into them in more detail:
echo $form->create('Post');
echo $form->input('Post.body');
echo $form->end('Submit');
This small code block creates an input box, wrapped up in a div and complete with a label.
Let's look at an alternative:
echo $form->create('Post');
echo $form->text('Post.body');
echo $form->end('Submit');
This just creates the form action, and the input box. No divs, no labels, no magic.
The most important differences about these two functions doesn't lie at the surface though.
First, $form->input() is wonderful for edit forms. Why? Because you don't have to pass a variable to the view.
Here's an example:
/controllers/posts_controller.php
class PostsController extends AppController {
function edit($_id) {
if (!empty($this->data) {
$this->Post->id = $_id;
$this->Post->save($this->data); //update post
}
$this->data = $this->Post->findById($_id); //populates input box
}
}
This controller class doesn't set any variables, but see the line commented as "populates edit forms"? $form->input() will automatically grab relevant data from the $this->data array and display it in the input box.
Here's the view:
/views/posts/edit.ctp
echo $form->create('Post'); //begin the form
echo $form->input('Post.title'); //display label, input box, wrapped up in a div
echo $form->input('Post.body', array('type' => 'textarea'); //display label, textarea box, wrapped up in a div
echo $form->end('Submit'); //end form and display submit button
That's all that's required to automagically populate edit forms - it is a great time saver...but there are problems when you get into CSS. Because $form->input() displays Labels (if you choose to display labels) and the input boxes wrapped up in it's own div, it can cause problems for your designer (or you).
The alternative is to use the form specific functions ($form->text(), $form->select(), etc.) - though it might be longer to code, your designer will probably love you for it. Just remember that those forms are not automatically populated (you have to set variables to the views, and in each supply the variable to the form function).
But wait...there's more.
Validation. $form->input() will also automagically display validation errors, where as the form specific functions do not.
To display the validation errors, you'll need to place $form->error(); in your view (this will also allow greater flexibility where you want your validation error placed)
Here's an example of edit.ctp using the form specific functions:
echo $form->create('Post'); //begin the form
echo $form->label('Post.title'); //displays a label
echo $form->text('Post.title'); //displays the input box
echo $form->error('Post.title'); //displays validation errors for the title input box
echo $form->label('Post.body'); //displays the text area
echo $form->textarea('Post.body'); //displays the textarea for the post body
echo $form->error('Post.body'); //displays validation errors for the textarea box
echo $form->end('Submit'); //end the form and display a submit button
That's a lot more coding, but it gives you more power to design your page.
Here's a small list to recap:
Benefits of $form->input();
- Model Introspection - grabs data, and chooses a form type based on that data (can override it by setting 'type' in the options array)
- Displays validation errors
- Displays label
- the added divs might impede your design work or annoy your designer
- Gives you the bare minimum when you need it
- Allows greater flexibility for design work
- Doesn't display validation errors
- Form needs you to pass the data into the function
is a great place for information. As well as the Manual
Both these form functions are extremely helpful the way they are - this post was just to note the differences between the two, so that it might help someone use the correct function when they need it (and save them some time, code and/or headaches!)
Please note: If there are any mistakes, please let me know and I'll fix the post asap (It's 1:17am at the moment, so there's bound to be a few *wink* ). If I left anything out between the differences of $form->input() and the form specific functions, feel free to comment.
Comments, questions, or feedback on this post? I'm all ears....or eyes.
Subscribe to:
Comments (Atom)