Welcome Guest, Not a member yet? Register   Sign In
Validating Select Menus
#1

[eluser]jzmwebdevelopement[/eluser]
Hello,

I have come to writing my controller code after doing the view:

A few questions regarding the Select Menus:

How do I validate the select menu - I do not want them to be able to select "Please Select"

Can I still use
Code:
$this->form_validation->set_rules('','','required');

How do I send the correct answer into the controller?

Code:
<label for="hostingRequired">Hosting Required:</label>
    <select name="hostingRequired">
     <option value="pleaseSelect"> Please Select</option>
         <option value="yes">Yes</option>
                  <option value="no">No</option>
              </select>

   <label for="domainRequired">Domain Registration: </label>
               <select name="domainRequired">
                   <option value="pleaseSelect">Please Select</option>
                   <option value="yes">Yes</option>
                   <option value="no">No</option>
               </select>

   <div id="domainToBeReged">
               <label for="domainToBeReged">Domain:</label>&lt;input name="domainToBeReged" type="text" placeholder="http://www." /&gt;
      <label for="domainToBeReged0">Domain:</label>&lt;input name="domainToBeReged0" type="text" placeholder="http://www." /&gt;
   </div>
#2

[eluser]psychoder[/eluser]
you can use callback functions
Code:
public function _check($data)
    {
        if ($data == 'pleaseSelect')
        {
            $this->form_validation->set_message('_check', 'The %s field is required');
            return FALSE;
        }
        else
        {
            return TRUE;
        }
    }

  
        $config = array(
            array(
                'field' => 'hostingRequired',
                'label' => 'Hosting',
                'rules' => 'required|callback__check'
            )
        );

$this->form_validation->set_rules($config);
#3

[eluser]jzmwebdevelopement[/eluser]
Thanks,

How would I feed the correct yes or no option into the code. Does CI do this for me. I know it gets complicated with PHP.
#4

[eluser]psychoder[/eluser]
it goes with the form, once you submit the form

Code:
$hosting = $this->input->post('hostingRequired');
#5

[eluser]boltsabre[/eluser]
Easiest way is just not to give your <option> a value, there is no reason to!
Code:
<option value=""> Please Select</option>

And then your "required" rule will fail if they don't select something else. That easy!

On a side note, with regards to security and data cleansing, I'd make the value for Yes = 1, No = 0, and then use this rule set - it will stop people from tampering with your post data (which there are now "out of the box" hacking programs, plugins, etc that people can and do use) and only return what you want.

Code:
$this->form_validation->set_rules('','','trim|required|is_natural|less_than[2]');
With this validation it will only ever validate if user submits a 0 or a 1... just using "required" is not good enough. I expect somewhere in your model or controller you run something like
Code:
if($hosting == "yes"){
   //do something
}else{
   // then it MUST be "no", so lets do something else
}
Well if the user is malicious and have submitted "im a hacker" then your above code will default to your else statement. Best case scenario is you get an "no" value in your database, worse case, depending on your application, is security/exploit holes or causing a fatal error and crashing that page.




Theme © iAndrew 2016 - Forum software by © MyBB