ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

form validation (must enter something in at least one field)

November 28, 2009 8:16am

Subscribe [4]
  • #1 / Nov 28, 2009 8:16am

    andychurchill

    21 posts

    I had a form on another site where the requirement was to enter something in an email address field, or in the phone number field, or both.  I wondered how I would incorporate this kind of validation into a codeigniter site using the form validation library?

  • #2 / Nov 28, 2009 10:44am

    überfuzz

    350 posts

    Check out the user guide.

  • #3 / Nov 28, 2009 4:48pm

    Aken

    2430 posts

    1)  Use the non-deprecated Form Validation library, not the one Uberfuzz linked.

    2)  There’s no rule in the Form Validation library that checks against whether one of two or more fields is filled in.  You’d have to create your own callback function rule that checks the forms and makes sure one is filled in.

  • #4 / Nov 29, 2009 5:46am

    überfuzz

    350 posts

    1)  Use the non-deprecated Form Validation library, not the one Uberfuzz linked.
    ...

    Ops my bad!

  • #5 / Nov 29, 2009 8:54am

    Colin Williams

    2601 posts

    Just set the rules conditionally, since the rules change depending on the condition of another field.

  • #6 / Nov 29, 2009 2:35pm

    andychurchill

    21 posts

    Just set the rules conditionally, since the rules change depending on the condition of another field.

    Not sure I understand here.  I need to check if a field is empty before checking the other field? or alternatively run a callback function on the first field, which says if the field is empty and field2 is empty then set a message and return false?

    In either case, how do I get the value of a form field, using the form validation library? reading the documentation for the form validation library, I thought I might be able to get the values using set_value(‘email’); and set_value(‘phone’), but I don’t think this works.  I’d have thought there is a “proper” way.

    $this->form_validation->set_rules(‘email’, ‘Email’, ‘callback_emailphone_check’);

          function emailphone_check($str)
          {
    if(empty($str) && empty(set_value('phone')))
    {
    $this->form_validation->set_message('emailphone_check', 'You must supply either a phone or an email address.');
    return FALSE;
    }
              else
              {
    return TRUE;
    }
          }

  • #7 / Nov 29, 2009 2:41pm

    Colin Williams

    2601 posts

    // If no email address, phone is required
    if ( ! $this->input->post('email'))
    {
       $this->form_validation->set_rules('phone', 'Phone Number', 'required');
    }
    else
    {
       $this->form_validation->set_rules('phone', 'Phone Number', '');
    }
    
    // If no phone number, email is required
    if ( ! $this->input->post('phone'))
    {
       $this->form_validation->set_rules('email', 'Email Address', 'required|valid_email');
    }
    else
    {
       $this->form_validation->set_rules('email', 'Email Address', 'valid_email');
    }

    You could streamline it a bit more, but that’s the general idea

  • #8 / Nov 29, 2009 2:44pm

    andychurchill

    21 posts

    cheers, input->post was the trick to it, makes perfect sense now - I had expected it to be part of form_validation, but I guess it makes more sense to be using input->post because the field may not need validating 😊

  • #9 / Dec 12, 2009 4:52am

    gnhaku

    1 posts

    Try to use arrays in field names.

    $this->load->library('form_validation');
    
    $this->form_validation->set_rules('contacts', 'Contacts', 'required');
    
    $this->form_validation->set_rules('contacts[email]', 'E-mail', 'valid_email|xss_clean');
    $this->form_validation->set_rules('contacts[phone]', 'Phone', 'xss_clean');
.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases