I'm wondering if anyone can assist me in updating the code detailed here (http://oif.eafarris.com/blog/pre-fill-cck-node-fields-based-on-a-node-re...) for Drupal 7. The function described in that post is identical to what I'm looking to do on my Drupal 7 site but I'm not well versed enough programmatically to do it myself.
I have a content type Event. On the node creation form for Event, I have an autocomplete field for "Client". Below that are additional fields for name, address, etc. The end result I'm hoping to achieve here is:
User enters client name in the autocomplete Client field.
Entered client name matches an existing client and is selected.
Using the node ID of the selected client, the address fields are then populated automatically.
I have a JSON view with a nid argument which spits out the required fields at the url http://domain.com/json-clients/[nid]. But I am unable to get that info returned to the correct fields on the form.
Below is the code as I've got it modified trying to get it to work with D7. Anyone see the glaring errors and care to assist?
(function ($) {
Drupal.behaviors.sponsorhelper = function () {
$("input[name='field_client[und][0][nid]']").blur(function() {
nidRegEx = /\[nid:(\d+)\]/;
SponsorHelper.fill($(this).attr('value').match(nidRegEx)[1]);
})
};
SponsorHelper.fill = function(nid) {
var url = Drupal.settings.basePath + 'json-clients/' + nid;
jQuery.getJSON(url, function (data, result) {
if (result != 'success') {
return;
}
$("input[name='field_address_1[und][0][value]']")
.attr('value',data.nodes[0].node.field_address_1_value);
$("input[name='field_address_2[und][0][value]']")
.attr('value',data.nodes[0].node.field_address_2_value);
})
};
})(jQuery);
Any help is greatly appreciated.
Thanks.
Instead of writing your own javascript try handling this with a couple of drupal's community modules. Check out:
http://drupal.org/project/conditional_fields
http://drupal.org/project/computed_field/
You can us conditional fields to hide the address until the client info is put in. Then use computed fields to search for the client and auto fill the address fields.
Related
I am using word javascript Api for developing a word add-in, I need to insert a content control on button click and send a ajax request. In ajax response i need to update the same content control.
I am trying to use following approaches :
1). While inserting the cc in document set tag as 'temporary' and after getting the ajax response, searching CC using 'contentControls.getByTag', but with multiple content control not able to update the correct cc as ajax response could take time so multiple cc will have 'temporary' tag.
2). After inserting the cc in the document, i tried to load the cc 'ID' using:
var range2 = context.document.getSelection().parentContentControlOrNullObject;
context.load(range2);
But it returns undefine.
Please guide me how i can achieve the above requirement. Which is the correct way to do this or can i use the same range object into another word run and update the cc for that range.
this should be really simple to do. When you insert the content control with the API a content control object is returned. This is effectively a handle to that content control. Once loaded you can later do any operations with it including adding modifying the content. Check out this example on how to do this:
function InsertCCandUpdate() {
Word.run(function(context) {
// we first insert a content control, on this case on the selection!
//notice we'll hold a reference to the CC in the myCC var:
var myCC = context.document.getSelection().insertContentControl();
context.load(myCC);
return context.sync()
.then(function(){
// myCC holds a handle to the contentt control.... then we can update its content
myCC.insertText(getSomeContent(),"replace");
})
});
}
function getSomeContent(){
//this method is just to simulate your AJAX call.
return("some text from your AJAX call");
}
I think this will help you in your scenario.
thanks!
I am sending through a form object to Google Apps Script with:
var formObject = $("#my_form")[0];
google.script.run.processForm(formObject)
The form includes a file input and I have no problem retrieving this and the other input values on the server with something like:
function processForm(formObject) {
var user_name = formObject.userNameInputName;
}
The documentation is clear that GAS can send through a form providing it is the only parameter:
https://developers.google.com/apps-script/guides/html/reference/run#myFunction(...)
My question is:
How do I access a data attribute value of a form input server side?
And to answer this I need to ask a silly question:
What is a "form object"? What is it's structure, what does it "look like" - is it just a JavaScript object or something else? (if I console.log(formObject) client side it just displays the HTML). If I know this I figure I will know how to access the data attribute value correctly.
Edit:
I ended up just adding a hidden input field to the form and setting the val() of it via an on click event before submitting the form, then I could access the value server side with:
function processForm(formObject) {
var user_name = formObject.userNameInputName;
var was_a_data_attribute = formObject.myHiddenInputField;
}
I have a few questions that I couldn't find answers anywhere online.
Does sails.js framework support HTTP PATCH method? If not - does anyone know if there is a planned feature in the future?
By default if I create method in a controller it is accessible with GET request is it the routes.js file where I need to specify that method is accessible only via POST or other type of methods?
How would you create a policy that would allow to change protected fields on entity only for specific rights having users. I.e: user that created entity can change "name", "description" fields but would not be able to change "comments" array unless user is ADMIN?
How would you add a custom header to "find" method which specifies how many items there are in database? I.e.: I have /api/posts/ and I do query for finding specific items {skip: 20; limit: 20} I would like to get response with those items and total count of items that would match query without SKIP and LIMIT modifiers. One thing that comes to my mind is that a policy that adds that that custom header would be a good choice but maybe there is a better one.
Is there any way to write a middle-ware that would be executed just before sending response to the client. I.e.: I just want to filter output JSON not to containt some values or add my own without touching the controller method.
Thank you in advance
I can help with 2 and 5. In my own experience, here is what I have done:
2) I usually just check req.method in the controller. If it's not a method I want to support, I respond with a 404 page. For example:
module.exports = {
myAction: function(req, res){
if (req.method != 'POST')
return res.notFound();
// Desired controller action logic here
}
}
5) I create services in api/services when I want to do this. You define functions in a service that accept callbacks as arguments so that you can then send your response from the controller after the service function finishes executing. You can access any service by the name of the file. For example, if I had MyService.js in api/services, and I needed it to work with the request body, I would add a function to it like this:
exports.myServiceFunction = function(requestBody, callback){
// Work with the request body and data access here to create
// data to give back to the controller
callback(data);
};
Then, I can use this service from the controller like so:
module.exports = {
myAction: function(req, res){
MyService.myServiceFunction(req.body, function(data){
res.json(data);
});
}
}
In your case, the data that the service sends back to the controller through the callback would be the filtered JSON.
I'm sorry I can't answer your other questions, but I hope this helps a bit. I'm still new to Sails.js and am constantly learning new things, so others might have better suggestions. Still, I hope I have answered two of your questions.
I have a Drupal 7 form where after submitting it, some validation happens. It is taking the email address and doing a database look-up to see if that user already exists. If the user exists, I need to alter the form that re-renders on the page that normally displays the errors, removing some fields. Basically on the error page, regardless of any other validation errors they would have normally received (First name required, last name required etc.) they would only get one error message that says "that email address is already in the system" and then I no longer want to display ANY of the other fields at this point except the email address field and a file upload field. So I'm having trouble trying to figure out how to alter the form after the first submission based on some validation.
Thanks
What you want to do is add some data to the $form_state variable in your validation function that can inform your form function as to what fields it should provide.
Untested Example:
function my_form($form, &$form_state){
$form['my_field1'] = array('#markup' => 'my default field');
// look for custom form_state variable
if ($form_state['change_fields']) {
$form['my_field2'] = array('#markup' => 'my new field');
}
}
function my_form_validate($form, &$form_state){
// if not valid for some reason {
form_set_error('my_field1','this did not validate');
$form_state['change_fields'] = true;
// }
}
We have a number of forms on our site that are shown with jquery .dialog and we submit them using an ajax post request.
I've done a fair bit of research and from what I can tell there isn't any definite patterns on how to return validation errors from the server side as a result of an ajax request.
Pretty much the only pattern I could find was to post the form and then do validation server side and in either case return a json object that encapsulated a result and if the result was incorrect the form html
ie.
{result: true}
{success: false, html = "<form>....</form>"}
So in the case the result was false you would need to rewire up any events attached to items on the form as you would be replacing the old form with the new form that has the validation errors.
This seems like an ok approach but you also end up potentially returning a lot more data to the client that you need to when they only really need to validation messages and you are also forced to rewire the form up which is a bit annoying.
I did find one other mention of somehow getting the validation errors and returning them in a json object from the action and somehow telling the client to display them against the correct fields.
Is there any frameworks out there that make this easier or should I write my own or just stick to returning the entire partial for the form and rewiring it when validation is incorrect?
I don't know of any frameworks that handle this particular case–and I don't know that there's a clear best practice–but it's easy enough to serialize validation errors and return them as a JSON object. You could try this extension method on ModelStateDictionary:
public static IEnumerable<ValidationResult> GetValidationResults(this ModelStateDictionary dictionary)
{
foreach (var key in dictionary.Keys)
foreach (var error in dictionary[key].Errors)
if (error != null)
yield return new ValidationResult(error.ErrorMessage, new string[] { key });
}
And in the controller:
if (!ModelState.IsValid)
{
return new JsonResult(ModelState.GetValidationResults());
}
But you're right, you would then have to loop through the object and append the errors to the correct fields. If you have ClientValidationEnabled and UnobtrusiveJavaScriptEnabled set to true, the loop would look something like this:
$.each(errors, function(i, item) {
$('span[data-valmsg-for=' + item.MemberNames[0] + ']').html(item.ErrorMessage);
})
If not, it wouldn't be that difficult to match up the error messages to their respective fields as the object contains the field name. This would definitely save you some data across the wire, but it moves a larger share of the validation responsibility into Javascript. Like I said, I don't know that there's a clear best practice, but I have used this method in the past with success.