Skip to content
Browse files

Added CheckMX support for Email ConstraintAdded getJsFormElementValue…

…(field) Twig ExtentionChanged interface of JS calls. Field object is passed instead of value by default.
  • Loading branch information...
1 parent ccd1220 commit a54fbbff1ad72bcf3126340aff06bba4e564e64c @recipe recipe committed Aug 10, 2012
Showing with 237 additions and 68 deletions.
  1. +13 −0 ChangeLog.md
  2. +2 −1 Contributors.md
  3. +25 −0 Controller/Controller.php
  4. +6 −0 Resources/config/routing/routing.yml
  5. +0 −4 Resources/doc/constraints_warning.md
  6. +2 −1 Resources/views/Constraints/AllValidator.js.twig
  7. +3 −2 Resources/views/Constraints/BlankValidator.js.twig
  8. +2 −1 Resources/views/Constraints/CallbackValidator.js.twig
  9. +2 −1 Resources/views/Constraints/ChoiceValidator.js.twig
  10. +2 −1 Resources/views/Constraints/CollectionValidator.js.twig
  11. +3 −1 Resources/views/Constraints/CountryValidator.js.twig
  12. +3 −1 Resources/views/Constraints/DateTimeValidator.js.twig
  13. +3 −1 Resources/views/Constraints/DateValidator.js.twig
  14. +33 −2 Resources/views/Constraints/EmailValidator.js.twig
  15. +3 −1 Resources/views/Constraints/FalseValidator.js.twig
  16. +3 −1 Resources/views/Constraints/FileValidator.js.twig
  17. +3 −1 Resources/views/Constraints/ImageValidator.js.twig
  18. +4 −2 Resources/views/Constraints/IpValidator.js.twig
  19. +3 −1 Resources/views/Constraints/LanguageValidator.js.twig
  20. +3 −1 Resources/views/Constraints/LocaleValidator.js.twig
  21. +3 −1 Resources/views/Constraints/MaxLengthValidator.js.twig
  22. +4 −2 Resources/views/Constraints/MaxValidator.js.twig
  23. +3 −1 Resources/views/Constraints/MinLengthValidator.js.twig
  24. +4 −2 Resources/views/Constraints/MinValidator.js.twig
  25. +3 −1 Resources/views/Constraints/NotBlankValidator.js.twig
  26. +3 −1 Resources/views/Constraints/NotNullValidator.js.twig
  27. +3 −1 Resources/views/Constraints/NullValidator.js.twig
  28. +3 −1 Resources/views/Constraints/RegexValidator.js.twig
  29. +3 −1 Resources/views/Constraints/RepeatedValidator.js.twig
  30. +3 −1 Resources/views/Constraints/SizeLengthValidator.js.twig
  31. +3 −1 Resources/views/Constraints/SizeValidator.js.twig
  32. +3 −1 Resources/views/Constraints/TimeValidator.js.twig
  33. +3 −1 Resources/views/Constraints/TrueValidator.js.twig
  34. +3 −1 Resources/views/Constraints/TypeValidator.js.twig
  35. +7 −5 Resources/views/Constraints/UniqueEntityValidator.js.twig
  36. +3 −1 Resources/views/Constraints/UrlValidator.js.twig
  37. +0 −3 Resources/views/Frameworks/JsFormValidation.js.dojo.twig
  38. +0 −3 Resources/views/Frameworks/JsFormValidation.js.extjs.twig
  39. +0 −3 Resources/views/Frameworks/JsFormValidation.js.mootools.twig
  40. +0 −3 Resources/views/Frameworks/JsFormValidation.js.prototype.twig
  41. +0 −3 Resources/views/Frameworks/JsFormValidation.js.yui3.twig
  42. +5 −3 Resources/views/JsFormValidation.js.twig
  43. +60 −5 Twig/Extension/JsFormValidationTwigExtension.php
