PDA

View Full Version : Help! I have a deadline



kordsmen
22 Jul 2007, 6:17 AM
I have a registration form, when a registrant chooses "no" in a yes/no combo box or a yes/no radio button, I need to require a textfield to be filled in. I have searched the forums and can't find any examples of this functionality. I would like to have the field hidden untill It is required but just required if the other field is negative is good enough.

oshcha
22 Jul 2007, 10:44 AM
I have a registration form, when a registrant chooses "no" in a yes/no combo box or a yes/no radio button, I need to require a textfield to be filled in. I have searched the forums and can't find any examples of this functionality. I would like to have the field hidden untill It is required but just required if the other field is negative is good enough.

Hi,

I am just a newbie with Ext Js. But using simple JS it could be done like this I guess:

1) add onChange action to your checkbox. Name it like myCheckAction or whatever you like

2) in that function check if checkbox is checked then Ext.get('yourHiddenInputField') and make it visible else make it hidden

3) when submiting form add validation that check if checkbox is checked then field shoud be mandatory.


I hope this helps.

dj
22 Jul 2007, 10:48 AM
add a change listener to your checkbox and connect both fields via an id attribute:


new Ext.form.Checkbox({
fieldLabel: 'Checkbox',
name: 'checkbox',
linkedField:"idOfLinkedField",
listeners:{
change:function(field, value){
var linkedField = Ext.ComponentMgr.get(field.linkedField);
linkedField.allowBlank = value===true||value=="true";
linkedField.clearInvalid();
}
}
})
...
new Ext.form.TextField({
fieldLabel: 'OptionalField',
name: 'optional',
id:"idOfLinkedField"
})



I haven't tested the code above. I have used something slightly different in my code.


Dynamically hide or show a field is - as far as I know - not possible in Ext. But it can be easily added:


Ext.apply(Ext.form.Form.prototype, {
renameFieldLabel:function(field, label){
if (typeof field == "string"){
field = this.findField(field);
}
var wrap = field.el.findParent(".x-form-item");
Ext.fly(wrap).child('label').update(label);
},
setFieldVisibility: function(field, visibility){
if (typeof field == "string"){
field = this.findField(field);
}
var wrap = field.el.findParent(".x-form-item");
Ext.fly(wrap).setDisplayed(visibility);
}
});

(in Ext 2.0 replace Ext.form.Form with Ext.FormPanel)




PS: a better thread title would probably attract much more people that can actually help you and not only people that want to see a post of a possibly desperate person ;)

kordsmen
22 Jul 2007, 3:05 PM
PS: a better thread title would probably attract much more people that can actually help you and not only people that want to see a post of a possibly desperate person ;)

Thank you for the advice but I ask the same/a similar question a weak ago with the title "Dependent form Field" and got no responses. What should I have called it?

kordsmen
22 Jul 2007, 3:23 PM
add a change listener to your checkbox and connect both fields via an id attribute:


Thank you for your help? :D

where is the reference to linkedField it isn't part of checkbox or form as far as I can see?

dj
22 Jul 2007, 6:44 PM
Thank you for the advice but I ask the same/a similar question a weak ago with the title "Dependent form Field" and got no responses. What should I have called it?

Don't know, IMHO that was a pretty good subject. I must have missed that topic otherwise I would have answered that.


Thank you for your help? :D

where is the reference to linkedField it isn't part of checkbox or form as far as I can see?

You are right, it isn't part of any Ext component. But anything that you put in the config-object in the constructor will be applied to the object. So it's the same as writing


var f = new Ext.form.Checkbox({
fieldLabel: 'Checkbox',
name: 'checkbox',
listeners:{
change:function(field, value){
var linkedField = Ext.ComponentMgr.get(field.linkedField);
linkedField.allowBlank = value===true||value=="true";
linkedField.clearInvalid();
}
}
});
f.linkedField = "idOfLinkedField";


The call to Ext.ComponentMgr.get converts the string-id into the real Ext.form.TextField object.


