CakePHP validation rule automatically adds the required attribute to the field

I use a special validation rule in CakePHP to make sure that the value is entered in the field when the corresponding checkbox is checked:

When the "Purchasing TV" checkbox is marked, there needs to be a value in the "Enter TV Price" field.

Here's the validation rule in my model validation array ...

'tv_price'=>array(        
    'check'=>array(
        'rule'=>array('check_for_tv_price'),
        'message'=>'Please enter the television pricing information.',
    ),
)

... and here is my really simple custom check function:

public function check_for_tv_price($check) {
    if($this->data['Client']['tv']==1&&$this->data['Client']['tv_price']=="") {
        return false;
    }
    if($this->data['Client']['tv']==1&&$this->data['Client']['tv_price']!="") {
        return true;
    }
    if($this->data['Client']['tv']==0) {
        return true;
    }

}

I have tried to add 'required'=>falseand 'allowEmpty'=>trueto different parts of the array to test my field tv_price, but they always override my custom rule! As a result, the user cannot submit the form because the browser prevents it (due to the required attribute).

For reference, the browser spits out the following HTML:

<input id="ClientTvPrice" type="text" required="required" maxlength="255" minyear="2013" maxyear="2018" name="data[Client][tv_price]"></input>

(Note that the minyear and maxyear attributes are specified in the default settings.)

- required ?

.

!

+5
3

false allowEmpty true, .

'tv_price'=>array(        
    'check'=>array(
        'rule'=>array('check_for_tv_price'),
        'message'=>'Please enter the television pricing information.',
        'required' => false,
        'allowEmpty' => true
    ),
)

, .

+9

!

, ; JavaScript ( , ), , , .

. onload :

<script type="text/javascript">
    window.onload = function() {
        toggle_setup(document.getElementById('ClientTv'), 'tvPrice', false)
        toggle_required(document.getElementById('ClientTv'), "ClientTvPrice")
    }
</script>

, . !

, , , div , toggle_setup, div . div 'tvPrice'

<?php echo $this->Form->input('Client.tv', array('label'=>'Purchasing TV? <small>(Check if Yes)</small>', 'onclick'=>'toggle_setup(this, "tvPrice", "TV pricing");toggle_required(this, "ClientTvPrice")'));
echo $this->Form->input('Client.tv_price', array('label'=>'Enter TV Price','div'=>array('class'=>'hidden', 'id'=>'tvPrice')));
 ?>

JavaScript:

function toggle_required(element, id) {
    var object = document.getElementById(id)
    if(element.checked) {
        object.setAttribute( 'required', "required")
    } else {
        object.removeAttribute('required')
    }    
}
function toggle_setup(element, div, human_term) {
    if(element.checked) {
        document.getElementById(div).style.display='block'
    } else {
        if(!human_term) {
            clearChildren(document.getElementById(''+div))
            document.getElementById(div).style.display='none'
        } else {
            if(confirm('Are you sure you want to clear the client\ '+human_term+' settings?')) {
                 clearChildren(document.getElementById(div))
                 document.getElementById(div).style.display='none'
            } else {
                element.checked = true
            }
        }
    }

}
function clearChildren(element) {
   for (var i = 0; i < element.childNodes.length; i++) {
      var e = element.childNodes[i];
      if (e.tagName) switch (e.tagName.toLowerCase()) {
         case 'input':
            switch (e.type) {
               case "radio":
               case "checkbox": e.checked = false; break;
               case "button":
               case "submit":
               case "image": break;
               default: e.value = ''; break;
            }
            break;
         case 'select': e.selectedIndex = 0; break;
         case 'textarea': e.innerHTML = ''; e.value=""; break;
         default: clearChildren(e);
      }
   }
}

CSS, "", :

.hidden {
    display: none;
}

In the above suggestions, I have added lines to 'required'=>falseboth 'allowEmpty'=>truemy validation array and it seems to work.

Thanks for the help!

0
source

All Articles