Array size confusion

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Array size confusion

Patrick Larkin-2
Hello -

I have a form and items are in groups.  When the form is submitted, the code checks to make sure you selected at least one item in a group.  If you missed a group, it adds the group number to a variable with a comma.  So if there are 5 groups, the error variable may look like this:

1,3,5

Then on the next page, I do a

var('errors') = $error_marker->(split:',');

Which I think makes it an array.  Bear with me here.  

errors = array((1),(3),(5))

Then further down the page I iterate through the array and show error messages to the end user:

[if(var('errors')->Size > 1)]
       
        <div class="alert alert-error">
                <strong>FINALIZING CANNOT COMPLETE</strong><br>
        [iterate: $errors, (var('theErr'))]
                       
                        ['You must select at least one rating from Group ' + $theErr + '.<br>']
        [/iterate]
        </div>

This seems to work fine until there is ONE error.  The conditional would be false and not show the error.  So I added an equals sign:


[if(var('errors')->Size >= 1)]
       
        <div class="alert alert-error">
                <strong>FINALIZING CANNOT COMPLETE</strong><br>
        [iterate: $errors, (var('theErr'))]
                       
                        ['You must select at least one rating from Group ' + $theErr + '.<br>']
        [/iterate]
        </div>

However, now when there are NO errors, it still shows the DIV presumably because the value of $errors is still 1.  

I'm confused and not sure what to do.  


Patrick Larkin
Information Systems
Bethlehem Area School District
https://www.beth.k12.pa.us





#############################################################
This message is sent to you because you are subscribed to
  the mailing list Lasso
[hidden email]
To unsubscribe, E-mail to: <[hidden email]>
Send administrative queries to  <[hidden email]>
Reply | Threaded
Open this post in threaded view
|

Re: Array size confusion

French, Shelane
If your string is empty and you do a split, it creates an array with an
empty value. So if $error_marker is an empty string, you get an array with
1 empty value.

var('errors') = array('');


So do this:
var('errors') = $error_marker->(split:',');

$errors->removeall('');


On 12/5/13 8:44 AM, "Patrick Larkin" <[hidden email]> wrote:

>Hello -
>
>I have a form and items are in groups.  When the form is submitted, the
>code checks to make sure you selected at least one item in a group.  If
>you missed a group, it adds the group number to a variable with a comma.
>So if there are 5 groups, the error variable may look like this:
>
>1,3,5
>
>Then on the next page, I do a
>
>var('errors') = $error_marker->(split:',');
>
>Which I think makes it an array.  Bear with me here.
>
>errors = array((1),(3),(5))
>
>Then further down the page I iterate through the array and show error
>messages to the end user:
>
>[if(var('errors')->Size > 1)]
>
> <div class="alert alert-error">
> <strong>FINALIZING CANNOT COMPLETE</strong><br>
> [iterate: $errors, (var('theErr'))]
>
> ['You must select at least one rating from Group ' + $theErr + '.<br>']
> [/iterate]
> </div>
>
>This seems to work fine until there is ONE error.  The conditional would
>be false and not show the error.  So I added an equals sign:
>
>
>[if(var('errors')->Size >= 1)]
>
> <div class="alert alert-error">
> <strong>FINALIZING CANNOT COMPLETE</strong><br>
> [iterate: $errors, (var('theErr'))]
>
> ['You must select at least one rating from Group ' + $theErr + '.<br>']
> [/iterate]
> </div>
>
>However, now when there are NO errors, it still shows the DIV presumably
>because the value of $errors is still 1.
>
>I'm confused and not sure what to do.
>
>
>Patrick Larkin
>Information Systems
>Bethlehem Area School District
>https://www.beth.k12.pa.us
>
>
>
>
>
>#############################################################
>This message is sent to you because you are subscribed to
>  the mailing list Lasso
>[hidden email]
>To unsubscribe, E-mail to: <[hidden email]>
>Send administrative queries to  <[hidden email]>


#############################################################
This message is sent to you because you are subscribed to
  the mailing list Lasso
[hidden email]
To unsubscribe, E-mail to: <[hidden email]>
Send administrative queries to  <[hidden email]>
Reply | Threaded
Open this post in threaded view
|

Re: Array size confusion

Tim Taplin
In reply to this post by Patrick Larkin-2
so, if there are no errors, what does $error_marker look like? is it an empty string?

you could do something like
if($error_marker->size > 0);
        var('errors') = $error_marker->split(',');
else;
        var('errors') = array();
/if;
so that you create an empty array if the string was empty, but you'd still get an array with one element if your string contained one element.

this could be written as a ternary also:
$error_marker->size > 0 ? var('errors' ) = $error_marker->split(',') | var('errors') = array();

I like the neat oneliner layout but it can be confusing if you're not used to it.

Tim taplin

On Dec 5, 2013, at 9:44 AM, Patrick Larkin <[hidden email]> wrote:

