when we create form element through zend command. it creates the necessary class and extend from Zend_Form. in the init() method when call the $this variable to refer to the form that we have created.
$this
this refers to the class we are in. so if we are in the form class that we have created and we call $this we will be referring to the class we are in. so in zend we can call any function or method of Zend_Form through through $this->{method name}.
in the similar way we can call other methods if we are in other classes that extends any other Zend Class.
adding elements and attributes to form
$this->setAction('index/users') and $this->setMethod('post') method will will set the action and method attributes of the form. this will set the form like;
<form action="index/user" method="POST"></form>
$this->{variable name} is use to define a variable.
Zend provide us with the functionality to create forms elements like text field, password, check boxes, drop down menu etc.
creating text field.
$this->name = new Zend_form_element_text('{name of text field}');
$this->name = new Zend_form_element_text('Name');
this will create text element like that;
<input type="text" name="Name" />
creating password field.
$this->name = new Zend_form_element_password('{name of text field}');
$this->name = new Zend_form_element_password('passwordfield');
<input type="password" name="passwordfield" />
creating a checkbox
$this->checkbox = new Zend_form_element_checkbox({name of checkbox},{other options});
$this->checkbox = new Zend_form_element_checkbox('checkbox',array(
'label' => 'mylabel',
'name' => 'myname',
'checkedValue' => 'value1',
'uncheckedValue' => 'value2'
));
<dd id="myname-element">
<input type="hidden" name="myname" value="value2"><input type="checkbox" name="myname" id="myname" value="value1"></dd>
for multiple checkbox we use other method that is;
multiple checkbox creation;
$this->multi = new Zend_Form_Element_MultiCheckbox('name',array(
'label' => 'mylabel',
'multiOptions'=>array(
'student'=>'students',
'teacher'=>'Teacher',
'employee'=>'Employee'
)
));
<dd id="name-element">
<label><input type="checkbox" name="name[]" id="name-student" value="student">students</label><br><label><input type="checkbox" name="name[]" id="name-teacher" value="teacher">Teacher</label><br><label><input type="checkbox" name="name[]" id="name-employee" value="employee">Employee</label></dd>
this default styles of the multi-checkboxes are not good, we have to set decorators, we will we Decorators in zend framework later.
<label><input type="checkbox" name="name[]" id="name-student" value="student">students</label><br><label><input type="checkbox" name="name[]" id="name-teacher" value="teacher">Teacher</label><br><label><input type="checkbox" name="name[]" id="name-employee" value="employee">Employee</label></dd>
this default styles of the multi-checkboxes are not good, we have to set decorators, we will we Decorators in zend framework later.
Radio Buttons
$this->multi = new Zend_Form_Element_Radio('radio',array(
'label' => 'mylabel',
'multiOptions'=>array(
'student'=>'students',
'teacher'=>'Teacher',
'employee'=>'Employee'
)
));
<dd id="radio-element">
<label><input type="radio" name="radio" id="radio-student" value="student">students</label><br><label><input type="radio" name="radio" id="radio-teacher" value="teacher">Teacher</label><br><label><input type="radio" name="radio" id="radio-employee" value="employee">Employee</label></dd>
Select elements
$this->multi = new Zend_Form_Element_Select('select',array(
'label' => 'mylabel',
'multiOptions'=>array(
'student'=>'students',
'teacher'=>'Teacher',
'employee'=>'Employee'
)
));
<select name="select" id="select">
<option value="student">students</option>
<option value="teacher">Teacher</option>
<option value="employee">Employee</option>
</select>
Setting labels to the elements;
Adding elements in zend is easy and adding labels to these element almost same for every element. every zend element has a method known as setLabel({name of label });. with that element we can set any element with a label for it. for example if we want to set the label for the name textfield we will set like that;
$this->name->setLabel("Name of Text");
for password ;
$this->password->setLabel("Enter your password");
and so on.
Validation in Zend
A form element can be validated either by passing the validate value or by passing Zend_validate_* object. like for alphabets validation we can either pass 'alpha' or Zend_validate_alpha to the addValidator method.
adding validation to text field can be like that;
$this->name->addValidator('alpha'); or
$this->name->addValidator(new Zend_Validate_Alpha);
or if we want to validate the password field only to the alpha-numeric value we can do that as
$this->password->addValidator('alnum');
or
$this->passworld->addValidator(new Zend_Validate_Alnum);
and when we add something other than alph-numeric to the password field we will be given error saying that this field can only be alph-numeric.
No comments:
Post a Comment