Form Validation validating blank values with min_length and valid_email rules - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5) +--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24) +--- Thread: Form Validation validating blank values with min_length and valid_email rules (/showthread.php?tid=69173) Pages:
1
2
|
Form Validation validating blank values with min_length and valid_email rules - natanfelles - 10-16-2017 I need to update database table columns only if valid fields are received in the request. If a field is received with a blank value '', the validation rules must be applicated or not? Why is a blank value '' accepted when the min_length[5] (strlen('') equals 0, less then 5) and valid_email ('' is not a valid email address) rules are set? If proceed the columns will be populated with blank values... But this can not happen! How to solve this? Please, check the code: PHP Code: <?php I see that it is intentional. But is correct and I have not understand something? RE: Form Validation validating blank values with min_length and valid_email rules - dave friend - 10-16-2017 The easiest thing is to add required to the set of rules. PHP Code: $this->form_validation->set_rules([ RE: Form Validation validating blank values with min_length and valid_email rules - natanfelles - 10-16-2017 Hi, thanks for reply. Ok. But I do not want that this fields be "always required". I have other method, with a db insert, where I'm using the require rule to force the user send the required fields. But on the update method, I need to update only the data received. If the user send only the email, then I need to validate only it, if sent only the username then only it. Or both if both is received. But on the update I can't force the user send all the fields. RE: Form Validation validating blank values with min_length and valid_email rules - natanfelles - 10-16-2017 If I change this line to just ($postdata === NULL) then the validation works well. I'm not certain yet if it can be a bug or not. If changing this change will affect other rules... RE: Form Validation validating blank values with min_length and valid_email rules - Narf - 10-16-2017 Blank values are effectively unset values, otherwise everything would be 'required'. You'll have to filter the blanks out before updating. RE: Form Validation validating blank values with min_length and valid_email rules - natanfelles - 10-16-2017 Uhmm. Ok. Is not a "fail" in the Validation accept this? Because I will need to validate again, all the fields... RE: Form Validation validating blank values with min_length and valid_email rules - Narf - 10-16-2017 It's not a "fail", whatever you mean by that. I don't understand why you'd have to validate again all the fields? RE: Form Validation validating blank values with min_length and valid_email rules - natanfelles - 10-16-2017 Yeah. I found a logic for working with blank fields. For now I'll add an isset rule if the blank field is set but with an empty string. The logic will be like this: PHP Code: <?php I did some research and I saw that there was a issue like this a long time ago. RE: Form Validation validating blank values with min_length and valid_email rules - natanfelles - 10-16-2017 To validate input data to the database I will use one rule group to the inserts and other to the updates. Then, in the inserts always will have a 'required' rule on all the fields. In the updates, I want update only what the user sent (and is supported). Not forcing to send all the data if he do not want to update it. But I need to validate what he sent. The idea is do the HTTP Patch method works. RE: Form Validation validating blank values with min_length and valid_email rules - Narf - 10-16-2017 Your set_rules() calls effectively construct a server-side representation of the form. It makes no sense to define rules for fields that wouldn't exist (that's the only way they wouldn't be sent). This is a really bizzare thing that you're trying to do. |