> Hello -
>
> I have a form and items are in groups.  When the form is submitted, the code checks to make sure you selected at least one item in a group.  If you missed a group, it adds the group number to a variable with a comma.  So if there are 5 groups, the error variable may look like this:
>
> 1,3,5
>
> Then on the next page, I do a
>
> var('errors') = $error_marker->(split:',');
>
> Which I think makes it an array.  Bear with me here.  
>
> errors = array((1),(3),(5))
>
> Then further down the page I iterate through the array and show error messages to the end user:
>
> [if(var('errors')->Size > 1)]
>
> <div class="alert alert-error">
> <strong>FINALIZING CANNOT COMPLETE</strong><br>
> [iterate: $errors, (var('theErr'))]
>
> ['You must select at least one rating from Group ' + $theErr + '.<br>']
> [/iterate]
> </div>
>
> This seems to work fine until there is ONE error.  The conditional would be false and not show the error.  So I added an equals sign:
>
>
> [if(var('errors')->Size >= 1)]
>
> <div class="alert alert-error">
> <strong>FINALIZING CANNOT COMPLETE</strong><br>
> [iterate: $errors, (var('theErr'))]
>
> ['You must select at least one rating from Group ' + $theErr + '.<br>']
> [/iterate]
> </div>
>
> However, now when there are NO errors, it still shows the DIV presumably because the value of $errors is still 1.  
>
> I'm confused and not sure what to do.  
>
>
> Patrick Larkin
> Information Systems
> Bethlehem Area School District
> https://www.beth.k12.pa.us
>
>
>
>
>
> #############################################################
> This message is sent to you because you are subscribed to
>  the mailing list Lasso
> [hidden email]
> To unsubscribe, E-mail to: <[hidden email]>
> Send administrative queries to  <[hidden email]>


#############################################################
This message is sent to you because you are subscribed to
  the mailing list Lasso
[hidden email]
To unsubscribe, E-mail to: <[hidden email]>
Send administrative queries to  <[hidden email]>
Reply | Threaded
Open this post in threaded view
|

Re: Array size confusion

Patrick Larkin-2
In reply to this post by French, Shelane
That did it.  Thank you very much.





On Dec 5, 2013, at 11:54 AM, "French, Shelane" <[hidden email]> wrote:

> If your string is empty and you do a split, it creates an array with an
> empty value. So if $error_marker is an empty string, you get an array with
> 1 empty value.
>
> var('errors') = array('');
>
>
> So do this:
> var('errors') = $error_marker->(split:',');
>
> $errors->removeall('');
>
>
> On 12/5/13 8:44 AM, "Patrick Larkin" <[hidden email]> wrote:
>
>> Hello -
>>
>> I have a form and items are in groups.  When the form is submitted, the
>> code checks to make sure you selected at least one item in a group.  If
>> you missed a group, it adds the group number to a variable with a comma.
>> So if there are 5 groups, the error variable may look like this:
>>
>> 1,3,5
>>
>> Then on the next page, I do a
>>
>> var('errors') = $error_marker->(split:',');
>>
>> Which I think makes it an array.  Bear with me here.
>>
>> errors = array((1),(3),(5))
>>
>> Then further down the page I iterate through the array and show error
>> messages to the end user:
>>
>> [if(var('errors')->Size > 1)]
>>
>> <div class="alert alert-error">
>> <strong>FINALIZING CANNOT COMPLETE</strong><br>
>> [iterate: $errors, (var('theErr'))]
>>
>> ['You must select at least one rating from Group ' + $theErr + '.<br>']
>> [/iterate]
>> </div>
>>
>> This seems to work fine until there is ONE error.  The conditional would
>> be false and not show the error.  So I added an equals sign:
>>
>>
>> [if(var('errors')->Size >= 1)]
>>
>> <div class="alert alert-error">
>> <strong>FINALIZING CANNOT COMPLETE</strong><br>
>> [iterate: $errors, (var('theErr'))]
>>
>> ['You must select at least one rating from Group ' + $theErr + '.<br>']
>> [/iterate]
>> </div>
>>
>> However, now when there are NO errors, it still shows the DIV presumably
>> because the value of $errors is still 1.
>>
>> I'm confused and not sure what to do.
>>
>>
>> Patrick Larkin
>> Information Systems
>> Bethlehem Area School District
>> https://www.beth.k12.pa.us
>>
>>
>>
>>
>>
>> #############################################################
>> This message is sent to you because you are subscribed to
>> the mailing list Lasso
>> [hidden email]
>> To unsubscribe, E-mail to: <[hidden email]>
>> Send administrative queries to  <[hidden email]>
>
>
> #############################################################
> This message is sent to you because you are subscribed to
>  the mailing list Lasso
> [hidden email]
> To unsubscribe, E-mail to: <[hidden email]>
> Send administrative queries to  <[hidden email]>


#############################################################
This message is sent to you because you are subscribed to
  the mailing list Lasso
[hidden email]
To unsubscribe, E-mail to: <[hidden email]>
Send administrative queries to  <[hidden email]>
Reply | Threaded
Open this post in threaded view
|

Re: Array size confusion

Wade Maxfield
In reply to this post by Patrick Larkin-2
On 6/12/2013, at 5:44 AM, Patrick Larkin <[hidden email]> wrote:

>
>   If you missed a group, it adds the group number to a variable with a comma.  So if there are 5 groups, the error variable may look like this:
>
> 1,3,5
>
> Then on the next page, I do a
>
> var('errors') = $error_marker->(split:',');
>
> Which I think makes it an array.  Bear with me here.  
>
> errors = array((1),(3),(5))
>
> Then further down the page I iterate through the array and show error messages to the end user:

Coming at it from a different angle, why isn't $error_marker an array to start with?  If you need a comma separated list of errors later on for logging then $error_marker->(join:',') will get you there quickly.

 - Wade



#############################################################
This message is sent to you because you are subscribed to
  the mailing list Lasso
[hidden email]
To unsubscribe, E-mail to: <[hidden email]>
Send administrative queries to  <[hidden email]>