Question

Currently I have data that it must has 1 title and 1 content, and randomly number (or none) link group (linktext and link ) , (as the it is dynamic generated) E.g. LinkText1 , Link1 , LinkText2, Link2....and so on. So, one example data is:

title:"abc"
content:"test"
linktext1:"yahoo"
link1:"http://yahoo.com"

my form is

<input type="text" name="linktext[]">
<input type="text" name="link[]">

The problem is, simply

$this->form_validation->set_rules('linkTxt[]', '連結標題', 'required');
$this->form_validation->set_rules('link[]', '連結地址', 'required');

is not working

I would like to if either linkTxt[0] or link[0] (or any pair) is exist , check the corresponding pair. If none of them exist (User can choose not to insert any link) (then no checking require) Thanks

Était-ce utile?

La solution

Try:

if(set_value('linkTxt') or set_value('link') ){
$this->form_validation->set_rules('linkTxt[]', '連結標題', 'required');
$this->form_validation->set_rules('link[]', '連結地址', 'required');
}

Basically, Set_value('input_field') will return false if no value . (I'm not 100% sure if this works with array inputs, otherwise try

$this->input->post('linkTxt[]')

If stil don't work, Might stick into a forloop and test each linkTxt for a value.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top