| 1 |
patForms RFC #0002: patForms FormNamespaces |
|---|
| 2 |
====================================================================== |
|---|
| 3 |
Author: Sven |
|---|
| 4 |
Created: 2005-06-22 |
|---|
| 5 |
Status: draft |
|---|
| 6 |
|
|---|
| 7 |
# Scope: |
|---|
| 8 |
---------------------------------------------------------------------- |
|---|
| 9 |
|
|---|
| 10 |
Invent ability to set a form-wide namespace per form to avoid element |
|---|
| 11 |
naming conflicts when existing forms are joined. |
|---|
| 12 |
|
|---|
| 13 |
# Rationale |
|---|
| 14 |
---------------------------------------------------------------------- |
|---|
| 15 |
|
|---|
| 16 |
When we have two forms that are already in use and now shall be joined |
|---|
| 17 |
to one form, element naming conflicts can occure. For example, we have |
|---|
| 18 |
two forms that map to two different address classes: HomeAddress and |
|---|
| 19 |
DeliveryAddress. It's most likely that both classes will use identical |
|---|
| 20 |
member names and thus that form elements in both forms are named |
|---|
| 21 |
identically. |
|---|
| 22 |
|
|---|
| 23 |
Solution: set a namespace attribute on each of the forms. |
|---|
| 24 |
|
|---|
| 25 |
# Functionality |
|---|
| 26 |
---------------------------------------------------------------------- |
|---|
| 27 |
|
|---|
| 28 |
We will invent a method to set the patForms namespace. When a form |
|---|
| 29 |
namespace is set, the resulting elements (serialized) html code will |
|---|
| 30 |
have a name attribute that uses an array-like syntax. This will result |
|---|
| 31 |
in arrays in $_GET/$_POST that can be read by the patForms after the |
|---|
| 32 |
form has been submitted. |
|---|
| 33 |
|
|---|
| 34 |
For example: |
|---|
| 35 |
|
|---|
| 36 |
$form1 = &patForms::createForm(...); |
|---|
| 37 |
$form1->setNamespace('home'); |
|---|
| 38 |
|
|---|
| 39 |
$form2 = &patForms::createForm(...); |
|---|
| 40 |
$form2->setNamespace('delivery'); |
|---|
| 41 |
|
|---|
| 42 |
When both patForms will be rendered into one Html form tag, this will |
|---|
| 43 |
result in: |
|---|
| 44 |
|
|---|
| 45 |
<form> |
|---|
| 46 |
<input name="home[name]"> |
|---|
| 47 |
<input name="home[street]"> |
|---|
| 48 |
<input name="delivery[name]"> |
|---|
| 49 |
<input name="delivery[street]"> |
|---|
| 50 |
</form> |
|---|
| 51 |
|
|---|
| 52 |
And after the form has been submitted, we will have in $_REQUEST: |
|---|
| 53 |
|
|---|
| 54 |
array( |
|---|
| 55 |
'home' => array('name' => ..., 'street' => ...), |
|---|
| 56 |
'delivery' => array('name' => ..., 'street' => ...), |
|---|
| 57 |
) |
|---|
| 58 |
|
|---|
| 59 |
$form1 will then only read data from $_REQUEST['home'] and $form2 will |
|---|
| 60 |
only read data from $_REQUEST['delivery']. So both forms can use and |
|---|
| 61 |
process their data independently. |
|---|
| 62 |
|
|---|
| 63 |
# Dependencies |
|---|
| 64 |
---------------------------------------------------------------------- |
|---|
| 65 |
|
|---|
| 66 |
This shouldn't break existing code since functionality remains the |
|---|
| 67 |
same as long as no namespace has been set for a patForms instance. |
|---|
| 68 |
|
|---|
| 69 |
# Implementation |
|---|
| 70 |
---------------------------------------------------------------------- |
|---|
| 71 |
|
|---|
| 72 |
[uncomplete] |
|---|
| 73 |
|
|---|
| 74 |
Basically three things will be necessary: |
|---|
| 75 |
|
|---|
| 76 |
1. provide a method patForms::setNamespace($namespace) to set an |
|---|
| 77 |
attribute and make this attribute visible to the patForms_Elements. |
|---|
| 78 |
|
|---|
| 79 |
2. when rendering/serializing the patForms_Elements, use the namespace |
|---|
| 80 |
attribute value to create the appropriate Html element names. |
|---|
| 81 |
|
|---|
| 82 |
3. make sure that client-side Javascript variable names etc. get |
|---|
| 83 |
rendered in an appropriate way. Variable names like |
|---|
| 84 |
namespace[elementname] won't work of course, so we'll probably want |
|---|
| 85 |
to use something like namespace_elementname here. |
|---|
| 86 |
|
|---|
| 87 |
We need to make sure, subpackages also provide support for namespaces. |
|---|
| 88 |
|
|---|
| 89 |
1. patForms_Parser |
|---|
| 90 |
Make sure that the parser is able to treat the namespace attribute |
|---|
| 91 |
correctly: |
|---|
| 92 |
<patForms:Form namespace="delivery"> |
|---|
| 93 |
... |
|---|
| 94 |
</patForms:Form> |
|---|
| 95 |
There are two possible solutions: |
|---|
| 96 |
- The Parser will call setNamespace after creating the form |
|---|
| 97 |
- The namespace property is no real property, but an attribute |
|---|
| 98 |
and can be passed to the constructor. |
|---|
| 99 |
|
|---|
| 100 |
# History |
|---|
| 101 |
---------------------------------------------------------------------- |
|---|
| 102 |
|
|---|
| 103 |
$Log$ |
|---|
| 104 |
Revision 1.2 2005/06/22 15:04:30 schst |
|---|
| 105 |
added some notes on the patForms_Parser |
|---|
| 106 |
|
|---|
| 107 |
Revision 1.1 2005/06/22 14:41:40 sfuchs |
|---|
| 108 |
Startet RFC #0002 |
|---|