0

I ran into an issue where an input field is missing its name attribute. I have been unable to set the value of this field with the set_values() function, which specifically requires a name to assign a value. Here is the function code:

function (form, ...) 
{
    new_values <- list(...)
    no_match <- setdiff(names(new_values), names(form$fields))
    if (length(no_match) > 0) {
        stop("Unknown field names: ", paste(no_match, collapse = ", "), 
            call. = FALSE)
    }
    for (field in names(new_values)) {
        type <- form$fields[[field]]$type %||% "non-input"
        if (type == "hidden") {
            warning("Setting value of hidden field '", field, 
                "'.", call. = FALSE)
        }
        else if (type == "submit") {
            stop("Can't change value of submit input '", field, 
                "'.", call. = FALSE)
        }
        form$fields[[field]]$value <- new_values[[field]]
    }
    form
}

I would like to override set_values() with a function that accepts either an input name, an id or other css selector.

Any ideas?

Adam
  • 2,137
  • 16
  • 29
  • 1
    When asking for help, you should include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. What code have you tried exactly? – MrFlick Jun 20 '18 at 18:41
  • I added some sample code. Hopefully this helps... – Adam Jun 22 '18 at 19:57
  • 1
    @Adam Your extra code does help, but please add yet a little more. You just say "So far, no go", but what isn't going? Do you get an error message? Does it run but not produce the result you expected? Please describe how this is failing. – G5W Jun 22 '18 at 20:21
  • Okay, please see above. – Adam Jun 22 '18 at 21:10
  • This isn't a reproducible example. We really need to see the HTML form or to see the `dput()` of `form.unfilled`. Does the field actually have an ID? Your "bandaid" is' pretty much the way to do things if you just want to update the second element whatever it is. And it should be posted as an answer rather than an edit to the question. – MrFlick Jun 22 '18 at 23:17

1 Answers1

0

Question: Is it possible to set the value of this field with the set_values() function, without referencing the name attribute?

Answer: No. Look at the source code. Any element with no name cannot change the object returned by the set_values() function.

If the elements in the ... argument do not have names (or the element you're looking for doesn't have a name, set_values() can't do anything. To illustrate using the extreme case, with no names, it will return its form argument unchanged. See the for loop in the source code, for (field in names(new_values)) new_values is a list of the ... arguments. If that list is empty, or if names(...) is empty, the loop exits immediately and the function returns form unchanged.

function (form, ...) 
{
    new_values <- list(...)
    no_match <- setdiff(names(new_values), names(form$fields))
    if (length(no_match) > 0) {
        stop("Unknown field names: ", paste(no_match, collapse = ", "), 
            call. = FALSE)
    }
    for (field in names(new_values)) {
        type <- form$fields[[field]]$type %||% "non-input"
        if (type == "hidden") {
            warning("Setting value of hidden field '", field, 
                "'.", call. = FALSE)
        }
        else if (type == "submit") {
            stop("Can't change value of submit input '", field, 
                "'.", call. = FALSE)
        }
        form$fields[[field]]$value <- new_values[[field]]
    }
    form
}
De Novo
  • 7,120
  • 1
  • 23
  • 39
  • Are you trying to say "no, it's not possible with the `set_values` function"? Otherwise i'm not sure I see an answer to the question here. – MrFlick Jun 20 '18 at 19:06
  • @MrFlick. I clarified with an edit. It's not possible with that function. – De Novo Jun 20 '18 at 19:13
  • OK. But it's certainly possible to write a function that would. But I agree that the current version of `set_values` will not. – MrFlick Jun 20 '18 at 19:17
  • Sure, if you have the data. I just thought I'd answer the answerable question :) – De Novo Jun 20 '18 at 19:19
  • 1
    What about modifying the xml input node to add the name attribute before running set_values()? Any ideas along those lines? Thx – Adam Jun 20 '18 at 20:10
  • @Adam If you post a reproducible example, we can suggest alternative solutions to your problem. Right now since there's nothing to test with and it's not clear what the input data is, it's a lot of work to write up a solution for any possible set of inputs. If you give a clear example, it will be easier to help you. – MrFlick Jun 20 '18 at 21:13
  • @MrFlick How would you override `set_values()` to accept either a name or an id? – Adam Jun 22 '18 at 22:39