Syphon serializes disabled fields - backbone.js

Looks like Syphon is currently serializing every form field regardless of its state. Is there a way to easily tell Syphon to not serialize disabled fields?

Standard form submit does not include disabled fields and neither does JQuery serialize() method.
Since my primary concern was with disabled checkboxes getting serialized I was able to stop it from serializing those by adding this validator:
Backbone.Syphon.KeyAssignmentValidators.register("checkbox", function ($el, key, value) {
return $el.prop("checked") && $el.is(":enabled");
});
Note: this is a global change and effects all views.

Related

CakePHP 3.x Custom Validation Field Required

As I'm building form elements dynamically I want to be able to check and see if a form field is required or not via a custom validation rule. The problem is that when I add a custom validation rule, it forces the field to not be empty. If I allow the field to be empty, it doesn't check my custom validator unless something is entered in the field.
How can I check in a callback whether to allow or not a field as required?
In my SubmissionsTable
public function validationDefault(Validator $validator)
{
$validator
->add("custom_value_q", [
"custom" => [
"rule" => [$this, "customFieldIsRequired"],
"message" => "Message Here"
]
]
);
return $validator;
}
public function customFieldIsRequired($value, $context)
{
//logic here
return true;
}
Returning true in your custom one when empty $value is passed in should do the trick.
If you want the field to allow empty string (= empty), use allowBlank('custom_value_q') on top, logically you don't need to invoke the custom validator function then, that's why it is bypassed in the empty case.
//UPDATE
You do, however, have the option to provide a callback for allowEmpty(), with this it should be possible to only invoke the custom validation rule if you really want it (if the field needs to be validated because non blank).
$validator->allowEmpty('fieldname', function ($context) {
return !isset($context['data']['description']) || $context['data']['description'] !== '';
});
I know this is a bit old, but I'm facing the same problem, and as I see in github the discussion about it is still open (https://github.com/cakephp/cakephp/issues/8925 and https://github.com/cakephp/cakephp/issues/12484).
In this case, when you have a field that may be empty on some situations (may be if other field was filled), you can do this:
$validator->allowEmptyString('field_a', function ($context) {
// check whether the field can or cannot be empty
return $canBeEmpty;
});
as this may be incorrectly evaluated when an empty form is built (for new entities) as all fields are empty probably, you may have to add the attribute required => false to the form input, if not the field would be marked as required and ask to be filled mandatory.
While having to instruct the form helper whether the field should or shouldn't be required is far from ideal, it's not a big deal, and works to validate entities and modeless forms as well.
Only for validating entities, according to this (https://github.com/cakephp/cakephp/issues/12484#issuecomment-414465002) you may use application rules, which are evaluated only when the entity is being persisted, so a field can be allowed to be empty in validations and then application rules will be applied anyway.

How to always validate an angular json schema form?

I am using angular-schema-form, and ran into a problem that when I load a schema and a form from a server using REST, the validation sometimes did not kick in. I could post a schema even though some fields were required.
How can I always be sure that the required fields in the form has to be filled in by the user before posting?
I found that using $scope.$broadcast('schemaFormValidate'); before submitting the form works (from the docs).
$scope.onSubmit = function(form) {
// First we broadcast an event so all fields validate themselves
$scope.$broadcast('schemaFormValidate');
// Then we check if the form is valid
if (form.$valid) {
// ... do whatever you need to do with your data.
}
}
However, we can not disable any buttons beforehand.
#John you can set a value in your model that is part of a display condition. That allows you to hide the buttons on submit and then re-enable them when you are ready for the user to submit the form again for any reason.

how to setup default ng-model value for mutiple switch-toggle inside ng-repeat

I have used switch-toggle inside ng-repeat. I don't know how to set default value to ng-model when you have multiple switch-toggle in your form and on form submit you need to have all the values. I am very much new to angular world and here is the Example In this example on form load the default value for switch-toggle is shown as "OFF". And if I submit form without making any change to the switch-toggle and check in browser console you can see empty model array. And on making some changes then I get the appropriate values.
So, how can I get all the values of the switch-toggle irrespective I make changes or not. As far as my angularJS knowledge is concern I guess it is related to its model. But how to do it in this case I feel I am lost.
This i believe should be model driven. You should intialize your switchModel, something like this
$scope.switchModel = {1:false,2:true,3:false };
instead of {}

Drupal 7 pass value from javascript to webform hidden field

I have a webform with some normal fields, but also some hidden fields which are set to Secure value (allows use of all tokens), so I am able to use tokens.
How do I pass values from JavaScript into those hidden fields so they are submitted with the form?
I tried using the %post[f1], %post[f2], and %post[f3] tokens, but I still don't know how to add those values with JavaScript.
You can use some basic jQuery for this.
$('input[name=INPUT_NAME]').val('NEW_VALUE');
To fully comply with Drupal theming, you probably want to wrap this up in a Drupal behavior:
(function ($) {
Drupal.behaviors.CUSTOMNAME = {
attach: function(context) {
$('input[name=INPUT_NAME]').val('NEW_VALUE');
}
}
})(jQuery);
...and of course change INPUT_NAME with the name attribute of the hidden input field and CUSTOMNAME with a descriptive camelcase name (e.g ChangeHiddenValuesForm).
As a final note: be sure to include this javascript file on the page of your form.
EDIT:
Sorry, I overlooked the Secure value reference.
Anyway, if you want the value to be secure then you shouldn't be altering it by Javascript as anyone can change it to whatever he or she likes through the DOM... That's why Webform implements the Secure value feature: the value does get submitted along with the form but simply won't be sent to the end user's browser and hereby disabling possible abuse. (For the record: Secure value uses the 'value' type from Drupal's Form API)
If you do want to change such a hidden value, you should opt for Hidden element (less secure, changeable via JavaScript) which already mentions its ability to be changed through Javascript and then use the Drupal behavior described above. Only if you do it like this it gets printed as a hidden input.
If you just want to add some JavaScript then use drupal_add_js(), for example in hook_preprocess_page() in a theme.
function mytheme_preprocess_page(&$vars, $hook) {
drupal_add_js(drupal_get_path('theme', 'mytheme') . '/mytheme.js');
$vars['scripts'] = drupal_get_js();
}

Angular form - send only changed fields

I'm creating a web client that works with a settings web API with angular.
There are a lot of settings and they are all optional. If I send a setting, it should be saved. Settings that are not sent should not change.
The requirement is to have one Save Changes button for all the settings.
I wonder if there is some way in Angular to implement this.
I thought about not using HTML form and collecting the data and creating an ajax request by myself but then I will lose the validation mechanism (that is working well with Angular-UI validate).
I thought about splitting the form into little forms and submiting only the forms where ng-dirty is not false, but this can cause a partial save if some requests will fail (and this is against the requirement).
Any idea?
You can check if the form or any named field is modified before submission. If the form has a name and your inputs have names like:
<form name="myForm">
<input name="input1">
</form>
In the controller you will have access to the object $scope.myForm and $scope.myForm.input1, and these objects will have a $dirty property which is true if the original value was modified by the user.
In the Angular documentation there is an example that covers ng-copy to implement a reset function.
http://docs.angularjs.org/cookbook/advancedform
During submit you could compare your starting model(master copy) to the changed/submitted object (changed copy) and only submit the changed items (or just delete those that are the same/unchanged).
Diff the copy and master with
http://blog.vjeux.com/2011/javascript/object-difference.html
This needs extra work to handle arrays.
Or convert to JSON and diff the JSON
https://github.com/benjamine/JsonDiffPatch

Resources