Select from drop down menu or add another - ruby-on-rails

I'm using simple_form. How can I have a select menu and a text input and have something like Select or add another.
The app will only take one value, either from the select menu or the text input.
It would also be good to validate to have either one or the other but not both, to avoid user confusion.

Implement what is called a 'combobox' to your simple_form
Jquery UI has a combobox:
http://jqueryui.com/autocomplete/#combobox
something fancier:
http://demos.kendoui.com/web/combobox/index.html
This will get you as far as your combo boxes displaying. I don't think there is a plugin for validating, so you'll have to write the code yourself.

You can try to use a combination of JavaScript and Ruby to solve this. If a user wants to enter a different value then what's available in the dropdown, you should have JS listen to a keydown event in that input and clear the dropdown, i.e.:
$(".input_field").bind('keydown', function() {
$(".select_field").val('0') // this should be a default blank value
});
That will clear the select when a user types. Likewise, you want to clear the input when the user selects from the dropdown, right?
$(".select_field").change(function() {
// Let's only clear the input field if the value is not the default null value
if ( $(this).val() != 0 ) {
$(".input_field").val('');
}
});
This will handle the front-end interactions. In the controller, you'll want to do some additional logic:
def create
object.new(...)
if params[:select] and params[:input]
# Overwrite the select with the input value
object.attribute = params[:input]
end
object.save
end
I assume that you want the input value to supersede the select, therefore if they are most submitted, we can just overwrite the select value with the input value and then continue to save the object.
Is this what you're looking for? Not sure if the inputted value was suppose to create a new object with a relationship or not. Hope this helps.

Related

Salesforce URL Hacking

