• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Validating on multiple inputs

#1
[eluser]taewoo[/eluser]
Hey all.
I have a form with 3 inputs
- name
- city
- state

I form one of those blog URLs based on the three (i.e.. http;//blah.com/John-Smith/Los-Angeles/CA), and I save the query string (i.e.. "John-Smith/Los-Angeles/CA")as a row in a DB table. Of course, I need this to be unique...

How do I validate this? The tricky issue here is that there is no "URL" field in the form to validate on, not to mention is auto-generated.

So far this is what I have:
Code:
$rules['club_id']             = "trim|required|xss_clean";
        $rules['name']                 = "trim|required|xss_clean";
        $rules['description']         = "trim|required|xss_clean";
        $rules['city']              = "trim|required|xss_clean";
        $rules['state']                 = "trim|required|xss_clean|exact_length[2]";
      
        $this->validation->set_rules($rules);
        
        $fields['club_id']     = '<><><><> DB ERROR <><><><>';
        $fields['name']     = '<b>Name</b>';
        $fields['description']    = '<b>Description</b>';
        $fields['city']      = '<b>City</b>';
        $fields['state']    = '<b>State</b>';
              
        $this->validation->set_fields($fields);
        $club_id = $this->input->post("club_id");
        $city  = $this->input->post("city", true);
        $state = $this->input->post("state", true);
        $name  = $this->input->post("name", true);
        $url   = $state ."/".url_title( $city )."/".url_title($name);
        
        $club_names = $this->club_model->getByClubUrl( $url );
        if(count($club_names) > 0)
        {
            $this->validation->set_message('url', 'Name is taken');
        }
        
        
        if ($this->validation->run() == FALSE)
        {    
            return ( !$club_id || $club_id == "X") ? $this->add() : $this->edit($club_id);
        }

Yeap, this doesn't work. Smile

#2
[eluser]Eric Cope[/eluser]
If I understand you correctly, you need to check if three HTML form inputs combine to form a unique string. That unique string is stored in a database. For similar requirements, I have created a callback validation function that does that.
Code:
function checkDB($state)
{
    $name = $this->validation->$name;
    $city = $this->validation->$city;
    $string = $name . '/' . $city . '/' . $state;
    $this->db->select('columnName');
    $this->db->where('columnName',$string);
    $this->db->from('tableName');
    $result = $this->db->count_all_results();
    if(0 == (int) $result) // must type cast, not sure why this returns string
        return FALSE;
    else
        return TRUE;
}

You can add more functionality, but this example should explain it well...

#3
[eluser]taewoo[/eluser]
Oh sweet. Thanks Eric.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.