简体   繁体   中英

Setting $_POST in CodeIgniter overrides form validation..?

So in this post How do you set a CodeIgniter radio button, before it's in the $_POST? TwiStar explained how to modify the $_POST variable, which is great... except that it seems to then override the CodeIgniter form validation functions.

if ($this->form_validation->run() === FALSE) {...}

It is probably due to my design... I have some data that I use to pre-populate my form, which I present to the user to review and modify if they wish. The pre-population (unfortunately, in this case) provides exactly the data for the form validation.

So I'm guessing that CodeIgniter's form_validation->run() is true when the $_POST contains the right content, which means that my form validation is right before I even start.

If I leave out one of the pre-populated values, I get a crappy user experience.

How can I handle this better?

I found the problem seemed to be more of a design and flow.

The problem: Wanting to pre-populate a form with database values, and then allow the user to modify them, and submitting them through a form validation.

The solution:

  • In the controller I did not attempt to modify the $_POST values with if (!isset($_POST['myradio'])) {...}

  • In the form, I did make use of the set_radio() function, specifically the third parameter about default values.

The HTML is

<input type="radio" name="target" value="direct" <?php echo set_radio('target', "direct", ($data['target'] === 'direct' ? TRUE : " ") ); ?> />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM