afterValidate / beforeValidate setting in PHP?

Hello,

I’m trying to use the new beforeValidate and afterValidate events in ActiveForm (http://www.yiiframework.com/doc/api/CActiveForm). This is referring to the client-side javascript events.

I’m not sure how to use these events when setting the ActiveForm options in PHP - when I pass in a javascript function (in a string) into the clientOptions->afterValidate, the function is written to the client as a string:




$('#question-form').yiiactiveform({'validateOnSubmit':true,'validateOnChange':false,'afterValidate':'function(form, data, hasError) { alert("Validated"); }','attributes':[{'inputID':'Question_title','errorID':'Question_title_em_'},{'inputID':'Question_body','errorID':'Question_body_em_'}],'summaryID':'question-form_es_'});

});



When the form validates, it calls that string as if it were a function (jquery.yiiactiveform.js, line 119), and that of course doesn’t work (right?). Is there a way to pass in a function like this via PHP?

Thank you,

Remy

I am also struggling with this, only with beforeValidateAttribute. Did you get anywhere with it?

I’ve tried passing in both a function (as a string)




'clientOptions'=>array(

	'beforeValidateAttribute'=>'function(form, attribute){alert("working");}',

),



I’ve tried passing in the name of a function defined elsewhere, eg




'clientOptions'=>array(

	'beforeValidateAttribute'=>'myFunc',

),



I always get the same JavaScript error




attribute.beforeValidateAttribute is not a function

[Break on this error] if(attribute.beforeValidateAttrib...alidateAttribute($form, attribute)) { 



How are we supposed to turn the passed in string into a function or a reference to a function?

I’ve discovered from another post that this is how to create the function, but I have no idea what <<<EOD is doing. Anyone care to enlighten me?




$js=<<<EOD

js:function(form, attribute) {

   alert('working');

}

EOD;


$form = $this->beginWidget('CActiveForm', array(

	'id'=>"myID",

	'enableAjaxValidation'=>true,

	'clientOptions'=>array(

		'beforeValidateAttribute'=>$js,

	),

));



Oops, never followed up on this before. The answer was to start the string with ‘js:’.

So, like

‘afterValidate’ => 'js:function() {

blah();

}’;

Hope this works!

P.S. Looks like EOD stuff is just another way to declare a string. Interesting, see here

Thanks ZMan9854

I find heredoc/nowdoc useful to define longer multi line strings (like code snippets). There’s one more useful difference from double quoted strings, which is not mentioned in the PHP manual: heredoc (and nowdoc) strings do not contain the first and last newline character which is practical for code snippets like above. You can try this:




<?php

$a=<<<EOD

abc

EOD;


$B="

abc

";


echo '<pre>';

echo 'strlen($a): '.strlen($a)."\n";

echo 'strlen($<img src='http://www.yiiframework.com/forum/public/style_emoticons/default/cool.gif' class='bbc_emoticon' alt='B)' />: '.strlen($<img src='http://www.yiiframework.com/forum/public/style_emoticons/default/cool.gif' class='bbc_emoticon' alt='B)' />."\n";

echo '<pre>';



Output:


strlen($a): 3

strlen($<img src='http://www.yiiframework.com/forum/public/style_emoticons/default/cool.gif' class='bbc_emoticon' alt='B)' />: 5

Thank you so much I had a big issue and this solved it.