If all that linkField stuff confuses you, a normal - Web 1.0 ;) - way would be to directly address the TextField via a variable in the change listener:


var c,t;
c = new Ext.form.Checkbox({
fieldLabel: 'Checkbox',
name: 'checkbox',
listeners:{
change:function(field, value){
t.allowBlank = value===true||value=="true";
t.clearInvalid();
}
}
});
...
t = new Ext.form.TextField({
fieldLabel: 'OptionalField',
name: 'optional'
}) ;

kordsmen
24 Jul 2007, 3:33 PM
var f = new Ext.form.Checkbox({
fieldLabel: 'Checkbox',
name: 'checkbox',
listeners:{
change:function(field, value){
var linkedField = Ext.ComponentMgr.get(field.linkedField);
linkedField.allowBlank = value===true||value=="true";
linkedField.clearInvalid();
}
}
});
f.linkedField = "idOfLinkedField";




Can anyone show me an example of a using a combo box instead of a check box? I can't think of the proper logic to only require the text field to be required when the anser is negitive.

IE:
do you live in the usa? if the answer is no file in a textfield with the country you live in.

dj
25 Jul 2007, 2:36 AM
IE:
do you live in the usa? if the answer is no file in a textfield with the country you live in.


I don't understand. What should be a TextField, what a Combo, what a Checkbox?

kordsmen
25 Jul 2007, 7:38 AM
I don't understand. What should be a TextField, what a Combo, what a Checkbox?

I am sorry I copied the wrong portion of code.

Here is the original I would like to change from using a checkbox to using a combo box.



new Ext.form.Checkbox({
fieldLabel: 'Checkbox',
name: 'checkbox',
linkedField:"idOfLinkedField",
listeners:{
change:function(field, value){
var linkedField = Ext.ComponentMgr.get(field.linkedField);
linkedField.allowBlank = value===true||value=="true";
linkedField.clearInvalid();
}
}
})
...
new Ext.form.TextField({
fieldLabel: 'OptionalField',
name: 'optional',
id:"idOfLinkedField"
})



can someone show me/lead me in the right direction on how to make the OptionalField required like in the code above using a combo box in stead of the checkbox?

Here is my combo box


