BuffaloBuffalo
BuffaloBuffalo

Reputation: 7852

Dojo/Dijit: Dynamically choosing input required attribute

I am attempting to put together a fairly complex form using dojo and dijit widgets. The form has multiple 'sections' which allow the user to attach an existing object (via select tag) or create an entirely new object inline in the form.

My inputs are rendered conditionally based radio buttons and manipulated via javascript. What I am having problems doing, is conditionally making dijit widgets required based on whether the inputs are rendered or not (which itself depends on which radio button is selected.

My html (actually jsp)

<div>
    <input id="useExisting" type="radio" name="radio"  checked value="useExisting" onclick="renderExistingInput()" /> <label for="useExisting">Use Existing</label>
    <input id="new" type="radio" name="radio" value="new" onclick="renderNewInputs()"/> <label for="new">Create New</label>
</div>    
<br>
<div id="newInputs">
    <div class="row">
        <label class="label"  for="newName">Name </label>
        <span class="formInput"><input type="text" id="newName" name="newName" required="true" dojoType="dijit.form.ValidationTextBox"/></span>
    </div>
    <!-- More inputs with required="true"-->

    <br>
</div>
<div id="existingInput>
    <div class="row">
        <label class="label" for="existingSelect">Existing Object </label>
        <span class="formInput"> 
            <select name="existingSelect" id="existingSelect" dojoType="dijit.form.Select">
                <!--JSTL tags for compiling list of options --> 
            </select>
        </span>
    </div>
</div>

Accompanying javascript functions:

function renderExistingInput() {

    dojo.fx.wipeOut(getWipeArguments('newInputs')).play();
    dojo.fx.wipeIn(getWipeArguments('existingInput')).play();
}

function renderNewInputs() {
    dojo.fx.wipeOut(getWipeArguments('existingInput')).play();
    dojo.fx.wipeIn(getWipeArguments('newInputs')).play();

}

function getWipeArguments(id) {
    var wipeArgs = {
        node : id
    };
    return wipeArgs;
}

The basic 'flow' of user interactions is User clicks a radio button, the correct div renders as a result of that. What I want then are inputs that are not rendered to not be considered required. I'm not entirely sure how to do this. Is it possible to manipulate that particular attribute directly via dojo? Or is there a better way to do this entirely?

Upvotes: 0

Views: 2309

Answers (1)

BuffaloBuffalo
BuffaloBuffalo

Reputation: 7852

Seem's like My answer was staring me right in the face. I simply needed to pull together the different parts I had come across. My final function for changed the 'required' attribute looks like:

function setWidgetRequiredAttributes(baseDomNodeId, requiredValue){
    foundWidgets = dijit.findWidgets(dojo.byId(baseDomNodeId));
    console.log(foundWidgets);
    foundWidgets.forEach(function(widget){
        widget.required=requiredValue;
    });
}

Upvotes: 1

Related Questions