Problems, need help? Have a tip or advice? Post it here.
14 posts Page 1 of 2
Hello again !
I was not able to find such topic in the forum posts, if there is any...
This is what I would like to do:
I have a registration form, where multiple choices can be made...
Course 1, course 2, workshop 1, workshop 2

if course 1 or course 2 are selected, than the text field below should be required..otherwise, it is not required....
I tried to do that by putting cms:if within the input tag , but that does not seem to work. I get an error:
Code: Select all
<cms:input type='checkbox' name='kurs1' opt_values="Kurs 1 Name=Kurs 1 Name "/>
      Your Repertoire  (composer, work ):
<cms:input type='textarea'  rows="4" cols="30"  wrap="physical" name="work1" <cms:if "<cms:not_empty frm_kurs1 />" />required='1'</cms:if>/>


I get this error:
ERROR! ATTRIB_NAME: Invalid char "<"

Any idea how to achieve, that a input is required only under one condition?


best regards

Tanja
hi Tanja!

I have a suggestion - which may work for you. In this example I allow the client to choose if they want pagination on a listing page. There is an editable field called number_of_review_items -
Code: Select all
<cms:editable name='number_of_review_items' type='text' search_type='integer' validator='non_negative_integer | max_len=4' label='NUMBER OF REVIEWS PER PAGE: set this value to 9999 if you want to display all reviews on a single page. If you want to display say 6 reviews per page set to 6 - and so on' maxlength='4' width='50' order='17' />

which they can set to the number of items they wish to have on each page OR they set it to 9999 and there will be no pagination. I simply check this value and then set a variable - pass_paginate - to 0 or 1 and use pass_paginate within the cms:pages tag ....
Code: Select all
<cms:if number_of_review_items=='9999' ><cms:set pass_paginate='0' />
<cms:else /><cms:set pass_paginate='1' /></cms:if>

<cms:pages masterpage='reviews.php' order='desc' paginate=pass_paginate limit=number_of_reviews >


You can pass a 0 or a 1 to required via a variable that you have set by checking if frm_kurs1 is empty.
Hi potato!

Thank you for your reply. I am afraid, I cannot follow your example. Could you please explain to me, how you see that applied to a form? Is there any link where I can read about number_of_review_items? I cannot find that with documentation seach...

thanks


Tanja
hi again Tanja ... what I mean is that somewhere above your form check frm_kurs1 to see if it is empty or not. 'number_of_review_items' is simply the name I assigned to the variable in my example.

Code: Select all
<cms:if "<cms:not_empty frm_kurs1 />" /> <cms:set pass_required='1' /><cms:else /> <cms:set pass_required='0' /></cms:if>


Then in your form

Code: Select all
<cms:input type='textarea'  rows="4" cols="30"  wrap="physical" name="work1" required='pass_required' />


Not entirely sure if the syntax is error free ;)
HI potato!

ok, I understand now and I like the idea - to set the variable and than call it...
but I recieve again error messages when I add these lines - some "if" seem to be redunant, but I do not find any extra "if"...
I wonder if there is something special about forms, because the simple cms:if that I tried before would normaly work in other cases....


best regards

Tanja
try deleting the slash highlighted in red


<cms:if "<cms:not_empty frm_kurs1 />" /> <cms:set pass_required='1' /><cms:else /> <cms:set pass_required='0' /></cms:if>
@potato, the reasoning behind your proposed solution is sound.
Unfortunately, the 'frm_' variables get set in a form only *after* all the fields have been validated. So, we cannot use the 'frm_kurs1' variable to determine the required/not-required rule as the target field would already have been validated before this variable gets set.

Fortunately, as often happens, where Couch falters we can rely on raw PHP to get us home.
The following code should do the trick -
Code: Select all
    <cms:php>
        global $CTX;
        if( isset($_REQUEST['kurs1']) ){
            $CTX->set( 'pass_required', '1' );
        }
    </cms:php>
   
    <cms:input type='checkbox' name='kurs1' opt_values="One | Two"/><br />

    <cms:input type='textarea' name="work1" required=pass_required /><br />

@tanja, hope this helps. Please let us know.
Hello Kamran!

Thanks!! this works great!!!!



I have 3 single checkboxes for 3 different courses - because each is optional, and each has own textfield to enter composer and piano work for that particular course....

So, it is always this case: if the checkbox 1 (kurs1) is selected than textfield related (work1) to it should be required, so it can output error if not filled in...

if the checkbox 2 (kurs2) is selected than textfield (work2) related to it should be required, so it can output error if not filled in... etc...

I tried to "clone" your PHP code, 3 times just changing the kurs1, kurs2 or kurs3...

but that did not work correctly - I got all three error messages displayed when only one course was selected, and in such case other textfields should not be required (show the error message) because other two checkboxes are not checked...


Could you please show me the code for more than one input?


thanks

Tanja
Tanja
I think you can do that by using a different variable for each set of related fields e.g.
Code: Select all
$CTX->set( 'pass_required', '1' );
...
<cms:input type='textarea' name="work1" required=pass_required /><br />
...
...
$CTX->set( 'pass_required_2', '1' );
...
<cms:input type='textarea' name="work2" required=pass_required_2 /><br />
Does it work?
I´ll try that now... previous post I edited while you were posting your answer...
14 posts Page 1 of 2
cron