TypeError: date.clone is not a function After Added the "name" property to Form.Item (ant design) - datepicker

I am currently building an application with DatePicker from Ant Design and I am wrapping with Form.item.
<Form.Item
name={key}
label={label}
>
<DatePicker
disabledDate={(current) => disableDaysAfterToday(current)}
onChange={(current) => handleChangeDate(current)}
/>
</Form.Item>
However, when the "name" property is added to the Form.Item component I end up getting the following error: TypeError: date.clone is not a function. And, if I remove this property or change to some other string, that is, remove "name" from the Form.Item everything works normally. Can anybody help me?

The "name" property is used to select the correct attribute in the "initialValues" provided in your <Form/> component. If you don't inform it, it doesn't break because the attribute is not being selected, your input must have been empty.
For me, it worked when I treated the attribute in question before passing it to "initialValues".
this.object.date = moment(this.object.date);

Related

Getting all inputs with custom attribute in a view

I need to go through all inputs (of different types) which contain a custom attribute in common, like below:
<m:Input value="{building>/shortName}" custom:required="true"/>
...
<m:Input value="{building>/longName}" custom:required="true"/>
So I can do a validation on each one of them.
Some of you can imagine why I'm doing that (sap.m.Input hasn't a required property itself as sap.ui.commons.TextField has).
By pure jQuery, I could get it, but it's definitely my last option to try.
Does anyone know how to get such filtered list of controls?
Another better solution for the same issue is also welcome.
I think adding minLength constraint with String type and listening validation error would be a better approach.
<Input value="{
path : 'building>/shortName',
type : 'sap.ui.model.type.String',
constraints : {
minLength: 1
}
}" />
sap.ui.getCore().attachValidationError(function (oEvent) {
oEvent.getParameter("element").setValueState("Error");
});
Also you can have a look at this sample
https://openui5.hana.ondemand.com/explored.html#/sample/sap.m.sample.InputChecked/preview

How can I pull data in a controller and form for one model that is in a different model with out a foreign key relationship?

