Problems, need help? Have a tip or advice? Post it here.
17 posts Page 1 of 2
Hi

I'm working on a checkout page where I have the following functionality:

There is a radio button item with 2 choices:
    Pick item up
    Deliver item

When a buyer choose for "Pick item up", he only have to fill in his name, email,....

When a buyer choose for "Deliver item", he has to fill in his name, email,... PLUS his address.
With a little bit javascript I figured out to hide or show the address section based on the choice of radio button.

So far so good.
But I would like to give the address input field a 'required' value, but only when the radio button is on "Deliver item". Currently the buyer gets an error message to fill in his address when he is on "Pick item up".

Tried I couple of options, but haven't figured out how to "switch off" the required value when the radio button is on "Pick item up".

Any ideas or tips to get this done?
Thanks!
Hello!
We can't operate with php in template via frontend JS. I suppose that you could use AJAX submitting for that. Make this form a separate template or use AJAX detection from this post: viewtopic.php?p=18342#p18342 .

For example, set value of "Pick item up" to 0 and "Deliver item" to 1. Then serialize via JS the fields you want, like:
{ "radio_choosen": "0", "name": "Nick", "email": "nick@gmail.com" }

Then use <cms:gpc> to detect "radio_choosen" option, and if "0" do success, if "1" && "address" is null, then give an error. And, of course, if the fields don't pass the validation, throw the error, too.

Then parse the response of AJAX and make your logic for displaying an error message.
A little bit complicated trick, but gives you more JS control and asyncrous working.

And don't forget to remove "required" param from the field, because you use another logic for detection.

UPD: I'm thinking of complicated solutions first, but there of course more easy ways. For example, just make 2 different forms and show the form you need with JS!
@sam,

As an alternative to @musman's suggestion, you can try the following -

All fields in Couch (cms:input and cms:editable) support custom validators which are plain PHP functions. We can use a custom validator to ensure that the address field is not let empty when delivery is chosen.

Please take a look at the following sample form -
Code: Select all
<cms:form method="post">
    <cms:if k_success >
        <h3>Form successfully submitted</h3>
        <cms:each k_success sep='\n'>
            <cms:show item /><br>
        </cms:each>
    </cms:if>
   
    <cms:if k_error >
        <h3>Failed to submit form</h3>
        <cms:each k_error >
            <cms:show item /><br>
        </cms:each>
    </cms:if>

    Item type: <cms:input type="radio" name="item_type" opt_values="Pick item up=pickup | Deliver item=deliver" /><br />
    Address: <cms:input type='textarea' rows="5" cols="20" name="address" label='Address' ></cms:input><br />

    <cms:input type="hidden" name="delivery_address" label='Address' value="1" validator='required_on_deliver' />
   
    <cms:input name="submit" type="submit" value="Submit" />
</cms:form>

The form above has the two fields we are interested in ('item_type' and 'address'). I have added a third field - a hidden field that only serves to carry our custom validator named 'required_on_deliver'.

Now place the validator (i.e. the PHP function) in addons/kfunctions.php file -
Code: Select all
// Custom validator for form field
function required_on_deliver( $field, $args ){
   
    // find the 'item_type' field and check if set to 'delivery'
    $check_address = 0;
    foreach( $field->siblings as $f ){
        if( $f->name=='item_type' ){
            $val = trim( $f->get_data() );
            if( $val=='deliver' ){
                $check_address = 1;
            }
            break;
        }
    }
   
    if( $check_address ){
        // find the 'address' field and check that it is not empty
        foreach( $field->siblings as $f ){
            if( $f->name=='address' ){
                $val = trim( $f->get_data() );
                if( !strlen($val) ){
                    return KFuncs::raise_error( "Address cannot be left empty" );
                }
                break;
            }
        }
    }

}

The names of the two fields and the values in the radio-button is hardcoded in the function so please make sure they match those used in your form.

Hope this helps.
I tried the sollution of KK and that does the trick!

