{ "info": { "author": "Tim van der Linden", "author_email": "tim@shisaa.jp", "bugtrack_url": null, "classifiers": [ "Development Status :: 3 - Alpha", "Environment :: Web Environment", "Intended Audience :: Developers", "License :: OSI Approved :: BSD License", "Natural Language :: English", "Operating System :: OS Independent", "Programming Language :: Python :: 2", "Programming Language :: Python :: 2.6", "Programming Language :: Python :: 2.7", "Programming Language :: Python :: 3", "Programming Language :: Python :: 3.3", "Programming Language :: Python :: 3.4", "Topic :: Internet :: WWW/HTTP :: Dynamic Content", "Topic :: Software Development :: Libraries :: Python Modules" ], "description": "WTForms Dynamic Fields\n======================\n\nSimple wrapper to add \"dynamic\" (sets of) fields to an already\ninstantiated WTForms form.\n\nInstallation\n------------\n\nSimply use pip to install:\n\n.. code:: bash\n\n pip install wtforms-dynamic-fields --pre\n\nThe '--pre' flag is necessary until this module has an official release.\n\nThen include it in your project:\n\n.. code::python\n\n from wtforms_dynamic_fields import WTFormsDynamicFields\n\n\nA few notes before using this module\n------------------------------------\n\nIf you simply want to add one field to an already existing form, it may\nbe less overhead to simply use *setattr*:\n\n.. code:: python\n\n setattr(Form, field_name, TextField(field_label, validators=[InputRequired()]))\n\nDoing so will attach a text field, with one validator to the \"Form\"\nobject. This module is intended for slightly more complex scenarios and\nto offer an easier way of configuration.\n\nAlso, this module, in its current state, is developed to scratch a\npersonal itch - simple server side validation of dynamic fields (through\nWTForms itself). It is most likely missing some needed flexibility\nand/or features, so do not hesitate to pinch in or drop me a line!\n\nQuick overview\n--------------\n\nAdding a field\n~~~~~~~~~~~~~~\n\nThe method *add\\_field()* is used to add a field to the modules\nconfiguration.\n\nUsage: add\\_field('machine name', 'label name', WTFormField, \\*args,\n\\*\\*kwargs)\n\nAdding a validator\n~~~~~~~~~~~~~~~~~~\n\nThe method *add\\_validator()* is used to add a validator to an added\nfield configuration.\n\nUsage: add\\_validator('field\\_machine\\_name', WTFormValidator, \\*args,\n\\*\\*kwargs)\n\n- Decorate field machine name arguments with %'s\n (%some\\_field\\_machine\\_name%) to have them automatically suffixed\n with a set number if applicable. More on this below.\n\nApply the configuration to a form\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\nOnce you have setup your configuration using the above methods, you can\napply it to any valid WTForm instance.\n\nUsage: process(ValidFormClass, POST)\n\nNote that *POST* has to be a MultiDict, which is already the case with\nmost frameworks like Flask, Django, ...\n\nBasic usage\n-----------\n\nThe idea behind this module is that you can add \"dynamic\" fields to a\nform that has already been created. \"Dynamic\" here means fields that are\nnot rendered (nor present in the original form object) initially, but\nget injected into the DOM afterwards.\n\nThe module uses the POST variables together with a user defined\nconfiguration to determine which fields are new and are allowed to be\nprocessed.\n\nThe first thing you need, obviously, is a valid WTForms instance to put\nthe new fields on. Say, for example, we have a form that contains a\nfirst name and a last name field, this would be declared as follows:\n\n.. code:: python\n\n from wtforms import Form, TextField\n from wtforms.validators import InputRequired\n\n class PersonalFile(Form):\n \"\"\" A personal file form. \"\"\"\n first_name = TextField('First name, validators=[InputRequired()])\n last_name = TextField('Last name, validators=[InputRequired()])\n\nWhen we present this form to our user, we wish to have the ability to\noptionally add an email address and make it required once added.\n\nIn most cases, to make for a nice user experience, we go ahead an create\na button that has some JavaScript bound to it which will inject the new\nemail input field. Also, because we all like instant feedback glory, we\ncould add some client side validation in our JavaScript to catch\nmistakes early and prevent a round trip to the server.\n\nHowever, we also want this new email address field to be correctly\nvalidated *on the server* and, in case when validation fails, be\nrendered back to the user for inspection. We do not want to write our\nown validation code for this field, but leverage the power of the\nalready present, full-blown WTForms form library to do the heavy\nlifting.\n\nThis is where this module steps in.\n\nFirst you will need an instance of the module:\n\n.. code:: python\n\n dynamic = WTFormsDynamicFields()\n\nNext you will need to build the configuration which will hold the\nallowed, dynamic fields (and their validators). To do this, you use the\n\"add\\_field\" method: define the fields machine name, the label and\nfinally a WTForms field type:\n\n.. code:: python\n\n dynamic.add_field('email', 'Email address', TextField)\n\nOptionally, you can pass \\*args and \\*\\*kwargs to the field as well.\n\nOf course, the machine name of the field needs to correspond with the\ninput's \"name\" attribute as injected by JavaScript. Also notice we do\nnot add any parenthesis after the WTForms field type (TextField).\n\nIf needed, you can also apply optional validators by using the\n\"add\\_validator\" method. You define on which field you wish to apply the\nvalidator and you pass in a WTForms validator:\n\n.. code:: python\n\n dynamic.add_validator('email', InputRequired, message='This field is required')\n\nHere too you have the ability to pass in optional \\*args and \\*\\*kwargs to\nthe validator. Again, no parenthesis after InputRequired, its arguments\nwill be bound by the module later on.\n\nNow that you have added this email field and pushed a validator on it,\nyou are ready to process your form. For the form to be processed, you\nwill need your original form (*PersonalFile* in our case) and the POST\nthat comes back from the server.\n\nNormally, you would bind your form variable directly to the WTForm\ninstance:\n\n.. code:: python\n\n form = PersonalFile()\n\nTo enable this module to process you form, however, you simply need to\nwrap its \"process\" method around it and add the incoming POST:\n\n.. code:: python\n\n form = dynamic.process(PersonalFile, request.post)\n\nNow the form will pick up the optional email field when injected and\nmake the validation fail server side if the field is left empty.\nRemoving the field from the DOM will make your form pass validation\nagain (given that you filled in the first\\_name and last\\_name fields,\nthat is).\n\nUsage with sets\n===============\n\nNow imagine the use case where you which to capture not one, but an\nundefined amount of email address in that same form and have them all\nvalidated correctly.\n\nWith WTForms Dynamic Fields, this is trivial as the module supports sets\n- multiple fields of the same kind. To support these sets in your forms,\nyou only need to uphold a simple naming convention: \"\\_X\" where X is a\nnumber.\n\nIf we would add, say, four email fields, these HTML inputs would look\nlike this:\n\n.. code:: html\n\n \n \n \n \n\nThe fun fact is, you would not have to change anything to the code we\nused in the previous example. The module will derive the canonical name\nof each field (\"email\" in this case) and apply the user defined\nconfiguration for the email field to each individually.\n\nAdvanced usage with sets\n========================\n\nA more complex scenario could occur when you would have a set comprised\nout of two or more fields that are dependent on one another.\n\nFor example, to elaborate on our email scenario from above, imagine we\nwish to also capture a telephone number with each email. But, to up the\nstakes, we only allow one of the two fields to be filled in.\n\nThis would require a dependency between the two fields - a validator\nwhich checks if its field is filled in and the other one is not. Such a\nvalidator would take the *other* field's name as an argument:\n\n::\n\n RequiredIfEmpty('email')\n\nThe above (fictional) validator would be put on the \"telephone\" field to check if\nthe email field was left empty.\n\nNow if you have multiple sets of these fields, each field name will be\nsuffixed with a number, like we have seen before:\n\n.. code:: html\n\n
\n \n \n \n\nSo which field machine name would you have to pass to the validator in\nsuch a use case?\n\nFor this, the WTForms Dynamic Fields module provides the ability to wrap\na field name argument with % signs.\n\n.. code:: python\n\n dynamic.add_field('telephone, 'Telephone number, TextField)\n dynamic.add_validator('telephone, RequiredIfEmpty, '%email%')\n\nThe module detects when it is processing a set of fields (derived from\nthe \"*X\" naming convention) and as such, when wrapping your field name\nwith % signs, will append the correct suffix to the field when binding\nthe arguments to the validator. So if we would be looking at email*\\ 4,\nonce expanded, the above code will translate to:\n\n.. code:: python\n\n telephone_4 = TextField('Telephone number', validators=[RequiredIfEmpty('email_4')])", "description_content_type": null, "docs_url": null, "download_url": "UNKNOWN", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/timusan/wtforms-dynamic-fields", "keywords": null, "license": "BSD", "maintainer": null, "maintainer_email": null, "name": "WTForms-Dynamic-Fields", "package_url": "https://pypi.org/project/WTForms-Dynamic-Fields/", "platform": "any", "project_url": "https://pypi.org/project/WTForms-Dynamic-Fields/", "project_urls": { "Download": "UNKNOWN", "Homepage": "https://github.com/timusan/wtforms-dynamic-fields" }, "release_url": "https://pypi.org/project/WTForms-Dynamic-Fields/0.1a3/", "requires_dist": null, "requires_python": null, "summary": "Simple wrapper to add \"dynamic\" (sets of) fields to an already instantiated WTForms form.", "version": "0.1a3" }, "last_serial": 1187183, "releases": { "0.1a1": [ { "comment_text": "", "digests": { "md5": "fd87d7f9a8b13a3af397767aa1585675", "sha256": "e85cf6ecaf59ede8f7fd51e5ab08c5f4343f428c19d60521ba6be98e34e2cf2b" }, "downloads": -1, "filename": "WTForms-Dynamic-Fields-0.1a1.tar.gz", "has_sig": false, "md5_digest": "fd87d7f9a8b13a3af397767aa1585675", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 9046, "upload_time": "2014-08-04T07:30:54", "url": "https://files.pythonhosted.org/packages/d0/e9/da660473b2cbd6a164a0aaabf1d6c72f130950fd3ee077b6e3f8954ca417/WTForms-Dynamic-Fields-0.1a1.tar.gz" } ], "0.1a2": [ { "comment_text": "", "digests": { "md5": "ecafc00f9938236b0608be1ff2adbc61", "sha256": "0ffb0f780aafba3976190f23aa641c4344b3ce979069b75a7efbea4e72866104" }, "downloads": -1, "filename": "WTForms-Dynamic-Fields-0.1a2.tar.gz", "has_sig": false, "md5_digest": "ecafc00f9938236b0608be1ff2adbc61", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 13533, "upload_time": "2014-08-05T02:43:16", "url": "https://files.pythonhosted.org/packages/66/ce/cdcd369ef7ef288e169100d764577c1fb6e4e2a2f5506ad2e4e8f0031242/WTForms-Dynamic-Fields-0.1a2.tar.gz" } ], "0.1a3": [ { "comment_text": "", "digests": { "md5": "b27d8ded0476539a0524b713a70da293", "sha256": "6e18cf8194018011567d0df6abeb7fd1de5f3bc732b1243a13ba88321bc3844e" }, "downloads": -1, "filename": "WTForms-Dynamic-Fields-0.1a3.tar.gz", "has_sig": false, "md5_digest": "b27d8ded0476539a0524b713a70da293", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 13819, "upload_time": "2014-08-12T02:54:49", "url": "https://files.pythonhosted.org/packages/9f/04/114459415af33966eabe9dc50bdeff5856b6a6eb055c132b568df4d64743/WTForms-Dynamic-Fields-0.1a3.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "b27d8ded0476539a0524b713a70da293", "sha256": "6e18cf8194018011567d0df6abeb7fd1de5f3bc732b1243a13ba88321bc3844e" }, "downloads": -1, "filename": "WTForms-Dynamic-Fields-0.1a3.tar.gz", "has_sig": false, "md5_digest": "b27d8ded0476539a0524b713a70da293", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 13819, "upload_time": "2014-08-12T02:54:49", "url": "https://files.pythonhosted.org/packages/9f/04/114459415af33966eabe9dc50bdeff5856b6a6eb055c132b568df4d64743/WTForms-Dynamic-Fields-0.1a3.tar.gz" } ] }