I am new to rails and haven't really done to much with data outside of the model.
I have a form that references a table controller for files. I want to add an dropdown that will display a list of projects from a project table for the user to assign a the file to a project if they want too. Assigning a project is not a requirement. The file can be unassigned to a project. I do have a project_id column in the file table for those projects assigned but it is allowed to be null and I did not build a relationship because I need to have cases where there are none.
Can someone please tell me how to do this?
When they evaluate the file, the screen posts back with a save or update button depending if it has already been saved in the database.
At the same time as the save and update pop up on the right I want to display a list box of the projects with an assign project button. If new just a list, if update either just a list because not assigned or display the selected value in the list if already assigned, while allowing them to change it from the list if desired.
In the file controller method that posts back to the UI I have this code:
#file_alias_filedata = FileAliasFiledata.all
#projects = Project.all
for update
#projects = Project.find(params[:id])
In the form I have this code:
<p> <label> Select Project to Assign:</label> <br />
<%= select_tag 'projects', (#projects.present? ? options_for_select(#projects, #selected_project) : []) %> </p>
The form runs but I get this in the dropdown box:
#<Project:0x))7ff531ab4518>
Can someone please help me figure out how to accomplish my task and why I see the strange box value?
What am I doing wrong?
Thank you for your help!
Assigning a project is not a requirement. The file can be unassigned
to a project. I do have a project_id column in the file table for
those projects assigned but it is allowed to be null
From the docs:
belongs_to(name, scope = nil, options = {}) public
Specifies a
one-to-one association with another class. This method should only be
used if this class contains the foreign key. If the other class
contains the foreign key, then you should use has_one instead. See
also ActiveRecord::Associations::ClassMethods’s overview on when to
use has_one and when to use belongs_to.
Methods will be added for retrieval and query for a single associated
object, for which this object holds an id:
association(force_reload = false)
Returns the associated object. nil is returned if none is found.
Likewise,
has_many(name, scope = nil, options = {}, &extension) public
Specifies
a one-to-many association. The following methods for retrieval and
query of collections of associated objects will be added:
collection(force_reload = false) Returns an array of all the
associated objects. An empty array is returned if none are found.
But belongs_to() and has_many() are supposed to make things more convenient for you. You certainly do not have to use them.
Next,
and why I see the strange box value? What am I doing wrong?
You see the strange value for the same reason the following two loops display different things:
class Dog
attr_reader :name
def initialize(name)
#name = name
end
end
#dogs = [
Dog.new("Sam"),
Dog.new("Betty"),
Dog.new("Pete"),
]
#dogs.each {|dog| puts dog}
#dog_names = #dogs.map {|dog| dog.name }
#dog_names.each {|dog_name| puts dog_name}
--output:--
#<Dog:0x0000010099a308>
#<Dog:0x0000010099a2b8>
#<Dog:0x0000010099a268>
Sam
Betty
Pete
You will see the same result if you do something like the following in a view:
<div>
<%= select_tag "dog", options_for_select(#dogs) %>
</div>
<div>
<%= select_tag "dog_name", options_for_select(#dog_names) %>
</div>
If you read the docs here:
http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/options_for_select
...you will see several examples, which should make it clear that options_for_select() takes an argument that is:
An array of Strings.
A Hash where both the keys and values are Strings.
An enumerable that iterates over some Strings.
etc.
Do you see a pattern? It's Strings! options_for_select() needs an argument that consists of Strings. If the argument is not a collection of Strings, e.g. an array of project objects, then options_for_select() tries to convert the objects to strings by calling to_s() on the objects. And the default to_s() method is Object#to_s() which is inherited by all objects and produces a string containing the class name and the object_id.
I am also new to rails, but I think you can try using the options_from_collection_for_select method.
<%= select_tag :search_state, options_from_collection_for_select(State.find(:all, :select => :name), :name, :name) %>
Hope this help. Cause it certainly helped me.

Angularjs form with "name" attribute set, ignores default values

To validate a form in Angularjs, the "name" attribute needs to be set on the form. When the "name" attribute is set, setting default values in the controller won't work anymore.
Here's an example to demonstrate:
http://plnkr.co/edit/Q59yBZFIGWPMveYuez52?p=preview
On the first form, I have set no "name" attribute, which will set the default value correct, but will not have a validation object when I submit the value.
On the second form, I have set the "name" attribute, which will not set the default value correct, but will have a validation object when I submit the value.
Is this expected behavior or a bug?
The form controller is put in the $scope with the given name. So in your case, the name of the model collides with the name of the form and strange things happen; this is a timebomb, a bug that will haunt you sometime in the future.
I like giving forms a distinct name; if you try <form name="user2form"> in your fiddle, it will work. (I always suffix the form name with form for this reason.)

Struts 2.3 form with multiple submit tags with action attribute

This is something quite simple which worked perfectly with Struts 2.1.x. But we recently upgraded to 2.3.15.2 and it broke. Basically we have a form (actually, many forms) with multiple submits:
<s:form>
<s:submit action="save" />
<s:submit action="resetPassword" />
</s:form>
If I stick the action in the tag all is well. But if instead it is in the tag, I get a 404 error. And it's the same action!
I've been debugging and discovered that when you use the "action" attribute in the tag, the generated html is:
<input type="submit" name="action:save">
<input type="submit" name="action:resetPassword">
Supposedly Struts should take this "action" prefix and say "A-ha! This is an action!" and execute it. And it more or less does this. Or at least tries to. What I've discovered is that at a very low level, the DefaultActionMapper.handleSpecialParameters() method goes through all the parameters and tries to create a ParameterAction for each one, and if it's not null, it is executed. Most of the parameters produce a "null" ParameterAction, but not "action:".
In the docs I found this about ParameterAction:
Defines a parameter action prefix. This is executed when the configured prefix key is
matched in a parameter name, allowing the implementation to manipulate the action mapping
accordingly. For example, if the "action:foo" parameter name was found, and a
ParameterAction implementation was registered to handle the "action" prefix, the execute
method would be called, allowing the implementation to set the "method" value on the
ActionMapping
So what it does is set the mapping's result to a new ServletDispatcherResult with the name of the Action:
mapping.setResult(new ServletDispatcherResult(actionName));
On the other hand, when the action is specified in the s:form tag, the mapping's result is null.
So that when finally we get to Dispatcher.serviceAction():
if (mapping.getResult() != null) {
Result result = mapping.getResult();
result.execute(proxy.getInvocation());
} else {
proxy.execute();
}
So when the action is specified in the tag, proxy.execute() is called, which just calls the Action/method itself. Which is what should happen! But when the action is specified in the tag, since the mapping has a result, the proxy's invocation is passed to result.execute(), which calls ServletDispatcherResult ... and in the end, I get a 404.
This seems like a whole lot of work just to get multiple submit buttons with action attributes working. Is this a known issue for Struts 2.3? Do I need to implement a ParameterAction for the "action" prefix as stated in the docs?
EDIT
Ok, known bug, opened just a few days ago. In the meantime I can either downgrade to 2.3.15.1 or use the "method" attribute rather than the "action" attribute.
Hopefully it will be fixed soon ...
this is b/c in struts2.3.16.
Disables support for action: prefix
by default struts.mapper.action.prefix.enabled = false
set
<constant name="struts.mapper.action.prefix.enabled" value="true"/>
in struts.xml
Internal Changes of struts2-core 2.3.16
The action: and method: prefixes are be by default excluded and changed order to first check excludeParams and then acceptedParams in ParametersInterceptor
This is supposed to be in the process of being fixed for 2.3.15.3.
The specific jira is:
https://issues.apache.org/jira/browse/WW-4204

ValidationMessageFor returns no information when calling modelState.AddModelStateError

I created a class that implements the IModelBinder interface. Within a method of that class I basically retrieve some values and try to validate them. If the validation fails, I add update the model state with necessary information like below:
DateTime mydate;
if (!DateTime.TryParse(convValue,out mydate))
{
bindingContext.ModelState.AddModelError("Date", "Date was crap");
}
The problem is that Html.ValidationMessageFor(m => m.Model) returns no value. I looked at the MVC source code and found out that a proper key with id "Date" can't be found in the ModelState dictionary.
Why is that ? The controller that returns the view have access to the model state and can enumerate over ModelState.Errors
Thanks,
Thomas
Is "Date" the name of the property you are validating?
The first parameter of ModelState.AddModelError should either be the name of the property you want the validation message to display for, or left as string.Empty if you only want the error to display in in the validation summary.
If you want to display an error message that isn't tied to a specific property of your viewmodel, you could call <%: Html.ValidationMessage("Date") %> in your view to display that particular message if it has been set.
Edit: just realised how old this question is. Ah well, might come in handy anyway...