Thanks KK and Musman for the offered help.

KK thanks for all you do!

This function is pretty nifty and works, however when I use this to validate the "other" field, it doesn't error out the fields in the rest of the form when it's triggered to run.

For example, if I have a form with 6 fields and hit submit with it empty, I get 6 errors. Works!

One of the fields is a dropdown for "reason for contact" and when I select the "other" value and hit submit while leaving the other field empty the validator runs and errors out the form. Works!

But - when the function added triggers the error validation, it doesn't show the other fields that are still missing on the form, is that the expected behavior? Can the errors all be grouped together?

@Blutbaden, technically the error being shown is that of the 'hidden' field itself - it just happens to mention the other field that is empty (or whatever you chose to check).

If the hidden field is checking multiple fields, you can concatenate together the error messages of all the fields it is checking and return a consolidated value. There should be no problem in that.

I suspect there is some logical error in the modification you might have done to the PHP code - quite possibly you are making it return after finding the first field that has error instead of looping through all the fields that need checking.

If you could please post your code here, I'll take a look and see what is going wrong.

Re:

@KK,

I did not adjust anything outside of the norm for the script that I see, maybe you can see something.

Here is my functions.php code:
Code: Select all
function other_validator( $field, $args ){
   
    $check_other = 0;
    foreach( $field->siblings as $f ){
        if( $f->name=='reason' ){
            $val = trim( $f->get_data() );
            if( $val=='otherchoice' ){
                $check_other = 1;
            }
            break;
        }
    }
   
    if( $check_other ){
        foreach( $field->siblings as $f ){
            if( $f->name=='reason_other' ){
                $val = trim( $f->get_data() );
                if( !strlen($val) ){
                    return KFuncs::raise_error( "The other reason field cannot be left empty" );
                }
                break;
            }
        }
    }

}


Here is the hidden field code in my form:
Code: Select all
<cms:input type="hidden" name="reason_check" label='Other Reason' value="1" validator='other_validator' />


When I submit the form empty when it firsts loads, it errors out:

Please check the application for missing fields.
First name: Required field cannot be left empty
Last name: Required field cannot be left empty
Reason: Required field cannot be left empty
email: Required field cannot be left empty
<!--- other fields.... -->

When I then select the othervalue option in the dropdown, the other field appears but I leave it empty and hit submit, couch reports:

Please check the application for missing fields.
Other Reason: The other reason field cannot be left empty

It's almost like the validator overrides the other form validation errors instead of adding to it, shouldn't it be this since I selected a Reason value in the dropdown:

Please check the application for missing fields.
First name: Required field cannot be left empty
Last name: Required field cannot be left empty
email: Required field cannot be left empty
Other Reason: The other reason field cannot be left empty
<!--- other fields.... -->


KK wrote: @Blutbaden, technically the error being shown is that of the 'hidden' field itself - it just happens to mention the other field that is empty (or whatever you chose to check).

If the hidden field is checking multiple fields, you can concatenate together the error messages of all the fields it is checking and return a consolidated value. There should be no problem in that.

I suspect there is some logical error in the modification you might have done to the PHP code - quite possibly you are making it return after finding the first field that has error instead of looping through all the fields that need checking.

If you could please post your code here, I'll take a look and see what is going wrong.

Do you need more code from me to help with this?

Only checking one field based on answer from the dropdown, I just would like when the other field errors it does not stop showing the other field errors.

It'd help if you could PM me the full template containing the form.
Could you please do that?

Re:

KK wrote: It'd help if you could PM me the full template containing the form.
Could you please do that?


KK - Can you confirm that this works with data bound forms as regular forms?

I got it working, as it should using the regular <cms:form method="post"> but once I switched it to data bound form the validator seems to stop functioning.
17 posts Page 1 of 2

Who is online

In total there are 4 users online :: 0 registered, 0 hidden and 4 guests
(based on users active over the past 5 minutes)

Users browsing this forum: No registered users and 4 guests