Thursday, July 30, 2009

Cake PHP 1.2 - Applying CSS to forms.

Today I will explain in a nutshell how to apply a css style to a form generated by the Form Helper. My form is very simple and serves as a registration form. You can find how to create a model and a controller for such form here: http://planetcakephp.org/aggregator/items/1620-creating-a-community-in-five-minutes-with-cakephp. Therefore, I will focus only on how to apply some css to this form.

My form has only 4 required fields and is generated by the following view:

<div class="users form">	
<?php echo $form->create('User', array('class'=>'form'));?>
<p class="formtitle">Sign Up</p>
<fieldset>
	<legend class="formtitle">Please complete the form below.</legend>
	<?php
		echo $form->input('username',  array( 
			'label' => 'Login', 
			'div'=>'formfield', 
			'error' => array(
				'wrap' => 'div', 
				'class' => 'formerror'
				)
			));
		echo $form->input('password',  array( 
			'label' => 'Password', 
			'div'=>'formfield', 
			'error' => array(
				'wrap' => 'div', 
				'class' => 'formerror'
				)
			));
		echo $form->input('password_confirm', array(
			'label' => 'Confirm password', 
			'type'=>'password', 
			'div'=>'formfield', 
			'error' => array(
				'wrap' => 'div', 
				'class' => 'formerror'
				)
			));
		echo $form->input('name',  array( 
			'label' => 'Name', 
			'div'=>'formfield', 
			'error' => array(
				'wrap' => 'div', 
				'class' => 'formerror'
				)
			));
		echo $form->input('surname',  array( 
			'label' => 'Surname', 
			'div'=>'formfield', 
			'error' => array(
				'wrap' => 'div', 
				'class' => 'formerror'
				)
			));
		echo $form->input('email',  array( 
			'label' => 'E-mail', 
			'div'=>'formfield', 
			'error' => array(
				'wrap' => 'div', 
				'class' => 'formerror'
				)
			));
		echo $form->input('city_id',  array( 
			'label' => 'City', 
			'div'=>'formfield', 
			'error' => array(
				'wrap' => 'div', 
				'class' => 'formerror')));
	?>
</fieldset>	
<?php echo $form->end('Submit');?>
</div>

Some explanations:

  • To add a class to a form tag:
    $form->create('User', array('class'=>'form')) 
    generates:
    <form class="form" id="UserAddForm" method="post" action="/kultura/users/add">
  • To add a class to an input:
    echo $form->input('username',  array( 
    			'label' => 'Login', 
    			'div'=>'formfield', 
    			'error' => array(
    				'wrap' => 'div', 
    				'class' => 'formerror'
    				)
    			));
    generates this:
    <div class="formfield required">
    <label for="UserUsername">Login</label>
    <input name="data[User][username]" type="text" maxlength="20" value="" id="UserUsername" />
    </div>
    An error in this case will appear in:
    <div class=”formerror”></div>

A sample css style may look like this:

.form{
	font-family: Verdana;    
	font-size:1em;
	margin:1em;
	padding:1em;	
}

.form p.formtitle{
	color: #026475;
	font-size:1.3em;
	font-weight: bold;
}

.form fieldset{
	width:40em;
	border:1px solid #FFE545;	
	background-image: url(../img/Contact.png);
	background-position: bottom right;
	background-repeat: no-repeat;
}

.form fieldset legend{
	color: #026475;
}

.formfield{
	width:30em;
	padding:5px;
}

.formfield label{
	display:block;
	float:left;
	width:12em;
	padding:1px;
	color:#C2A34F;
	text-align:right;
}

.formfield input, .formfield  select{
	padding:0.15em;
	width:14em;
	border:1px solid #ddd;
	background:#FEFBAF;
	
	font-family: Verdana;    
	font-size:1em;
	
	-moz-border-radius:0.4em;
	-khtml-border-radius:0.4em;
}

.formfield input:hover, input:focus {
	border-color:#c5c5c5;
	background:#f6f6f6;
} 

.required input {
	border:1px solid #FBB829;
}

.form .submit input{
	font-family: Verdana;    
	font-size:1em;
	margin-top: 0.3em;
}

.formerror{
	position:relative;
	left:12.1em;
	color:#FBB829;
}

The result:

nazwy 

More interesting sources about how to use css with forms:

Very interesting article about forms design patterns:

and, b.t.w., some hint how to do CakePHP forms more secure using Security component:

Enjoy!

5 comments:

  1. can you give us an example on how to style checkboxes which is populated from the table which is a HABTM relation bw two tables.

    CAKEPHP
    echo $form->input('Cuisine', array('type' => 'select', 'multiple' => 'checkbox'));





    i want to create a column of 4. with the check boxes. so that with more entries it doesnt take up more space.

    i have posted a details of the problem on this forum.
    http://forum.phpvideotutorials.com/showthread.php?t=5078

    ReplyDelete
  2. In fact I have no experience with this kind of form yet, but it seems to be just a css problem.
    If I assume that cake generates this output:

    <div class="input select">
    <label for="CuisineCuisine">Cuisine</label>
    <input type="hidden" name="data[Cuisine][Cuisine]" value="" />
    <div class="checkbox">
    <input type="checkbox" name="data[Cuisine][Cuisine][]" value="1" id="CuisineCuisine1" />
    <label for="CuisineCuisine1">American</label>
    </div>
    <div class="checkbox">
    <input type="checkbox" name="data[Cuisine][Cuisine][]" value="2" id="CuisineCuisine2" />
    <label for="CuisineCuisine2">Andhra</label>
    </div> <div class="checkbox">
    <input type="checkbox" name="data[Cuisine][Cuisine][]" value="2" id="CuisineCuisine2" />
    <label for="CuisineCuisine2">Polish</label>
    </div>
    <div class="checkbox">
    <input type="checkbox" name="data[Cuisine][Cuisine][]" value="2" id="CuisineCuisine2" />
    <label for="CuisineCuisine2">Portuguese</label>
    </div>
    </div>

    The css to put it in 3 cols would be more or less like this:

    .select{
    width:276px;
    }
    .select label{
    display:block;
    }

    .select .checkbox{
    display:block;
    width:80px;
    margin: 0;
    padding: 5px;
    border: 1px solid black;
    float:left;
    }

    .select .checkbox label{
    display:inline;
    }

    I hope that gives you any idea how to solve ur problem.

    ReplyDelete
  3. As we move to cake 1.3,
    there is some improvements in form helper
    http://book.cakephp.org/view/1616/x1-3-improvements

    now you can define all default options form elements like, erap, div, class,..

    ReplyDelete
  4. Promaestros believe in fulfilling the high expectations of our clients and offer them a skilled team to hire CakePHP programmer for their customized solutions.
    Promaestos provides Rapid web apps development in CakePHP, Support on your existing CakePHP project, Customized CakePHP codes to the clients, Division in programming logic and design,Profound CakePHP developer base.

    ReplyDelete