Zend Forms;
<form enctype="application/x-www-form-urlencoded" action="index/user" method="post">
<dl class="zend_form"></dl>
</form>
now we have set our action and methods, lets add some elements to the form.
adding element is easy,
first we create a variable $name and assign a text element to it than we set the label of the field and set required to true, like that;
$this->name = new Zend_Form_Element_Text('username');
$this->name->setLabel('User Name')
->setRequired(true);
than we add a new email field and set it label and required attributes.
$this->email = new Zend_Form_Element_Text('email');
$this->email->setLabel('Email Addr.')
->setRequired(true);
last we add a submit button.
$this->submit = new Zend_Form_Element_Submit('Submit');
add this code inside the init() method of the myform.php file.
first basic form is created. now we have to add some validation to the form field, just like the setlabel() and setRequired() method.
when an element is created by calling the new Zend_form_Element_Text() method. it creates the html for it. now we call other method to add functionality to the field.
first we set the label by calling setlable() method of the Zend_form. this will add the label to the field.
next we call a new method called setRequired() method that will set a class called required , and when we submit the form with empty field this class will show us the message that this field is required and the form will be considered as invalid.
Zend_Form simplifies form creation and handling in your web application. It performs the following tasks:
- Element input filtering and validation
- Element ordering
- Element and Form rendering, including escaping
- Element and form grouping
- Element and form-level configuration
Zend_Form makes use of several Zend Framework components to accomplish its goals, including Zend_Config, Zend_Validate,Zend_Filter, Zend_Loader_PluginLoader, and optionally Zend_View.
Creating project;
first we have to create project, open command prompt and go to your project directory, that is htdocs if your using XAMPP, or www if you are using WAMP, now type the following command to create a new project
zf create project {project name}
zf create project helloworld
and this will create a default zend project for you, we have made some changes to the default project but that ok it go along. so far our project look like this
CREATING ZEND FORM
with the command line interface, it is really easy to create forms, just enter the command and zend will create form with necessary directory for you.
to create form, go to your project directory in the command prompt. now create form by typing the following command.
zf create form {name of the file form}
zf create form myForm
and zend will create application/form/myForm.php file for us, having class name that is extending from the Zend_Form.
the following code is pre-written in the file.
<?php
class Application_Form_MyForm extends Zend_Form
{
public function init()
{
/* Form Elements & Other Definitions Here ... */
}
}
now we have the form file created. next we will add some element to the file. it is actually really easy, but the first we have to set the forms action and method attributes. here is the code;
public function init()
{
$this->setAction('index/user') //set the attribute action = "index/user"
->setMethod('post'); //set the attribute method="post"
}
the output of this form is something like that;
<dl class="zend_form"></dl>
</form>
now we have set our action and methods, lets add some elements to the form.
adding element is easy,
first we create a variable $name and assign a text element to it than we set the label of the field and set required to true, like that;
$this->name = new Zend_Form_Element_Text('username');
$this->name->setLabel('User Name')
->setRequired(true);
than we add a new email field and set it label and required attributes.
$this->email = new Zend_Form_Element_Text('email');
$this->email->setLabel('Email Addr.')
->setRequired(true);
last we add a submit button.
$this->submit = new Zend_Form_Element_Submit('Submit');
add this code inside the init() method of the myform.php file.
first basic form is created. now we have to add some validation to the form field, just like the setlabel() and setRequired() method.
when an element is created by calling the new Zend_form_Element_Text() method. it creates the html for it. now we call other method to add functionality to the field.
first we set the label by calling setlable() method of the Zend_form. this will add the label to the field.
next we call a new method called setRequired() method that will set a class called required , and when we submit the form with empty field this class will show us the message that this field is required and the form will be considered as invalid.
No comments:
Post a Comment