Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
330 views
in Technique[技术] by (71.8m points)

php - Codeigniter 2 forms on one page, validation_errors problem

On one of my websites I have 2 forms on one page, I am having a problem, with validation_errors(); basically what is happening, is for one of the forms I am checking for errors and if there are any errors I am doing some styling to turn the labels red, how the other form just display errors using echo validation_errors();. When I submit the form that does not display errors just error styling the validation errors are show in the form. How can I stop this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your question is a bit hard to read, but if I understand correctly - you're having trouble validating 2 separate forms from one controller, or issues dealing with errors from different forms using validation_errors() which afaik prints ALL errors:

Before running validation, check for the existence of either a hidden field, a field that is unique to the form, or you can check the value of the particular submit button.

<form>
<input type="hidden" name="form1" value="whatever">
<input name="form1_email" />
<input type="submit" value="Submit Form 1" />
</form>

Then you can use any of these methods to check which form was submitted (This example checks if "form1" was submitted):

<?php
// Choose one:
if ($this->input->post('form1')): // check the hidden input
if ($this->input->post('form1_email')): // OR check a unique value
if ($this->input->post('submit') == 'Submit Form 1'): // OR check the submit button value

    if ($this->form_validation->run()):

        // process form

    else:
            // Create a variable with errors assigned to form 1
            // Make sure to pass this to your view
            $data['form1_errors'] = validation_errors();
    endif;

endif;
// Do same for form 2

Then in your view, instead of using validation_errors() you would use:

if (isset($form1_errors)) echo $form1_errors; // Print only form1's errors

If this doesn't help let me know, and clarify your question by posting your code.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...