tags:

views:

69

answers:

3

I have recently been assigned a CSS & design project that's in a CakePHP environment and would like to know if I can add an additional button to the form, for a "sign up" link.

The form is currently:

 echo $form->create('User', array('action' => 'login'));
    echo $form->inputs(array(
      'legend' => 'Please log in:',
      'username', 'password'));
 echo $form->end('Login');

I would like to inject a button that goes to an action of 'register', preferably after the "Login" button, on the same line, like this:


username: [ ]

password: [ ]

[Log In] [Register]


I have everything but the 'register' button. Is this possible using the 'automagic' form creation? Thank you.

+2  A: 

Yep, pretty easy in Cake! Just use the FormHelper's "button" method.

echo $form->button( 'Register' );

of course, this will still send the action to the URL specified in Form::create, but you can do some magic in the controllers to correctly act on the request.

http://book.cakephp.org/view/791/button

Travis Leleu
Hi, thanks for the response. But the problem I am having is working that "magic". I have a controller ('users') and action ('register') present in the link that exists now for it. But I would like to convert it to a form button that does the same magic, but I can't seem to get it to work. thanks , Court
cloudhead
I'm a little confused. Are you saying that you'd like the form button to actually redirect to a different URL (say, /users/register versus /user/login)? In that case, you have several options: use js to rewrite the 'action' attribute of the form. You could make the entire form action post to /users/register, then in your UsersController::register() method just call login if that was the button they clicked. I recommend sending the data to a function that determines whether or not it's a login/register based on the button.
Travis Leleu
I just saw Spawn's answer -- his solution would do the js rewrite. But it doesn't degrade gracefully if someone has js disabled, which is why I wouldn't recommend it (the other method described above will work for any browser configuration).
Travis Leleu
@Travis Leleu,actually I believe the simplest effective way would be graceful.and with no defence I'm also curious why people do the js disable thing when they view the pages?
SpawnCxy
@Spawn some percentage of web users do not have javascript enabled when they browse the web. Some folks are using older computers, some folks have IT people turn it off, and some folks (like me) only allow js to run on sites that I whitelist. "Graceful degradation" means that your site will still work even as you start turning off browser features. So your site should work without js, and just use that to enhance the user experience, not be required for it.
Travis Leleu
@Travis Leleu,+1 for the nice explanation:)
SpawnCxy
@Spawn No problem. For more on the subject, here are two great articles: http://www.sitepoint.com/blogs/2009/09/22/progressive-enhancement-graceful-degradation-basics/ and http://www.alistapart.com/articles/understandingprogressiveenhancement/
Travis Leleu
A: 

Maybe you can try this:

echo $form->button('Register', array('onclick'=>"window.location='/users/register';"));
SpawnCxy
A: 

I'd make another form.

echo $form->create('User', array('action'=>array('controller'=>'users','action'=>'register'));
echo $form->end('Register');

Then at least you are not really 'hacking' the framework at all, and up with semantically correct code, with an accurate action.

DavidYell