stevenboa
stevenboa

Reputation: 1

Regarding Model's Scenario Property

I currently faced with problem on "scenario" property of a model. The problem is, I have a radio button list with two items "text" and "number". Below this radio button list, I defined three keyword textfields. Now I'm using JQuery to hide/show those keyword textfield (When users selected "number" item in the radio button list, those keyword textfields are hidden.) What I'm trying to do now is to add server-side validation on an "answer" textfield. If users are selected "number" item in the radio button list, the answer must be numerical.

My current plan is to specify the scenario property of the model in the JQuery part as follow:

<script type="text/javascript">
    $(document).ready(function(){ 
        $("#Task_0_answer_type_0").click(function () {
            if ($(this).is(":checked")){
                $(".keyword").show();
                <?php $task->scenario = ''?>
            }
        });
        $("#Task_0_answer_type_1").click(function () {
            if ($(this).is(":checked")){
                $(".keyword").hide();
                <?php $task->scenario = 'number'?>
            }
        });
    });
</script>

Then I changed the rules() in the model by adding the following statement:

array('answer', 'numerical', 'on'=> 'number'),

However, it does not work at all? Please help me out.

Thank you in advance.

Upvotes: 0

Views: 62

Answers (1)

Telvin Nguyen
Telvin Nguyen

Reputation: 3559

What I'm trying to do now is to add server-side validation on an "answer" textfield. If users are selected "number" item in the radio button list, the answer must be numerical.

You mixed client script and server language absolutely wrong, you have to remove the part of php code from your above script since they didn't make any sense.

<select name="Task[options]">
         <option value="-1">Select One</option>
         <option value="1">Answer Type Text</option>
         <option value="2">Answer Type Number</option>
</select>

When you post your form, on your action, just make a switch there, before you perform the validation

if($_POST['Task']['options'] == '2'){
   $taskModel->setScenario('number');
}

Upvotes: 2

Related Questions