Note that there are some explanatory texts on larger screens.

plurals
  1. POYii private common validation method
    text
    copied!<p>In a Yii php project, I use a a custom validation:</p> <pre><code>public function rules() { return array( array('nbC', 'cCustomValidation'), } public function cCustomValidation() { if ($this-&gt;nbC &gt;= 1) { if (empty($this-&gt;c_attrA_1)) $this-&gt;addError('c_attrA_1', Yii::t('yii', '{attribute} cannot be blank.', array('{attribute}'=&gt;$this-&gt;getAttributeLabel('c_attrA_1')))); if (empty($this-&gt;c_attrB_1)) $this-&gt;addError('c_attrB_1', Yii::t('yii', '{attribute} cannot be blank.', array('{attribute}'=&gt;$this-&gt;getAttributeLabel('c_attrB_1')))); if (empty($this-&gt;c_attrC_1)) $this-&gt;addError('c_attrC_1', Yii::t('yii', '{attribute} cannot be blank.', array('{attribute}'=&gt;$this-&gt;getAttributeLabel('c_attrC_1')))); } } </code></pre> <p>I want to use a private function like this: </p> <pre><code>private function checkNotEmpty($attribute) { if (empty($attribute)) $this-&gt;addError(''.$attribute, Yii::t('yii', '{attribute} cannot be blank.', array('{attribute}'=&gt;$this-&gt;getAttributeLabel($attribute)))); } </code></pre> <p>to call in cCustomValidation like this:</p> <pre><code>public function cCustomValidation() { if ($this-&gt;nbC &gt;= 1) { checkNotEmpty($this-&gt;c_attrA_1); checkNotEmpty($this-&gt;c_attrB_1); checkNotEmpty($this-&gt;c_attrC_1); } } </code></pre> <p>But my checkNotEmpty method doesn't work! Return nothing, no error but no validation or error added! How can I write and call correct common private method for attribute?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload