Welcome Guest, Not a member yet? Register   Sign In
Validating groups of inputs
#1

[eluser]vertmonkee[/eluser]
I have a form where a user is entering a location. The name and description are required so I have set my rules like this

Code:
$rules['locationName']        = "required";
$rules['locationDescription'] = "required";

They can then choose a coordinate system from three choices.

1. Decimal latitude / longitude
2. Grid reference
3. Latitude / longitude

How would I go about setting a rule for this?

Thanks for any help
#2

[eluser]Ihab Khattab[/eluser]
what I understand you have problem in setting rules for select

for example if your markup for select something like

Code:
<select name="coor-sys">

<option value="dec">Decimal latitude / longitude</option>
<option value="grid">Grid reference</option>
<option value="Lat/long">Latitude / longitude</option>

</select>

you should write something like

Code:
$config = array(
               array(
                     'field'   => 'locationName',
                     'label'   => 'Location name',
                     'rules'   => 'required'
                  ),
               array(
                     'field'   => 'locationDescription',
                     'label'   => 'Location description',
                     'rules'   => 'required'
                  ),
               array(
                   'field'   => 'coor-sys',
                   'label'   => 'Coordination system',
                   'rules'   => 'required'
               )
            );
$this->form_validation->set_rules($config);

I modified the code you provide as I think it won't work. for more example about passing rules as an array you may check
http://ellislab.com/codeigniter/user-gui...lesasarray
#3

[eluser]vertmonkee[/eluser]
Thanks for the suggestion. Unfortunately that's not how my form is set up.

What I have is the following
Code:
<label for="locationDecimalLatitude">Decimal latitude</label>
&lt;input type="text" name="locationDecimalLatitude" value="50.620411" id="locationDecimalLatitude"  /&gt;

<label for="locationDecimalLongitude">Decimal longitude</label>
&lt;input type="text" name="locationDecimalLongitude" value="-2.544741" id="locationDecimalLongitude"  /&gt;

<label for="locationLatitude">Latitude</label>
&lt;input type="text" name="locationLatitude" value="" id="locationLatitude"  /&gt;

<label for="locationLongitude">Longitude</label>
&lt;input type="text" name="locationLongitude" value="" id="locationLongitude"  /&gt;

<label for="locationEastOrWest">East or West</label>
<select name="locationEastOrWest" id="locationEastOrWest">
  <option value="east" selected="selected">East</option>
  <option value="west">West</option>
</select>

<label for="">Grid reference letter</label>
<select name="locationGridReferenceLetter" id="locationGridReferenceLetter">
  <option value="1" selected="selected">HL</option>
  <option value="2">HM</option>
  &lt;!-- Many more options here --&gt;
</select>

<label for="locationGridReferenceNumber">Grid reference</label>
&lt;input type="text" name="locationGridReferenceNumber" value="0" id="locationGridReferenceNumber"  /&gt;

The user must complete at least one combination of
locationDecimalLatitude and locationDecimalLongitude
locationLatitude, locationLongitude and locationEastOrWest
locationGridReferenceLetter and locationGridReferenceNumber

Hope this explains the problem clearer.

I'd like to be able to somehow include this in my validation rules as I check the form is valid with

