如何在对象var中放置if条件

时间:2014-05-12 05:41:06

标签: php

我要做的是在两个数组之间放置一个if条件。在我的表单中,如果他的父div仍然隐藏,则有一些输入不需要验证。 (我的表格有div,有些是隐藏的。)

$validate = new Validate;
    $validation = $validate->check($_POST, array(
                'nom' => array('required' => true),
                'prenom' => array('required' => true),
                'adresse' => array('required' => true),
                'ville' => array('required' => true),
                'codepostal' => array('required' => true),
                'depuis' => array('required' => true),
                'telephone' => array('required' => true),
                'loyer' => array('required' => true),
                'valeur' => array('required' => true),
                'solde' => array('required' => true),

所以,例如,我想做

$validate = new Validate;
    $validation = $validate->check($_POST, array(
                'nom' => array('required' => true),
                'prenom' => array('required' => true),
                'adresse' => array('required' => true),
                'ville' => array('required' => true),
                'codepostal' => array('required' => true)));

            if (Input::get('myinput') == 'on') {
                  'depuis' => array('required' => true),
                  'telephone' => array('required' => true),
            }

    $validation .= $validate->check($_POST, array(      
                'loyer' => array('required' => true),
                'valeur' => array('required' => true),
                'solde' => array('required' => true)));

但是,是的,你猜对了..它不那么容易......任何想法?

2 个答案:

答案 0 :(得分:0)

试试这个

       $validate = new Validate;

        $inputarr[]=array();
        $inputarr[]=array(
                    'nom' => array('required' => true),
                    'prenom' => array('required' => true),
                    'adresse' => array('required' => true),
                    'ville' => array('required' => true),
                    'codepostal' => array('required' => true));
                  );
        if (Input::get('myinput') == 'on') {
        $inputarr[]=array(
                  'depuis' => array('required' => true),
              'telephone' => array('required' => true),
           );
        }

       $inputarr[]= array(      
            'loyer' => array('required' => true),
            'valeur' => array('required' => true),
            'solde' => array('required' => true)
       );

       $validation = $validate->check($_POST, $inputarr);

答案 1 :(得分:0)

好吧,经过一些研究,我发现了最好的东西:

首先,您需要使用array_merge();

例如,

$validate = new Validate;
        $infoproValidate = array(
            'nom' => array('required' => true),
            'prenom' => array('required' => true),
            'adresse' => array('required' => true),
            'ville' => array('required' => true),
            'codepostal' => array('required' => true),
            'depuis' => array('required' => true),
            'telephone' => array('required' => true),
            'loyer' => array('required' => true),
            'valeur' => array('required' => true),
            'solde' => array('required' => true),
            'employeur' => array('required' => true),
            'fonction' => array('required' => true),
            'adressePro' => array('required' => true),
            'villePro' => array('required' => true),
            'telephonePro' => array('required' => true),
            'temps' => array('required' => true),
            'salaire' => array('required' => true),
            'concessionnaire' => array('required' => true),
            'marque' => array('required' => true),
            'modele' => array('required' => true),
            'annee' => array('required' => true),
            'kilometrage' => array('required' => true),
            'prix' => array('required' => true));

        if(Input::get('depuis') == '2 ans et moins') {
            $infoAncien = array(
                'adresseAncien' => array('required' => true),
                'codepostalAncien' => array('required' => true),
                'villeAncien' => array('required' => true),
                'telephoneAncien' => array('required' => true)
            );
        } else {
            $infoAncien = array();
        }

        $resultArr = array_merge((array)$infoproValidate, (array)$infoAncien);

我希望这可以帮助别人:)