I have an object with many record types, and I need to populate some fields on it whenever it is created.
For example, I have an object called "CustomObj" with a field called "CustomF" with these 2 Record Types "RecType1" and "RecType2".
On the creation of a new "CustomObj" I need to populate the field the "CustomF" by "Hello" when the record type is "RecType1"
and by "Bye" when the record type is "RecType2"
Can I do that using the URL Hacking or I have to create 1 visualforce page to select the record type then redirect to the standard page with the values to populate this field or there is another approach?
What is the best practice?
How can I know the RecordType selected from the url itself ?
Thank you.
You can do this by an Workflow Rule. Go to Setup->Create->Workflow & Approvals. Than u can choose your object on which u want to set up the workflows. Most of the part should be straightforward since all the steps are well documented.
So one rule would be like:
If Record Type == RecType1 than fill in Field XY with value ABC
I see.
This is also possible. Go to the object and than your field you want to fill in the value. Click on edit and use the formula editor.
You can use a rule like
IF( $RecordType.DeveloperName = 'RecType1', 'Value for this', '')
for the default value
Salesforce does not provide an option to override "continue" button on record type selection page. but you can override "new" button. So you can do the following
Override "New" button to move to a record type selection page, which
will be a custom vf page (use radio buttons, description etc.).
The submit button (u can name as "Continue", just to imitate) should redirect to the standard page of data entry. But the
URL will be custom made.
You can refer to this Blog (Saurabh's Salesforce Blog) - http://writeforce.blogspot.in/2012/12/prepopulating-fields-using-url-hacking.html - for the idea of how the URL hacking can be done as per your need. Here you need to identify the field id and use them in the URL to provide a value to be prepopulated.

How to handle default dropdown values using jquery ajax in mvc3?

Based on selection of first dropdown i am binding another drop down values based on first dropdownid value.My question is i given default first dropdown value as "select" but there is no id value for that in second dropdown to get the values.if user selectes "select" in frist dropdown i need to load all values in second dropdown.please tell me how to acheive this?
If you given default first dropdown value as "select" means, at that time disable the second drop down value. If 'country' value equal to 'select' means, at that time disable the 'state drop down'.When we click that country 'select' at that time also call that disable drop down function.
For example HTML view page, country drop down displayed. From that you will get country ID.
$countryID=> From your MVC code you have pass default country ID (default ID)
$countrySelected=$this->getCountryFromName(trim($countryID']));
if ($countrySelected!= ''){
$sVal=$this->getStateList($countrySelected);
$newStateList=array('0'=>'State');
$newStateList+=$this->formatDropdownArray($sVal,'STATE_ID','NAME');
$stateSelected='State';
echo CHtml::dropDownList('STATE_IDD',$stateSelected,$newStateList,array('tabindex'=>9));
}else{
$stateSelected=$defaultState;
echo CHtml::dropDownList('STATE_IDD',$stateSelected,$stateList,array('tabindex'=>9));
}

MVC PlaceHolder for DDL that disappears**

I've seen so many posts and examples of people using a DDL placeholder like this...
#Html.DropDownList("lstUsers",(SelectList)ViewBag.UsersList, "--Select User--")
or
#Html.DropDownListFor(u => u.RoleID, Model.RoleList, "Select", new { #id="hi"})
I mean yea these work, but I want the placeholder to disappear after the ddl index changed, and all these do is place dummy text for the 1st index which can then be selected again.
I haven't been able to find an answer for the life of me. I've tried jquery
$("#tb2").attr("placeholder", "--Please Select--"
which works for a textbox, but not a DropDown. I'm using dynamically generated ddl's
Thanks!
That's not how the select element works. That "placeholder" is actually a full-fledged option in the select list. It's value is usually set to an empty string so that if it's still selected on POST, an empty string is posted and will caused an error if the field is expected to have a value. It doesn't just disappear automatically on it's own.
A textbox is entirely different. When placeholder text is placed inside a textbox, it is literally overwritten by user input, hence why it goes away. In HTML5 textboxes now have an actual placeholder attribute, which will show and hide a bit of text based on the focus of the input. The select element has no equivalent, though.
You could potentially do what you want with some additional JavaScript. You would just watch for a change event on the select and if it has a value (not the "placeholder"), then you could remove the first item from the select (which should be the placeholder):
$('#mySelect').on('change', function () {
if ($(this).val() != '') {
var firstChild = $(this).children().eq(0);
if (firstChild.prop('value') == '') firstChild.remove();
}
});
(I'm using jQuery here because doing the same in straight JavaScript would be much more arduous and since you're using ASP.NET MVC, it's a safe bet you're also using jQuery)

Is there a way I can simply use a data annotation attribute to add a JavaScript attribute to a form field?

I would like to add a data-other-for attribute to a text input, to link it to a select, so that it can be used to capture a value not present in the select when the user selects 'Other' in the select. The attribute's code will determine which value or description is in fact 'Other', and if so, enable the text input and maybe make it mandatory.
It seems like the only way to do this is by creating a new helper, because going via a ValidationAttribute I can only add preset validation HTML attributes to my text input. Or go large and write a whole new metadata provider.
You could try to implement a custom ModelBinder.
Say, in the select you would have:
new SelectListItem(Text = "Other", Value="bind:propertyName", Selected = False);
Then in the overriden BindModel, you simply look for bind: in the model properties and when found, copy your value from there.
After this, you should be able to add normal validation attributes to your select list.

Multiple controls in MVC view that modify same model value

I have a field in my model - call it 'distance' - and I want to write a view that contains both a checkbox and a textbox.
If the checkbox is checked, the textbox becomes disabled and 'distance' gets the value 0 when the form is submitted.
If the checkbox is not checked, 'distance' gets whatever value is in the textbox when the form is submitted.
Anybody have any guidance how to do this? It's the logic of assigning a model value based on the state of both the controls that confusing me...
The UI behaviour can be accomplished with Javascript via an onclick event handler on the checkbox. The only value you need to submit is the input field whose value will be set to 0 or whatever is set manually.
With jQuery you would have:
$('#checkbox_id').click(function() { $('inputfield_id').val('0');
The Model behavior depends on what the checkbox represents. If it's an actual property on the Model, then you will need to check the value server-side as well as (optionally) using aleemb's javascript method.
If a check in that field simply represents a distance of zero, then aleemb's method will suffice.
Aleemb's answer worked like a charm, with a couple of minor modifications:
$("#Distance_checkbox").click(function() {
if ($(this).is(':checked')) {
$("#Distance_textbox").val(0);
$("#Distance_textbox").attr("disabled", true);
}
else {
$("#Distance_textbox").val("");
$("#Distance_textbox").attr("disabled", false);
}
});

Resources