Code:
if ($this->validation->run() != FALSE) {

Thanks
#4

[eluser]Ihab Khattab[/eluser]
hmmm this gets now more interesting Smile

you can use arrays as fileds name

http://ellislab.com/codeigniter/user-gui...ysasfields

For example your markup would be something like

Code:
<fieldset>
            <legend>First combination</legend>
        <label for="locationDecimalLatitude">Decimal latitude</label>
&lt;input type="text" name="comb1[]" value="50.620411" id="locationDecimalLatitude"  /&gt;

<label for="locationDecimalLongitude">Decimal longitude</label>
&lt;input type="text" name="comb1[]" value="-2.544741" id="locationDecimalLongitude"  /&gt;
        </fieldset>
        
        <fieldset>
            <legend>Second combination</legend>
<label for="locationLatitude">Latitude</label>
&lt;input type="text" name="comb2[]" value="" id="locationLatitude"  /&gt;

<label for="locationLongitude">Longitude</label>
&lt;input type="text" name="comb2[]" value="" id="locationLongitude"  /&gt;

<label for="locationEastOrWest">East or West</label>
<select name="comb2[]" id="locationEastOrWest">
  <option value="east" selected="selected">East</option>
  <option value="west">West</option>
</select>

        </fieldset>

<fieldset>

    <legend>Third combination</legend>
<label for="">Grid reference letter</label>
<select name="comb3[]" id="locationGridReferenceLetter">
  <option value="1" selected="selected">HL</option>
  <option value="2">HM</option>
  &lt;!-- Many more options here --&gt;
</select>

Note: fieldset and legend aren't required if you don't want to put them I just put them to make groups

Your validation rules would be something like

Code:
$config = array(
               array(
                     'field'   => 'comb1[]',
                     'label'   => 'First Combination',
                     'rules'   => 'required'
                  ),
               array(
                     'field'   => 'comb2[]',
                     'label'   => 'Second Combination',
                     'rules'   => 'required'
                  ),
               array(
                     'field'   => 'comb3[]',
                     'label'   => 'Third Combination',
                     'rules'   => 'required'
                  )
            );

And you would for sure pass them as
Code:
$this->form_validation->set_rules($config);

so you would have comb1[],comb2[],comb3[] as your groups of input as Codeigniter will treat each as one array with integer index start from 0 and you could set rule for each individual field of this group for example if you want the field with label locationDecimalLongitude -which is the second input in comb1- to be max length of 3 you should modify your rules to be like

Code:
$config = array(
               array(
                     'field'   => 'comb1[]',
                     'label'   => 'First Combination',
                     'rules'   => 'required'
                  ),
                  array(
                     'field'   => 'comb1[1]',
                     'label'   => 'location Decimal Longitude',
                     'rules'   => 'max_length[3]'
                  ),
               array(
                     'field'   => 'comb2[]',
                     'label'   => 'Second Combination',
                     'rules'   => 'required'
                  ),
               array(
                     'field'   => 'comb3[]',
                     'label'   => 'Third Combination',
                     'rules'   => 'required'
                  )
            );

The problem here you won't have your inputs named as you want them to be meaningful you have to refer to them by index comb1[1] and so on

Hope this help
#5

[eluser]vertmonkee[/eluser]
Thanks very much Ihab,

That is a very good solution.

I'll have a go at implementing it and report back.

Thanks for your help
#6

[eluser]Ihab Khattab[/eluser]
You are welcome vertmonkee

But I think there will be a problem Sad

When user submit only one combination he'll still get error that other combinations are
required there won't be the optional thing

Sorry this was my fault Sad . I think we could find a way to solve this
#7

[eluser]vertmonkee[/eluser]
I was thinking about creating a callback for the validation.

The problem I have with that is displaying the error in my view.

E.g. if I have a callback function as so
Code:
function validateGroups() {

// Not vlaid return false
return FALSE;

}

And then trigger this from one of the inputs like this

Code:
$rules['locationDecimalLatitude] = "numeric|callback_validateGroups";

In my view how would I be able to tell if the error applies to the decimal latitude not being numeric or there not being a valid group completed.
#8

[eluser]Ihab Khattab[/eluser]
You can set error message for your callback using

Code:
$this->form_validation->set_message()

inside your callback function like the example in user guide

http://ellislab.com/codeigniter/user-gui...#callbacks

but did your validateGroups() check if there is at least one group completed,so you could solve this problem ?

If so, could you please share your code with us ?
#9

[eluser]vertmonkee[/eluser]
In the callback function I would have something like the following

Code:
// Check the decimal lat and long
        if(is_numeric($this->input->post('locationDecimalLatitude')) && is_numeric($this->input->post('locationDecimalLongitude'))) {
            $decimal_valid = TRUE;
        } else {
            $decimal_valid = FALSE;
        }
        
        // Check the lat and long
        if(is_numeric($this->input->post('locationLatitude')) && is_numeric($this->input->post('locationLongitude'))) {
            $lat_long_valid = TRUE;
        } else {
            $lat_long_valid = FALSE;
        }
        
        // Check the grid reference
        if($this->input->post('locationEastOrWest') != "" && $this->input->post('locationGridReferenceLetter') != "" && is_numeric($this->input->post('locationGridReferenceNumber'))) {
            $grid_ref_valid = TRUE;
        } else {
            $grid_ref_valid = FALSE;
        }
        
        // Check that at least one group is valid
        if($decimal_valid == TRUE or $lat_long_valid == TRUE or $grid_ref_valid == TRUE) {
            return TRUE;
        } else {
            return FALSE;
        }

Which should work. I'm just unsure on displaying the error message in my view.

If the error message applies to there not being a valid group it needs to be in one place on the view or if it just applies to the locationDecimalLatitude it needs to be displayed next to this input.

The only way I can think is to check the value of the string in the error message but this seems like a horrible way of doing it.

E.g.
Code:
&lt;!-- Above all groups --&gt;
&lt;?php

if(
  isset($this->validation->locationDecimalLatitude_error)
  && $this->validation->locationDecimalLatitude_error == "Please complete a group") {
  echo $this->validation->locationDecimalLatitude_error;
}

?&gt;

&lt;!-- All the other inputs --&gt;

&lt;?php

if(
  isset($this->validation->locationDecimalLatitude_error)
  && $this->validation->locationDecimalLatitude_error == "The decimal latitiude needs to be numerical") {
  echo $this->validation->locationDecimalLatitude_error;
}

?&gt;
#10

[eluser]Ihab Khattab[/eluser]
You can show errors Individually using form_error()

http://ellislab.com/codeigniter/user-gui...dualerrors

for example above your groups you would write

Code:
&lt;?php echo form_error(); ?&gt;

3 times, every time you pass your group name so if there is an error in group it would echo it if there is no error it won't show any thing

and next to each input you would write
Code:
&lt;?php echo form_error(); ?&gt;
passing each input name to it

but I think you still should use arrays as input names so you can use form_error('comb1[]') for a group and use form_error('comb1[n]') to echo it next to input.and also using it to set your rules

I'll try to write some code to make things clearer




Theme © iAndrew 2016 - Forum software by © MyBB