View
13 ChangeLog.md
@@ -1,3 +1,16 @@
+#2012-08-10
+- Added CheckMX support for Email Constraint
+- Added getJsFormElementValue(field) Twig Extention
+- Changed interface of JS calls. Field object is passed instead of value by default.
+
+#2012-08-06
+- Unique Entity support has been implemented (jquery framework only)
+- Implemented validation of the constraints which are based on method of the entity clas
+
+#2012-08-04
+- Version 2.1 has been mastered.
+- Fix issue #8 Symfony 2.1 compability
+
#2011-12-05
- Dispatch events before and after processing constraints of a form
- Manage validation groups with an event listener
View
3 Contributors.md
@@ -1,3 +1,4 @@
Abhoryo (Abhoryo)
guillaumepotier (Guillaume Potier)
-mrtorrent (Miquel Rodríguez Telep)
+mrtorrent (Miquel Rodríguez Telep)
+recipe (Vitaliy Demidov)
View
25 Controller/Controller.php
@@ -88,6 +88,31 @@ public function uniqueEntityAction()
}
/**
+ * Check DNS records corresponding to a given Internet host name or IP address
+ *
+ * @return Response Returns json response
+ * @author Vitaliy Demidov <zend@i.ua>
+ * @since 10 Aug 2012
+ */
+ public function checkMxAction()
+ {
+ $a = new \stdClass();
+ $request = $this->container->get('request');
+ $address = $request->get('address');
+ $type = $request->get('type') ?: "MX";
+ if (empty($address)) {
+ return $this->getAjaxResponse($a, 'Invalid arguments.');
+ }
+ try {
+ $a->type = $type;
+ $a->result = \checkdnsrr($address, $type);
+ } catch (\Exception $e) {
+ return $this->getAjaxResponse($a, 'Failure');
+ }
+ return $this->getAjaxResponse($a);
+ }
+
+ /**
* Verifies if requested field has UniqueEntity constraint for desired entity.
*
* @param string $entityClass Entity class name
View
6 Resources/config/routing/routing.yml
@@ -3,3 +3,9 @@ apy_js_form_validation_unique_entity:
defaults: { _controller: "jsfv.controller:uniqueEntityAction", _format: "json" }
requirements:
_format: json
+
+apy_js_form_validation_checkmx:
+ pattern: /jsfv/checkmx.{_format}
+ defaults: { _controller: "jsfv.controller:checkMxAction", _format: "json" }
+ requirements:
+ _format: json
View
4 Resources/doc/constraints_warning.md
@@ -1,9 +1,6 @@
# Repeated field
On blur mode, validation isn't perfomed when you change the first repeated field after the second.
-# Email
-checkMX is not perfomed
-
# Url
JsFormValidation constraint checks for real IPv4
@@ -18,7 +15,6 @@ Not implemented yet
# UniqueEntity
It has not been implemented for multiple-column yet.
-Implemented only for the case when single field is associated with UniqueEntity constraint.
# All
Not implemented yet
View
3 Resources/views/Constraints/AllValidator.js.twig
@@ -1,4 +1,5 @@
-function All(value, params)
+function All(field, params)
{
+ var value = field && field.nodeName ? {{ getJsFormElementValue('field') }} : field;
return true;
}
View
5 Resources/views/Constraints/BlankValidator.js.twig
@@ -1,7 +1,8 @@
-function Blank(value, params)
+function Blank(field, params)
{
+ var value = field && field.nodeName ? {{ getJsFormElementValue('field') }} : field;
if (isNotDefined(value)) {
- return true;
+ return true;
}
return getComputeMessage(params.message);
View
3 Resources/views/Constraints/CallbackValidator.js.twig
@@ -1,4 +1,5 @@
-function Callback(value, params)
+function Callback(field, params)
{
+ var value = field && field.nodeName ? {{ getJsFormElementValue('field') }} : field;
return true;
}
View
3 Resources/views/Constraints/ChoiceValidator.js.twig
@@ -1,4 +1,5 @@
-function Choice(value, params)
+function Choice(field, params)
{
+ var value = field && field.nodeName ? {{ getJsFormElementValue('field') }} : field;
return true;
}
View
3 Resources/views/Constraints/CollectionValidator.js.twig
@@ -1,4 +1,5 @@
-function Collection(value, params)
+function Collection(field, params)
{
+ var value = field && field.nodeName ? {{ getJsFormElementValue('field') }} : field;
return true;
}
View
4 Resources/views/Constraints/CountryValidator.js.twig
@@ -1,5 +1,7 @@
-function Country(value, params)
+function Country(field, params)
{
+ var value = field && field.nodeName ? {{ getJsFormElementValue('field') }} : field;
+
if (isNotDefined(value)) {
return true;
}
View
4 Resources/views/Constraints/DateTimeValidator.js.twig
@@ -1,5 +1,7 @@
-function DateTime(value, params)
+function DateTime(field, params)
{
+ var value = field && field.nodeName ? {{ getJsFormElementValue('field') }} : field;
+
if (isNotDefined(value)) {
return true;
}
View
4 Resources/views/Constraints/DateValidator.js.twig
@@ -1,5 +1,7 @@
-function Date(value, params)
+function Date(field, params)
{
+ var value = field && field.nodeName ? {{ getJsFormElementValue('field') }} : field;
+
if (isNotDefined(value)) {
return true;
}
View
35 Resources/views/Constraints/EmailValidator.js.twig
@@ -1,14 +1,45 @@
-function Email(value, params)
+function Email(field, params)
{
+ var value = field && field.nodeName ? {{ getJsFormElementValue('field') }} : field;
+
if (isNotDefined(value)) {
return true;
}
value = String(value);
-
var pattern = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})$/;
if ( pattern.test(value) ) {
+ if (params && params.checkMX && field.nodeName) {
+ //TODO This part is compartible only with jquery
+ var cache, cacheindex = 'm' + value + '';
+ cache = $(field).data('_checkMXCache');
+ if (!cache) cache = {};
+ if (cache[cacheindex] != undefined) {
+ if (cache[cacheindex] === false) {
+ return getComputeMessage(params.message);
+ } else return true;
+ }
+ {% block checkmx_async_request -%}
+ $.ajax({
+ type: 'POST',
+ url: '{{ path('apy_js_form_validation_checkmx', {'_format': 'json'}) }}',
+ data: {address: value.replace(new RegExp("^.*@"), "")},
+ success: function(data, textStatus) {
+ {% block checkmx_handler -%}
+ if (data.status && data.status == 'ok') {
+ cache[cacheindex] = data.result;
+ $(field).data('_checkMXCache', cache);
+ if (!cache[cacheindex]) {
+ checkError($(field).attr('id'), Email, params);
+ }
+ }
+ {% endblock -%}
+ },
+ dataType: 'json'
+ });
+ {% endblock -%}
+ }
return true;
}
View
4 Resources/views/Constraints/FalseValidator.js.twig
@@ -1,5 +1,7 @@
-function False(value, params)
+function False(field, params)
{
+ var value = field && field.nodeName ? {{ getJsFormElementValue('field') }} : field;
+
if (isNotDefined(value)) {
return true;
}
View
4 Resources/views/Constraints/FileValidator.js.twig
@@ -1,4 +1,6 @@
-function File(value, params)
+function File(field, params)
{
+ var value = field && field.nodeName ? {{ getJsFormElementValue('field') }} : field;
+
return true;
}
View
4 Resources/views/Constraints/ImageValidator.js.twig
@@ -1,4 +1,6 @@
-function Image(value, params)
+function Image(field, params)
{
+ var value = field && field.nodeName ? {{ getJsFormElementValue('field') }} : field;
+
return true;
}
View
6 Resources/views/Constraints/IpValidator.js.twig
@@ -1,13 +1,15 @@
-function Ip(value, params)
+function Ip(field, params)
{
+ var value = field && field.nodeName ? {{ getJsFormElementValue('field') }} : field;
+
if (isNotDefined(value)) {
return true;
}
/*
* Private functions
* Inspired by php_filter_validate_ip function
- * Copyright (c) 1997-2011 The PHP Group
+ * Copyright (c) 1997-2011 The PHP Group
*/
var isIPv4 = function (value) {
var ip = value.match(/^(\d+)\.(\d+)\.(\d+)\.(\d+)$/);
View
4 Resources/views/Constraints/LanguageValidator.js.twig
@@ -1,5 +1,7 @@
-function Language(value, params)
+function Language(field, params)
{
+ var value = field && field.nodeName ? {{ getJsFormElementValue('field') }} : field;
+
if (isNotDefined(value)) {
return true;
}
View
4 Resources/views/Constraints/LocaleValidator.js.twig
@@ -1,5 +1,7 @@
-function Locale(value, params)
+function Locale(field, params)
{
+ var value = field && field.nodeName ? {{ getJsFormElementValue('field') }} : field;
+
if (isNotDefined(value)) {
return true;
}
View
4 Resources/views/Constraints/MaxLengthValidator.js.twig
@@ -1,5 +1,7 @@
-function MaxLength(value, params)
+function MaxLength(field, params)
{
+ var value = field && field.nodeName ? {{ getJsFormElementValue('field') }} : field;
+
if (isNotDefined(value)) {
return true;
}
View
6 Resources/views/Constraints/MaxValidator.js.twig
@@ -1,11 +1,13 @@
-function Max(value, params)
+function Max(field, params)
{
+ var value = field && field.nodeName ? {{ getJsFormElementValue('field') }} : field;
+
if (isNotDefined(value)) {
return true;
}
if (!((typeof(value) === 'number' || typeof(value) === 'string') && !isNaN(value))) {
- return getComputeMessage(params.invalidMessage, { 'limit' : params.limit } );
+ return getComputeMessage(params.invalidMessage, { 'limit' : params.limit } );
}
if (parseInt(value) > parseInt(params.limit)) {
View
4 Resources/views/Constraints/MinLengthValidator.js.twig
@@ -1,5 +1,7 @@
-function MinLength(value, params)
+function MinLength(field, params)
{
+ var value = field && field.nodeName ? {{ getJsFormElementValue('field') }} : field;
+
if (isNotDefined(value)) {
return true;
}
View
6 Resources/views/Constraints/MinValidator.js.twig
@@ -1,11 +1,13 @@
-function Min(value, params)
+function Min(field, params)
{
+ var value = field && field.nodeName ? {{ getJsFormElementValue('field') }} : field;
+
if (isNotDefined(value)) {
return true;
}
if (!((typeof(value) === 'number' || typeof(value) === 'string') && !isNaN(value))) {
- return getComputeMessage(params.invalidMessage, { 'limit' : params.limit } );
+ return getComputeMessage(params.invalidMessage, { 'limit' : params.limit } );
}
if (parseInt(value) < parseInt(params.limit)) {
View
4 Resources/views/Constraints/NotBlankValidator.js.twig
@@ -1,5 +1,7 @@
-function NotBlank(value, params)
+function NotBlank(field, params)
{
+ var value = field && field.nodeName ? {{ getJsFormElementValue('field') }} : field;
+
if (isNotDefined(value)) {
return getComputeMessage(params.message);
}
View
4 Resources/views/Constraints/NotNullValidator.js.twig
@@ -1,5 +1,7 @@
-function NotNull(value, params)
+function NotNull(field, params)
{
+ var value = field && field.nodeName ? {{ getJsFormElementValue('field') }} : field;
+
if (typeof(value) == 'undefined' || value === null) {
return getComputeMessage(params.message);
}
View
4 Resources/views/Constraints/NullValidator.js.twig
@@ -1,5 +1,7 @@
-function Null(value, params)
+function Null(field, params)
{
+ var value = field && field.nodeName ? {{ getJsFormElementValue('field') }} : field;
+
if (typeof(value) == 'undefined' || value === null) {
return true;
}
View
4 Resources/views/Constraints/RegexValidator.js.twig
@@ -1,5 +1,7 @@
-function Regex(value, params)
+function Regex(field, params)
{
+ var value = field && field.nodeName ? {{ getJsFormElementValue('field') }} : field;
+
if (isNotDefined(value)) {
return true;
}
View
4 Resources/views/Constraints/RepeatedValidator.js.twig
@@ -1,5 +1,7 @@
-function Repeated(value, params)
+function Repeated(field, params)
{
+ var value = field && field.nodeName ? {{ getJsFormElementValue('field') }} : field;
+
first = document.getElementById(params.first_name).value;
second = document.getElementById(params.second_name).value;
View
4 Resources/views/Constraints/SizeLengthValidator.js.twig
@@ -1,5 +1,7 @@
-function SizeLength(value, params)
+function SizeLength(field, params)
{
+ var value = field && field.nodeName ? {{ getJsFormElementValue('field') }} : field;
+
if (isNotDefined(value)) {
return true;
}
View
4 Resources/views/Constraints/SizeValidator.js.twig
@@ -1,5 +1,7 @@
-function Size(value, params)
+function Size(field, params)
{
+ var value = field && field.nodeName ? {{ getJsFormElementValue('field') }} : field;
+
if (isNotDefined(value)) {
return true;
}
View
4 Resources/views/Constraints/TimeValidator.js.twig
@@ -1,5 +1,7 @@
-function Time(value, params)
+function Time(field, params)
{
+ var value = field && field.nodeName ? {{ getJsFormElementValue('field') }} : field;
+
if (isNotDefined(value)) {
return true;
}
View
4 Resources/views/Constraints/TrueValidator.js.twig
@@ -1,5 +1,7 @@
-function True(value, params)
+function True(field, params)
{
+ var value = field && field.nodeName ? {{ getJsFormElementValue('field') }} : field;
+
if (isNotDefined(value)) {
return true;
}
View
4 Resources/views/Constraints/TypeValidator.js.twig
@@ -1,4 +1,6 @@
-function Type(value, params)
+function Type(field, params)
{
+ var value = field && field.nodeName ? {{ getJsFormElementValue('field') }} : field;
+
return true;
}
View
12 Resources/views/Constraints/UniqueEntityValidator.js.twig
@@ -3,9 +3,11 @@
*/
function UniqueEntity(field, params)
{
- var value = field.val(), cache, url, cacheindex = 'm' + value + '';
- if (params && params.fields) {
- cache = field.data('_uniqueEntityCache');
+ var value = field && field.nodeName ? {{ getJsFormElementValue('field') }} : field;
+ var cache, url, cacheindex = 'm' + value + '';
+ if (params && params.fields && field.nodeName) {
+ //TODO This part is compartible only with jquery
+ cache = $(field).data('_uniqueEntityCache');
if (!cache) cache = {};
if (cache[cacheindex] != undefined) {
if (cache[cacheindex] === false) {
@@ -26,9 +28,9 @@ function UniqueEntity(field, params)
{% block check_uniquity_handler %}
if (data.status && data.status == 'ok') {
cache[cacheindex] = data.isUnique;
- field.data('_uniqueEntityCache', cache);
+ $(field).data('_uniqueEntityCache', cache);
if (!cache[cacheindex]) {
- checkError(field.attr('id'), UniqueEntity, params);
+ checkError($(field).attr('id'), UniqueEntity, params);
}
}
{% endblock %}
View
4 Resources/views/Constraints/UrlValidator.js.twig
@@ -1,5 +1,7 @@
-function Url(value, params)
+function Url(field, params)
{
+ var value = field && field.nodeName ? {{ getJsFormElementValue('field') }} : field;
+
if (isNotDefined(value)) {
return true;
}
View
3 Resources/views/Frameworks/JsFormValidation.js.dojo.twig
@@ -20,9 +20,6 @@ if (field.siblings('ul.error_list').length == 0) {
field.prev('ul').append('<li>'+errorMessage+'</li>');
{% endblock %}
-
-{% block getVal %}dojo.query(field).val(){% endblock %}
-
{% block onEvent %}
dojo.connect(field, "on"+eventType, handler);
{% endblock %}
View
3 Resources/views/Frameworks/JsFormValidation.js.extjs.twig
@@ -15,9 +15,6 @@ if (field.parent().query('.error_list').length == 0) {
Ext.DomHelper.append(field.prev('ul'), '<li>'+errorMessage+'</li>');
{% endblock %}
-
-{% block getVal %}Ext.get(field).getValue(){% endblock %}
-
{% block onEvent %}
Ext.get(field).on(eventType, handler);
{% endblock %}
View
3 Resources/views/Frameworks/JsFormValidation.js.mootools.twig
@@ -17,9 +17,6 @@ if (field.getSiblings('.error_list').length == 0) {
field.getPrevious('ul').adopt(new Element('li', { html : errorMessage }));
{% endblock %}
-
-{% block getVal %}(field.nodeName.toLowerCase() == 'select') ? $(field).getSelected()[0].value : $(field).get('value'){% endblock %}
-
{% block onEvent %}
$(field).addEvent(eventType, handler);
{% endblock %}
View
3 Resources/views/Frameworks/JsFormValidation.js.prototype.twig
@@ -15,9 +15,6 @@ if (field.adjacent('ul.error_list').length == 0) {
field.previous('ul').insert('<li>'+errorMessage+'</li>');
{% endblock %}
-
-{% block getVal %}$(field).getValue(){% endblock %}
-
{% block onEvent %}
$(field).observe(eventType, handler);
{% endblock %}
View
3 Resources/views/Frameworks/JsFormValidation.js.yui3.twig
@@ -19,9 +19,6 @@ if (field.ancestor().all('.error_list').size() == 0) {
field.previous('ul').append('<li>'+errorMessage+'</li>');
{% endblock %}
-
-{% block getVal %}Y.one(field).get('value'){% endblock %}
-
{% block onEvent %}
Y.one(field).on(eventType, handler);
{% endblock %}
View
8 Resources/views/JsFormValidation.js.twig
@@ -26,13 +26,15 @@ var jsfv = new function () {
}
function checkError(field, checkFunction, parameters, value) {
- var match = checkFunction.toString().substr(9).indexOf('UniqueEntity') === 0;
field = jsfv.id(field);
// Remove old errors of the field
{% block removeErrors %}{% spaceless %}jsfv.removeErrors(field);{% endspaceless %}{% endblock %}
-
// Check the value
- errorMessage = checkFunction({% block getVal %}(match ? $(field) : (value === undefined ? $(field).val() : value) ){% endblock %}, parameters);
+ errorMessage = checkFunction((value === undefined ? field : value), parameters);
+ {# Notice! Block getVal was refused since 2.1 version.
+ Use twig extention function getJsFormElementValue('field') instead. -#}
+ /*// {% block getVal %}{% endblock %} */
+
if (errorMessage != true) {
{% block addError %}
{% spaceless %}jsfv.addError(field, errorMessage);{% endspaceless %}
View
65 Twig/Extension/JsFormValidationTwigExtension.php
@@ -32,12 +32,21 @@ public function getFunctions()
{
return array(
'JSFV' => new \Twig_Function_Method($this, 'JsFormValidationFunction', array('is_safe' => array('all'))),
+ 'getJsFormElementValue' => new \Twig_Function_Method($this, 'getJsFormElementValue', array('is_safe' => array('all'))),
'getCountries' => new \Twig_Function_Method($this, 'getCountries'),
'getLanguages' => new \Twig_Function_Method($this, 'getLanguages'),
'getLocales' => new \Twig_Function_Method($this, 'getLocales'),
);
}
+ /**
+ * Retrieves validation javascript.
+ *
+ * @param FormView $formView A Form View.
+ * @param string $getScriptPath Whether it should return script url instead of script tag (default: false).
+ * @return string Returns script tag or script url depending on getScriptPath option.
+ * @throws \RuntimeException
+ */
public function JsFormValidationFunction(FormView $formView, $getScriptPath = false)
{
$enabled = $this->container->getParameter('apy_js_form_validation.enabled');
@@ -49,12 +58,10 @@ public function JsFormValidationFunction(FormView $formView, $getScriptPath = fa
if ($getScriptPath) {
return $scriptFile;
- }
- else {
+ } else {
return sprintf('<script type="text/javascript" src="%s"></script>', $scriptFile);
}
- }
- else {
+ } else {
// If the bundle is disabled and $getScriptPath is set to true an empty script is generated
if ($getScriptPath) {
$asseticPath = $this->container->getParameter('assetic.write_to');
@@ -64,7 +71,7 @@ public function JsFormValidationFunction(FormView $formView, $getScriptPath = fa
$this->container->get('filesystem')->mkdir($scriptRealPath);
- $filePath = $scriptRealPath.$scriptFile;
+ $filePath = $scriptRealPath.$scriptFile;
if (false === file_exists($filePath)) {
if (false === @file_put_contents($filePath, '// JsFormValidation bundle is disabled')) {
throw new \RuntimeException('Unable to write file '.$filePath);
@@ -76,6 +83,54 @@ public function JsFormValidationFunction(FormView $formView, $getScriptPath = fa
}
}
+ /**
+ * Returns JS language construction which acquire form field value.
+ * This function generates code taking into account javascript_framework option
+ * of the bundle.
+ *
+ * @param string $formElementObjectName Variable name that represents form element object.
+ * @param string $framework Optional. Used to override default framework.
+ * @return string Returns javascript code that returns value of the
+ */
+ public function getJsFormElementValue($formElementObjectName, $framework = null)
+ {
@Abhoryo
Owner
Abhoryo added a note Aug 10, 2012

What you trying to do by exteralized this function ?

@recipe
Collaborator
recipe added a note Aug 10, 2012

This workaround should be available everywhere in core javascript template as well as in constraint templates in order we want to achieve cross-framework code. I have changed implementation of the interface of constraints, so that demands to pass form Element object instead of its value for all functions. Inside these functions we need to evaluate field value.
I considered two variants:

If we used macro we'd additionaly pass javascript_framework variable. Yet we could access it from twig Environment. I decided to use twig extesion.

@Abhoryo
Owner
Abhoryo added a note Aug 13, 2012

Ok

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ $ret = '';
+ $f = $formElementObjectName;
+ $framework = $framework ?: $this->container->getParameter('apy_js_form_validation.javascript_framework');
+ switch ($framework) {
+ case 'dojo':
+ $ret = 'dojo.query(' . $f . ').val()';
+ break;
+
+ case 'extjs':
+ $ret = 'Ext.get(' . $f . ').getValue()';
+ break;
+
+ case 'jquery':
+ case 'zepto':
+ $ret = '$(' . $f . ').val()';
+ break;
+
+ case 'mootools':
+ $ret = '((' . $f . '.nodeName.toLowerCase() == "select") ? '
+ . '$(' . $f . ').getSelected()[0].value : '
+ . '$(' . $f . ').get("value"))';
+ break;
+
+ case 'prototype':
+ $ret = '$(' . $f . ').getValue()';
+ break;
+
+ case 'yui3':
+ $ret = 'Y.one(' . $f . ').get("value")';
+ break;
+
+ default:
+ throw new \RuntimeException("Unknown framework!");
+ }
+ return $ret;
+ }
+
public function getCountries()
{
return json_encode(\Symfony\Component\Locale\Locale::getCountries());

0 comments on commit a54fbbf

Please sign in to comment.
Something went wrong with that request. Please try again.