Note that there are some explanatory texts on larger screens.

plurals
  1. POCakePHP Validate data from an associated model in the controller
    primarykey
    data
    text
    <p> Hello, basically I have two models: <strong>User</strong> and <strong>Profile</strong>. Each User <strong>hasOne</strong> Profile.</p> <p>So, when I submit the Form, it should validate as follows:</p> <pre class="lang-php prettyprint-override"><code>/* UsersController.php */ ... if ($this-&gt;request -&gt; isPost()) { $this-&gt;loadModel('Profile'); //By some reason I have to use loadModel, even if I include it on App::uses $this-&gt;Profile-&gt;set( $this-&gt;request-&gt;data ); $validates = $this-&gt;Profile-&gt;validateAssociated($this-&gt;request-&gt;data['Profile']); if($validates){ //do kinky stuff } else { //cry } </code></pre> <p>I defined the <code>$validate</code> rules on the Profile Model generated in bake.</p> <p>I've tried different approaches, with no success:</p> <pre class="lang-php prettyprint-override"><code> $this-&gt;Profile-&gt;set( $this-&gt;request-&gt;data ); if($this-&gt;Profile-&gt;validates()){ //do kinky stuff } else { //cry } </code></pre> <p>Every time it goes straight and throws me a SQL Error.</p> <p>Any ideas? Am I missing something?</p> <p><em><strong>EDIT:</em></strong></p> <p>Here's the Validation rules (I re-baked the rules to ensure that they were correctly formatted):</p> <pre class="lang-php prettyprint-override"><code>public $validate = array( 'twitter' =&gt; array( 'custom' =&gt; array( 'rule' =&gt; array('custom','([A-Za-z0-9_]+)'), 'message' =&gt; 'S&amp;oacute;lo son admitidos letras, n&amp;uacute;meros y guiones al piso' ), ), 'facebook_id' =&gt; array( 'notempty' =&gt; array( 'rule' =&gt; array('notempty'), 'message' =&gt; 'Ingrese su nombre en Facebook' ), ), 'country_id' =&gt; array( 'notempty' =&gt; array( 'rule' =&gt; array('notempty'), 'message' =&gt; 'Seleccione un pa&amp;iacute;s' ), ), 'state_id' =&gt; array( 'notempty' =&gt; array( 'rule' =&gt; array('notempty'), 'message' =&gt; 'Seleccione un departamento' ), ), 'city_id' =&gt; array( 'notempty' =&gt; array( 'rule' =&gt; array('notempty'), 'message' =&gt; 'Seleccione una ciudad' ), ), 'barrio' =&gt; array( 'notempty' =&gt; array( 'rule' =&gt; array('notempty'), 'message' =&gt; 'Ingrese un barrio' ), ), 'direccion' =&gt; array( 'notempty' =&gt; array( 'rule' =&gt; array('notempty'), 'message' =&gt; 'Ingrese una direcci&amp;oacute;n' ), ), 'telefono' =&gt; array( 'notempty' =&gt; array( 'rule' =&gt; array('notempty'), 'message' =&gt; 'Ingrese un tel&amp;eacute;fono' ), 'numeric' =&gt; array( 'rule' =&gt; array('numeric'), 'message' =&gt; 'S&amp;oacute;lo se admiten n&amp;uacute;meros.' ), ), 'celular' =&gt; array( 'notempty' =&gt; array( 'rule' =&gt; array('notempty'), 'message' =&gt; 'Ingrese un celular' ), 'numeric' =&gt; array( 'rule' =&gt; array('numeric'), 'message' =&gt; 'S&amp;oacute;lo se admiten n&amp;uacute;meros.' ), ), 'nacimiento' =&gt; array( 'date' =&gt; array( 'rule' =&gt; array('date'), 'message' =&gt; 'Ingrese una fecha de nacimiento v&amp;aacute;lida.' ), 'notempty' =&gt; array( 'rule' =&gt; array('notempty'), 'message' =&gt; 'Ingrese una fecha de nacimiento.' ), ), 'sexo' =&gt; array( 'notempty' =&gt; array( 'rule' =&gt; array('notempty'), 'message' =&gt; 'Seleccione sexo' ), ), 'estado_civil' =&gt; array( 'notempty' =&gt; array( 'rule' =&gt; array('notempty'), 'message' =&gt; 'Seleccione estado civil' ), ), 'estrato' =&gt; array( 'notempty' =&gt; array( 'rule' =&gt; array('notempty'), 'message' =&gt; 'Seleccione estrato' ), ), 'estudio_id' =&gt; array( 'notempty' =&gt; array( 'rule' =&gt; array('notempty'), 'message' =&gt; 'Seleccione nivel de estudios' ), ), 'ocupacion_id' =&gt; array( 'notempty' =&gt; array( 'rule' =&gt; array('notempty'), 'message' =&gt; 'Seleccione ocupaci&amp;oacute;n' ), ), 'personas_casa' =&gt; array( 'comparison' =&gt; array( 'rule' =&gt; array('comparison', '&gt;', 0), 'message' =&gt; 'Seleccione el n&amp;uacute;mero de personas en la casa' ), 'notempty' =&gt; array( 'rule' =&gt; array('notempty'), 'message' =&gt; 'Seleccione el n&amp;uacute;mero de personas en la casa' ), ), 'referred_by' =&gt; array( 'notempty' =&gt; array( 'rule' =&gt; array('notempty'), 'message' =&gt; 'Seleccione c&amp;oacute;mo se enter&amp;oacute; de la Comunidad Talk' ), ), 'referrer' =&gt; array( 'notempty' =&gt; array( 'rule' =&gt; array('notempty'), 'message' =&gt; 'Seleccione c&amp;oacute;mo se enter&amp;oacute; de la Comunidad Talk' ), ), ); </code></pre> <p>And the error (which states that the validation was skipped):<br/></p> <pre class="lang-php prettyprint-override"><code>Error: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'country_id' cannot be null SQL Query: INSERT INTO `newTalk`.`profiles` (`user_id`, `twitter`, `facebook_id`, `country_id`, `state_id`, `city_id`, `barrio`, `direccion`, `codigo_postal`, `telefono`, `celular`, `nacimiento`, `sexo`, `estado_civil`, `estrato`, `estudio_id`, `ocupacion_id`, `personas_casa`, `referred_by`, `modified`, `created`) VALUES (2, '', '', NULL, NULL, NULL, '', '', '', '', '', NULL, '0', 0, 0, 0, 0, 0, NULL, '2012-06-28 12:56:07', '2012-06-28 12:56:07') </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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