Changes between Version 4 and Version 5 of CallbacksAndForms


Ignore:
Timestamp:
11/29/2012 04:23:25 PM (5 years ago)
Author:
apoirier
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • CallbacksAndForms

    v4 v5  
    4444This simple GuessBook example associates a callback to a textarea element, 
    4545that append the new messages to the list of the existing ones: 
    46                   
     46 
    4747.. code-block:: python 
    4848 
     
    9494``h.input(type='password')`` ``.action(callback)``      the text received 
    9595``h.input(type='radio')``    ``.action(callback)``      boolean: is the radio button selected ? 
    96 ``h.input(type='checkbox')`` ``.action(callback)``      *no parameter* (only the callbacks of the 
    97                                                         selected checkboxes are called) 
     96``h.input(type='checkbox')`` ``.action(callback)``      the value of the ``value`` attribute (``on`` if not 
     97                                                        ``value`` attribute is set) 
    9898``h.input(type='submit')``   ``.action(callback)``      *no parameter* 
    9999``h.input(type='hidden')``   ``.action(callback)``      *no parameter* 
     
    128128  1. the ``form`` ``pre_action()`` callback 
    129129  2. the ``select``, ``textarea`` and ``input`` of type ``text``, ``password``, 
    130      ``radio``, ``checkbox``, ``hidden`` and ``file`` ``action()`` callback  
     130     ``radio``, ``checkbox``, ``hidden`` and ``file`` ``action()`` callback 
    131131  3. the ``form`` ``post_action()`` callback 
    132132  4. the ``a`` and ``input(type='submit')`` ``action()`` callback 
     
    159159selected checkboxes before to process the callbacks. This reset method is 
    160160typically the ```pre_action`` callback of the ``h.form``: 
    161     
     161 
    162162.. code-block:: python 
    163163 
     
    170170       def __init__(self): 
    171171           self.reset_colors() 
    172                 
     172 
    173173       def reset_colors(self): 
    174174           self.colors = [] 
     
    204204           h << h.br << h.input(type='submit', value='Send') 
    205205       return h.root 
    206         
     206 
    207207The ``var.Var()`` objects 
    208208------------------------- 
     
    217217callback. 
    218218 
    219 For example, the first snippet can be rewrote as:  
     219For example, the first snippet can be rewrote as: 
    220220 
    221221.. code-block:: python 
     
    288288  - that keeps the error message of the validation in its ``error`` attribute, 
    289289    or ``None`` if the value is valid. 
    290    
     290 
    291291.. code-block:: pycon 
    292292 
    293293   >>> from nagare.editor import Property 
    294     
     294 
    295295   >>> def is_lesser_than_10(n): 
    296296   ...     if n<10: 
     
    298298   ...     else: 
    299299   ...         raise ValueError, 'must be lesser than 10' 
    300     
     300 
    301301   >>> number = Property() 
    302302   >>> number.validate(is_lesser_than_10) 
     
    310310   2 
    311311   >>> number.error 
    312     
     312 
    313313   >>> # Invalid value 
    314314   >>> number.set(20) 
     
    320320   u'must be lesser than 10' 
    321321 
    322    # Setting a valid value by calling the property    
     322   # Setting a valid value by calling the property 
    323323   >>> number(2) 
    324324   2 
     
    330330   2 
    331331   >>> number.error 
    332     
    333    # Setting an invalid value by calling the property    
     332 
     333   # Setting an invalid value by calling the property 
    334334   >>> number(20) 
    335335   20 
     
    354354   >>> from nagare.validator import IntValidator 
    355355   >>> from nagare.editor import Property 
    356     
     356 
    357357   >>> number = Property() 
    358358 
     
    398398 
    399399An ``Editor`` object will modified the target object attributes only if all the 
    400 validating functions of its properties validate the data from the form.   
     400validating functions of its properties validate the data from the form. 
    401401 
    402402Putting all together 
     
    411411   from nagare import editor, presentation, component 
    412412   from nagare.validator import StringValidator, IntValidator 
    413     
     413 
    414414   class Person: 
    415415       def __init__(self): 
    416416           self.name = '' 
    417417           self.age = 0 
    418             
     418 
    419419We create a dedicated editor for this objects, to be able to edit these 
    420420attributes with a web form: 
     
    432432           # the name is required 
    433433           self.name.validate(lambda name: StringValidator(name).not_empty()) 
    434             
    435            # We use an integer validator to check the age is positive  
    436            self.age.validate(lambda age: IntValidator(age).greater_than(0))               
     434 
     435           # We use an integer validator to check the age is positive 
     436           self.age.validate(lambda age: IntValidator(age).greater_than(0)) 
    437437 
    438438       # This ``commit`` method will write back the values of the ``name`` and 
     
    440440       def commit(self): 
    441441           super(PersonEditor, self).commit(('name', 'age')) 
    442             
     442 
    443443Then we can create a view on the ``PersonEditor`` objects to render the web 
    444444form: 
     
    457457            # not ``None`` 
    458458            h << 'Name: ' << h.input.action(self.name).error(self.name.error) << h.br 
    459              
     459 
    460460            h << 'Age: ' << h.input.action(self.age).error(self.age.error) << h.br 
    461              
     461 
    462462            h << h.input(type='submit', value='Send').action(self.commit) 
    463              
     463 
    464464        return h.root 
    465465 
     
    475475           # Create a ``PersonEditor`` object that wrap the ``Person`` object to edit 
    476476           editor = PersonEditor(self.person) 
    477             
     477 
    478478           # Transform the editor object into a component 
    479479           self.editor_component = component.Component(editor) 
    480                    
     480 
    481481   @presentation.render_for(App) 
    482482   def render(self, h, *args): 
     
    486486 
    487487       h << h.hr 
    488            
     488 
    489489       # Render the editor 
    490490       h << self.editor_component 
     
    501501 
    502502.. note:: 
    503     
     503 
    504504   For more complete examples of validations, use of all the possible form 
    505505   elements, inter-fields validation ... see the