new Ext.formComboBox({
fieldLabel: 'Does your organization operat in USA?',
labeSeparator: '',
hiddenName: 'in_usa',
store: new Ext.dataSimleStor({
fields: ['id', 'yesno'],
data: Ext.combodata.yes_no
}),
displayField: 'yesno",
valueField: 'id',
typeahead: true,
mode: 'local',
triggerAction: 'all',
selectOnFocus: true,
emptyText: 'Make a Selection...',
width: 190,
allowBlank: false,
editable: false,
linkedField: 'idOfLinkedField',
listeners:{
change:function(field, value){
if (field.value == "1") {
var linkedField = Ext.ComponentMgr.get(field.linkedField);
linkedField.allowBlank = value===true||value=="true";
linkedField.clearInvalid();
} else {
return;
}
}
}),

new Ext.form.TextField({
fieldLabel: 'OptionalField',
name: 'optional',
id:"idOfLinkedField"
})

Overlord
25 Jul 2007, 8:02 AM
new Ext.formCoboBox({


Well, for one, you have many typos. Change the above to new "Ext.form.ComboBox".

dj
25 Jul 2007, 3:34 PM
new Ext.form.ComboBox({
fieldLabel: 'Does your organization operate in USA?',
labeSeparator: '',
hiddenName: 'in_usa',
store: new Ext.data.SimpleStore({
fields: ['id', 'yesno'],
data: Ext.combodata.yes_no
}),
displayField: 'yesno",
valueField: 'id',
typeahead: true,
mode: 'local',
triggerAction: 'all',
selectOnFocus: true,
emptyText: 'Make a Selection...',
width: 190,
allowBlank: false,
editable: false,
linkedField: 'idOfLinkedField',
listeners:{
change:function(field, value){
var linkedField = Ext.ComponentMgr.get(field.linkedField);
linkedField.allowBlank = field.getValue() == "1";
linkedField.clearInvalid();
}
}),

new Ext.form.TextField({
fieldLabel: 'OptionalField',
name: 'optional',
id:"idOfLinkedField"
})


maybe so.

kordsmen
26 Jul 2007, 9:18 AM
listeners:{
change:function(field, value){
var linkedField = Ext.ComponentMgr.get(field.linkedField);
linkedField.allowBlank = field.getValue() == "1";
linkedField.clearInvalid();
}
}),





I can't seem to get the listeners to fire in the combobox.. I place
alert(value); after the opening of the function in the checkbox example you gave me and I get a alert box, when I save the form, that shows true or false.

When I do the same thing in the combobox I get nothing and it dosen't force any required field in the text box.

dj
27 Jul 2007, 5:48 AM
Hi kordsmen,

please provide a complete, testable sample of your problem (a URL to a test or post a self-contained test-HTML here in the forum)

kordsmen
28 Jul 2007, 4:18 AM
Hi kordsmen,

please provide a complete, testable sample of your problem (a URL to a test or post a self-contained test-HTML here in the forum)

Ok here we go..

Here is the the html I use to call my js file:


<cfsetting showDebugOutput="No">
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Registration Form</title>
</head>
<link rel="stylesheet" href="../resources/css/ext-all.css" />
<!-- GC -->
<link rel="stylesheet" type="text/css" href="../resources/css/xtheme-aero.css" />
<!-- LIBS -->
<script type="text/javascript" src="../adapter/ext/ext-base.js"></script>
<!-- ENDLIBS -->
<script src="../ext-all-debug.js"></script>
<script type="text/javascript" src="combo.js"></script>
<script src="registration1.js"></script>

<link rel="stylesheet" type="text/css" href="../examples/form/forms.css" />

<!-- Common Styles for the examples -->
<link rel="stylesheet" type="text/css" href="../examples/examples.css" />
</head>
<body>

<div style="width:600px;">
<div class="x-box-tl"><div class="x-box-tr"><div class="x-box-tc"></div></div></div>
<div class="x-box-ml"><div class="x-box-mr"><div class="x-box-mc">

<h3 style="margin-bottom:5px;">Online Registration Form</h3>
<div id="form-ct">

</div>
</div></div></div>
<div class="x-box-bl"><div class="x-box-br"><div class="x-box-bc"></div></div></div>
</div>
</body>
</html>


I am using the rc1 version of ext and I am currently running my code in a directory called registration under the rc1 distribution.

Here is the 'js' file for the checkbox version that works:

Ext.onReady(function(){

Ext.QuickTips.init();

// turn on validation errors beside the field globally
Ext.form.Field.prototype.msgTarget = 'side';


/*
* ================ registration form =======================
*/
var registration = new Ext.form.Form({
labelWidth: 275 // label settings here cascade unless overridden
});
registration.fieldset(
{legend:'Attendee Information'},

new Ext.form.TextField({
fieldLabel: 'Organization Name',
name: 'org_name',
width:175,
maxLength:50,
allowBlank:false
}),


new Ext.form.Checkbox({
fieldLabel: 'Does your organization operate in the USA?',
labelSeparator: '',
name: 'checkbox',
hiddenName:'in_wv',
linkedField:"idOfLinkedField",
listeners:{
change:function(field, value){
//alert(value);
var linkedField = Ext.ComponentMgr.get(field.linkedField);
linkedField.allowBlank = value===false||value=="false";
linkedField.clearInvalid();
}
}
}),

new Ext.form.TextField({
fieldLabel: 'If not from USA where?',
labelSeparator: '',
name: 'optional',
width:175,
maxLength:39,
id:"idOfLinkedField"
}),


new Ext.form.ComboBox({
fieldLabel: 'Title',
hiddenName:'contact_title',
store: new Ext.data.SimpleStore({
fields: ['id', 'title'],
data : Ext.combodata.contact_title // from combo.js
}),
displayField:'title',
valueField: 'id',
typeAhead: true,
mode: 'local',
triggerAction: 'all',
selectOnFocus:true,
width:190,
maxLength:10

}),


new Ext.form.TextField({
fieldLabel: 'First Name',
name: 'first',
width:175,
maxLength:25,
allowBlank:false
}),

new Ext.form.TextField({
fieldLabel: 'Middle Initial',
name: 'Middle',
width:175,
maxLength:1,
allowBlank:false
}),

new Ext.form.TextField({
fieldLabel: 'Last Name',
name: 'last',
width:175,
maxLength:25,
allowBlank:false
})

);




registration.addButton('Save', function(){
if (registration.isValid()) {
var frmRegister = document.getElementById(registration.id);
frmRegister.method = 'POST';
frmRegister.action = 'save-form.php';
frmRegister.submit();
} else{
Ext.MessageBox.alert('Errors', 'Please fix the errors noted.');
}
});


registration.addButton('Cancel');

registration.render('form-ct');
});



I found out this morning that "select" is what I need to use and that the 3rd argument passed from the combobox gives me the possition that was selected. I can put an alert into the if statement and it fires now but the code to make the optional field required is not functioning like it dose for the checkbox.

Here is the 'js' file for the combobox:



Ext.onReady(function(){

Ext.QuickTips.init();

// turn on validation errors beside the field globally
Ext.form.Field.prototype.msgTarget = 'side';


/*
* ================ registration form =======================
*/
var registration = new Ext.form.Form({
labelWidth: 275 // label settings here cascade unless overridden

});
registration.fieldset(
{legend:'Attendee Information'},

new Ext.form.TextField({
fieldLabel: 'Organization Name',
name: 'org_name',
width:175,
maxLength:50,
allowBlank:false
}),




new Ext.form.ComboBox({
fieldLabel: 'Does your organization operate in the USA?',
labelSeparator: '',
hiddenName:'in_wv',
store: new Ext.data.SimpleStore({
fields: ['id', 'yesno'],
data : Ext.combodata.yes_no // from combo.js
}),
displayField:'yesno',
valueField: 'id',
typeAhead: true,
mode: 'local',
triggerAction: 'all',
selectOnFocus:true,
emptyText:'Make a Selection...',
width:190,
allowBlank:false,
editable:false,
linkedField:"idOfLinkedField",
listeners:{
select:function(c, r, i){
if (i == 1){
alert(i);
var linkedField = Ext.ComponentMgr.get(field.linkedField);
linkedField.allowBlank = value===false||value=="false";
linkedField.clearInvalid();
};
}
}
}),




new Ext.form.TextField({
fieldLabel: 'If not from the USA where?',
labelSeparator: '',
name: 'optional',
width:175,
maxLength:39,
id:"idOfLinkedField"
}),


new Ext.form.ComboBox({
fieldLabel: 'Title',
hiddenName:'contact_title',
store: new Ext.data.SimpleStore({
fields: ['id', 'title'],
data : Ext.combodata.contact_title // from combo.js
}),
displayField:'title',
valueField: 'id',
typeAhead: true,
mode: 'local',
triggerAction: 'all',
selectOnFocus:true,
width:190,
maxLength:10

}),


new Ext.form.TextField({
fieldLabel: 'First Name',
name: 'first',
width:175,
maxLength:25,
allowBlank:false
}),

new Ext.form.TextField({
fieldLabel: 'Middle Initial',
name: 'Middle',
width:175,
maxLength:1,
allowBlank:false
}),

new Ext.form.TextField({
fieldLabel: 'Last Name',
name: 'last',
width:175,
maxLength:25,
allowBlank:false
})

);




registration.addButton('Save', function(){
if (registration.isValid()) {
var frmRegister = document.getElementById(registration.id);
frmRegister.method = 'POST';
frmRegister.action = 'save-form.php';
frmRegister.submit();
} else{
Ext.MessageBox.alert('Errors', 'Please fix the errors noted.');
}
});


registration.addButton('Cancel');

registration.render('form-ct');
});