Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>To validate if the hash already exists and belongs to a specific user, you can use the following validator with a custom exclude condition. I assume that the hash is a hidden text field containing the hash from the database.</p> <pre><code>protected function _getEditValidator($field, $userId, $db = null) { if ($db == null) $db = Zend_Db_Table::getDefaultAdapter(); array( 'Db_RecordExists', true, array( 'table' =&gt; 'anunciantes', 'field' =&gt; $field, 'exclude' =&gt; $db-&gt;quoteInto('user_id = ?', $userId) ) ); } </code></pre> <p>This will check to make sure that the hash contained in the element matches the hash in the database that belongs to <code>user_id</code>. We are overriding the exclude option to make it so the hash value must match what is set for the given user id.</p> <p>The query should look roughly like this:</p> <pre><code>SELECT `users`.`db_hash_col` FROM `users` WHERE (`db_hash_col` = :value) AND (user_id = 1234) LIMIT 1 </code></pre> <p>You will need to use some logic to swap the validators when editing a user vs creating a user. You might set it like this:</p> <pre><code>if ($editing) { $this-&gt;getElement('hash') -&gt;setValidators( array($this-&gt;_getEditValidator('db_hash_col', $this-&gt;getElement('id')-&gt;getValue()); } </code></pre> <p>Also, in your question you mentioned using <code>$this-&gt;getValue('id')</code>; I believe this should actually be <code>$this-&gt;getElement('id')-&gt;getValue()</code>.</p> <p>Hope that helps.</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