DooForm learn to work with forms
Ok in this tutorial I will show you how do you work with forms, its very simple.
Loading DooForm helper is very easy:
Doo::loadHelper('DooForm');
First you create form from array and then just call
echo $form->render();
To render the form, render function is returning html of the form. So we will begin making one simple element, lets say textfield:
$form = new DooForm(array(
'method' => 'post',
'action' => $action,
'elements' => array(
'username' => array('text', array(
'required' => true,
'label' => 'My username: ',
'attributes' => array("style" => 'border:1px solid #000;', 'class' => 'username-field'),
'field-wrapper' => 'div',
'validators' => array(
array('username',4,7),
array('maxlength',6,'This is too long'),
array('minlength',6)
)
))
)
));
As you can see all form data you can add into form array, so when you are creating form you will have to define method, action and elements you have in your form, elemnts is another array, that array will contain all form elements. You begin with naming your element and then create array that will describe that element (type, label, validators, field-wrappers, etc…)
So in element you will have for example:
'username' => array('text', array(
'required' => true,
'label' => 'My username: ',
'attributes' => array("style" => 'border:1px solid #000;', 'class' => 'mitar'),
'field-wrapper' => 'div',
'validators' => array(
array('username',4,7),
array('maxlength',6,'This is too long'),
array('minlength',6))
)
),
So as you can see you define type of the element as first value of the array, second value is also array that defines other stuff such as label, attributes, validators etc… Label is field label, attributes are array that will go inside element so you can add style, class or something else. field-wrapper is html that wrapps your element, default is <dt>.
Now we will talk little about validators, all validators you have defined in DooValidator class, so validators is an array, and in it you will store all validators you need for that element. Some elements have “built in validators” such as element file or captcha, we will talk about them later on. So as you can see we are using validators from DooValidators class, when form is validating with isValid() method DooForm is checking validations with DooValidate helper class.
Now I will show you how should you work with this forms, for example lets say that we have form from above that has only one field username, now we have function for example register and in it we will do form validation:
if ($this->isPost()) {
if ($form->isValid($_POST)) {
echo "Form is valid!";
} else {
echo 'Form is not valid please correct all errors';
$this->view()->form = $form->render();
}
} else {
$this->view()->form = $form->render();
}
As you can see isPost method doesnt realy exist in DooController class its just my function for checking if method is post, I will now show you function in case you want to know how it looks like:
public function isPost() {
if ($_SERVER['REQUEST_METHOD'] == "POST") return true;
else return false;
}
So if method is post then “form is submited” and we need to see if its valid, we are doing that with isValid() function that function will see if all forms are ok and it will add errors to form if there are any, so when you do render() method after you will have form with errors showing if some field has erorrs. Ok if form is not valid we should define some variable in view that will have form html:
$this->view()->form = $form->render();
Now in your view script you should just have:
echo $this->form;
And you have everything running
Now I will present you little complexed form that has almost all needed elements:
public function test() {
$action = $this->host . 'test';
$form = new DooForm(array(
'method' => 'post',
'action' => $action,
'elements' => array(
'username' => array('text', array(
'required' => true,
'label' => 'My username: ',
'attributes' => array("style" => 'border:1px solid #000;', 'class' => 'mitar'),
'field-wrapper' => 'div',
'validators' => array(
array('username',4,7),
array('maxlength',6,'This is too long'),
array('minlength',6)
)
)),
'password' => array('password', array(
'required' => true,
'label' => 'Password: ',
'validators' => array(array('minlength',6))
)),
'repeat_password' => array('password', array(
'required' => true,
'label' => 'Repeat password:',
'validators' => array('equalas', 'post', 'password', 'Passwords mismatch!')
)),
'looking_type' => array('select', array(
'required' => true,
'multioptions' => array(1 => 'good looking', 2 => 'better looking', 3 => 'unfuckable'),
'label' => 'My look:'
)),
'looking_for' => array('MultiCheckbox', array(
'required' => false,
'multioptions' => array(0 => 'love', 1 => 'hate', 3 => 'other'),
'label' => 'I am looking for: ',
'validators' => array('notnull')
)),
'test_check' => array('checkbox', array(
'required' => true,
'label' => "I agree with condition" . ':',
'validators' => array(array('notnull','You must agree with terms of use!'))
)),
'captcha' => array('captcha', array(
'required' => false,
'directory' => Doo::conf()->ROOT_DIR . '_captcha',
'url' => $this->host . '_captcha/',
'image' => Doo::conf()->ROOT_DIR . 'global/img/captcha_bg.jpg',
'label' => 'Enter letters from picture',
'message' => 'Captcha is not valid!'
)),
'submit' => array('submit', array(
'label' => "Register"
))
),
));
$data['content'] = 'test';
if ($this->isPost()) {
if ($form->isValid($_POST)) {
echo "Forma is valid!";
} else {
echo 'Form is not valid!';
$this->view()->form = $form->render();
}
} else {
$this->view()->form = $form->render();
}
$this->renderc('template', $data, true);
}
First thing that is funny is in password element validator equalAs, well that validator should check if some value is same as some value from get or post method so
'validators' => array('equalas', 'post', 'password', 'Passwords mismatch!')
means if this value is equal as password value from post method its ok if not add error that passwords mismatch!
Now lets look at the captcha element, for this element you need to do little setup, first of all you need directory where to store captcha images, then you need to define url of captcha directory, image is background image of captcha, everything else I think you know thats label and message you want to show when captcha is not good.
Another element that will be updated in DooForm class is file element, I am syncing it as I type this tutorial, so this element looks like this:
'photo' => array('file', array(
'required' => true,
'label' => 'Select photo:',
'destination' => $this->_basePath . 'static/userphoto/tmp',
'size' => 2048000,
'extension' => 'jpg,png,gif'
)),
As you can see you can add size and extension. Size is the size of file and extension is list of extensions you want to have in your file form, all extension types are delimited with ‘,’ without space. Thats about it, if you have any questions please post comments.
New in DooForm class, we have display groups for making display group you simply make form first then call:
$form->addDisplayGroup("myDisplayGroup", array("username", "password", "repeat_password"));
As you can see first argument is name of your display group and second argument is array with names of your fields, now DooForm will make internal display group that can be returned by calling renderDisplayGroup function, with render function you will be returned whole form.
So to get HTML for your display group just call:
$form->renderDisplayGroup("myDisplayGroup");
About wrappers in new DooForm you have:
field-wrapper (Wrap’s both element and element’s label)
label-wrapper (Wrap’s just label)
element-wrapper (Wrap’s just element)


Leng
28 Sep, 2009
FYI,
The example given is using native PHP as template.
chucka
11 Oct, 2009
Check here for more information on available validation options: http://doophp.com/documentation/api_svn/doo-helper/DooValidator.html
Chuck
13 Oct, 2009
I see this tutorial uses the variable Doo::conf()->ROOT_DIR for the captcha element. That is not a variable in common.conf.php by default. By the looks of it, it points to the base directory in which your app’s index.php is located. Should the _captcha folder really be located there? Would it be better organized under protected or global? And if so, which would be best?
Milos Kovacki
14 Oct, 2009
Yes you are right variable ROOT_DIR is some variable that I assigned to directory of my webapp and there is _captcha dir in it where I store captchas.
You can have it anywhere you want, I wrote captcha element like that so you would have captchas stored in _captcha dir, in light of security there is no way that someone can guess md5 of some random string wich is filename of that captcha picture.
smit shah
10 Nov, 2009
hi i got the error
Fatal error: Class ‘DooForm’ not found in C:\wamp\www\emp\app\protected\controller\EmployeeController.php on line 8
Milos Kovacki
10 Nov, 2009
I guess you didnt download latest trunks from SVN, DooForm is not implemented in DooPhp 1.2 it will be implemented in 1.3 when it comes out.
Download latest DooPhp from SVN at http://code.google.com/p/doophp
Chuck
18 Nov, 2009
Using this, how would one avoid duplicate rows from a page refresh or browser navigation usage?
img
26 Dec, 2009
this is good to be use in backend , admin cotrol panel…
seralf
7 Jan, 2010
Hi
i’ve found that it’s important to call:
Doo::loadHelper(’DooValidator’);
Doo::loadHelper(’DooForm’);
in the right order.
Inverting the two load would case a DooValidator class not found error…
Murray
25 Feb, 2010
Your captcha example has:
‘captcha’ => array(’captcha’, array(
‘required’ => false,
Does this mean the captcha is not required, so if this field is left blank the form can still validate?
Milos Kovacki
26 Feb, 2010
Yes, so if captcha is blank it will not validate if it is not blank it will validate, but form should be valid if there is no captcha entered, it was just glitch, it should be required = true
Patrick
9 Aug, 2010
It would be useful to be able to set a class and id on the form. This currently seems to be hard-coded as ‘class=”doo-form”>’.
Milos Kovacki
9 Aug, 2010
It’s not hard coded, you can fully customize everything.
For example:
'password' => array('password', array(
'attributes' => array('class' => 'some_class'),
'label' => 'Password: ',
'validators' => array(array('minlength',6))
)),
Actually you have this example on the start of tutorial.
Patrick
9 Aug, 2010
Hi Milos – thanks for your answer!
I mean set class & id for the form though: i.e.
Is that possible?
My code got swallowed – I mean for
form action=”/contact” method=”post” class=”not-a-doo-form” id=”example-id”
Milos Kovacki
9 Aug, 2010
Not right now, I will change it in few days to make form class editable, tnx for notice. I totaly forgot about it.
ccc353
11 Aug, 2010
in most cases it would be faster:
function isPost( )
{
return ( strtoupper( $_SERVER[ 'REQUEST_METHOD' ] ) == ‘POST’ );
}