1

I'm using a form, inside which contains a div with a pair of input field which is added dynamically.

VIEW:

<?php echo form_open_multipart('location/add'); ?>
<div>
<input type="text" name="title[]"/>
<div id="infoMessage"><?php echo form_error('title[]'); ?></div>
</div>
<div>
<input type="text" name="desc[]"/>
<div id="infoMessage"><?php echo form_error('desc[]'); ?></div>
</div>
<div>
<input type="text" name="link[]"/>
<div id="infoMessage"><?php echo form_error('link[]'); ?></div>
</div>  

<input type="submit" name="" value="enter">  
<?php echo form_close(); ?> 

enter image description here

Now, Initially I don't want validation for this 3 input fields but I want the backend validation for all the input fields that are going to add dynamically(by clicking on +) on pressing submit button.

CONTROLLER:

    public function add()
    {

        $this->form_validation->set_rules('title[]','Title','required');
        $this->form_validation->set_rules('desc[]','Description','required');
        $this->form_validation->set_rules('link[]','Link','required');

            if ($this->form_validation->run() == FALSE)
            {
                $this->load->view('test');
            }
            else
            {
                ....
            }

    }

1 Answer 1

0

You can use custom callback validation function EX:

public function add()
{
    $this->form_validation->set_rules('title', 'Title', 'callback_title_validate');
    if ($this->form_validation->run() == FALSE)
    {
        $this->load->view('test');
    }
    else
    {
        //....
    }
}

function title_validate($title){
    foreach($title as $key=>$value){
        if($title[$key]==null || $title[$key]==false){
        $this->form_validation->set_message('title_validate', 'The Title field is Required.');
            return FALSE;
        }
        }
    return TRUE;
}
Sign up to request clarification or add additional context in comments.

6 Comments

See still the initial input filed will also validate right?
No, you will have to make custom validate function for all the fields you want to validated.
I think u didn't get my question :(
@Shihas do you want to validate only those data which are added dynamically and not the very first one ?
@samjhanajoshi I want to validate those fields that are added dynamically
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.