Skip to content

Commit

Permalink
Validator::clientValidateAttribute() now accepts third parameter: a v…
Browse files Browse the repository at this point in the history
…iew.

punycode.js asset bundle registering moved from ActiveField to validators.
  • Loading branch information
resurtm committed May 15, 2013
1 parent 6458b8d commit f7b8595
Show file tree
Hide file tree
Showing 13 changed files with 45 additions and 22 deletions.
4 changes: 3 additions & 1 deletion yii/validators/BooleanValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,11 @@ public function validateValue($value)
* Returns the JavaScript needed for performing client-side validation.
* @param \yii\base\Model $object the data object being validated
* @param string $attribute the name of the attribute to be validated.
* @param \yii\base\View $view the view object that is going to be used to render views or view files
* containing a model form with this validator applied.
* @return string the client-side validation script.
*/
public function clientValidateAttribute($object, $attribute)
public function clientValidateAttribute($object, $attribute, $view)
{
$options = array(
'trueValue' => $this->trueValue,
Expand Down
4 changes: 3 additions & 1 deletion yii/validators/CaptchaValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,11 @@ public function getCaptchaAction()
* Returns the JavaScript needed for performing client-side validation.
* @param \yii\base\Model $object the data object being validated
* @param string $attribute the name of the attribute to be validated.
* @param \yii\base\View $view the view object that is going to be used to render views or view files
* containing a model form with this validator applied.
* @return string the client-side validation script.
*/
public function clientValidateAttribute($object, $attribute)
public function clientValidateAttribute($object, $attribute, $view)
{
$captcha = $this->getCaptchaAction();
$code = $captcha->getVerifyCode(false);
Expand Down
4 changes: 3 additions & 1 deletion yii/validators/CompareValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,11 @@ public function validateValue($value)
* @param \yii\base\Model $object the data object being validated
* @param string $attribute the name of the attribute to be validated
* @return string the client-side validation script
* @param \yii\base\View $view the view object that is going to be used to render views or view files
* containing a model form with this validator applied.
* @throws InvalidConfigException if CompareValidator::operator is invalid
*/
public function clientValidateAttribute($object, $attribute)
public function clientValidateAttribute($object, $attribute, $view)
{
$options = array('operator' => $this->operator);

Expand Down
8 changes: 7 additions & 1 deletion yii/validators/EmailValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,16 @@ public function validateValue($value)
* Returns the JavaScript needed for performing client-side validation.
* @param \yii\base\Model $object the data object being validated
* @param string $attribute the name of the attribute to be validated.
* @param \yii\base\View $view the view object that is going to be used to render views or view files
* containing a model form with this validator applied.
* @return string the client-side validation script.
*/
public function clientValidateAttribute($object, $attribute)
public function clientValidateAttribute($object, $attribute, $view)
{
if ($this->enableIDN) {
$view->registerAssetBundle('punycode');
}

$options = array(
'pattern' => new JsExpression($this->pattern),
'fullPattern' => new JsExpression($this->fullPattern),
Expand Down
4 changes: 3 additions & 1 deletion yii/validators/InlineValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,14 @@ public function validateAttribute($object, $attribute)
*
* @param \yii\base\Model $object the data object being validated
* @param string $attribute the name of the attribute to be validated.
* @param \yii\base\View $view the view object that is going to be used to render views or view files
* containing a model form with this validator applied.
* @return string the client-side validation script. Null if the validator does not support
* client-side validation.
* @see enableClientValidation
* @see \yii\web\ActiveForm::enableClientValidation
*/
public function clientValidateAttribute($object, $attribute)
public function clientValidateAttribute($object, $attribute, $view)
{
if ($this->clientValidate !== null) {
$method = $this->clientValidate;
Expand Down
4 changes: 3 additions & 1 deletion yii/validators/NumberValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,11 @@ public function validateValue($value)
* Returns the JavaScript needed for performing client-side validation.
* @param \yii\base\Model $object the data object being validated
* @param string $attribute the name of the attribute to be validated.
* @param \yii\base\View $view the view object that is going to be used to render views or view files
* containing a model form with this validator applied.
* @return string the client-side validation script.
*/
public function clientValidateAttribute($object, $attribute)
public function clientValidateAttribute($object, $attribute, $view)
{
$label = $object->getAttributeLabel($attribute);
$value = $object->$attribute;
Expand Down
4 changes: 3 additions & 1 deletion yii/validators/RangeValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,11 @@ public function validateValue($value)
* Returns the JavaScript needed for performing client-side validation.
* @param \yii\base\Model $object the data object being validated
* @param string $attribute the name of the attribute to be validated.
* @param \yii\base\View $view the view object that is going to be used to render views or view files
* containing a model form with this validator applied.
* @return string the client-side validation script.
*/
public function clientValidateAttribute($object, $attribute)
public function clientValidateAttribute($object, $attribute, $view)
{
$range = array();
foreach ($this->range as $value) {
Expand Down
4 changes: 3 additions & 1 deletion yii/validators/RegularExpressionValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,12 @@ public function validateValue($value)
* Returns the JavaScript needed for performing client-side validation.
* @param \yii\base\Model $object the data object being validated
* @param string $attribute the name of the attribute to be validated.
* @param \yii\base\View $view the view object that is going to be used to render views or view files
* containing a model form with this validator applied.
* @return string the client-side validation script.
* @throws InvalidConfigException if the "pattern" is not a valid regular expression
*/
public function clientValidateAttribute($object, $attribute)
public function clientValidateAttribute($object, $attribute, $view)
{
$pattern = $this->pattern;
$pattern = preg_replace('/\\\\x\{?([0-9a-fA-F]+)\}?/', '\u$1', $pattern);
Expand Down
4 changes: 3 additions & 1 deletion yii/validators/RequiredValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,11 @@ public function validateValue($value)
* Returns the JavaScript needed for performing client-side validation.
* @param \yii\base\Model $object the data object being validated
* @param string $attribute the name of the attribute to be validated.
* @param \yii\base\View $view the view object that is going to be used to render views or view files
* containing a model form with this validator applied.
* @return string the client-side validation script.
*/
public function clientValidateAttribute($object, $attribute)
public function clientValidateAttribute($object, $attribute, $view)
{
$options = array();
if ($this->requiredValue !== null) {
Expand Down
4 changes: 3 additions & 1 deletion yii/validators/StringValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,11 @@ public function validateValue($value)
* Returns the JavaScript needed for performing client-side validation.
* @param \yii\base\Model $object the data object being validated
* @param string $attribute the name of the attribute to be validated.
* @param \yii\base\View $view the view object that is going to be used to render views or view files
* containing a model form with this validator applied.
* @return string the client-side validation script.
*/
public function clientValidateAttribute($object, $attribute)
public function clientValidateAttribute($object, $attribute, $view)
{
$label = $object->getAttributeLabel($attribute);
$value = $object->$attribute;
Expand Down
8 changes: 7 additions & 1 deletion yii/validators/UrlValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,17 @@ public function validateValue($value)
* Returns the JavaScript needed for performing client-side validation.
* @param \yii\base\Model $object the data object being validated
* @param string $attribute the name of the attribute to be validated.
* @param \yii\base\View $view the view object that is going to be used to render views or view files
* containing a model form with this validator applied.
* @return string the client-side validation script.
* @see \yii\Web\ActiveForm::enableClientValidation
*/
public function clientValidateAttribute($object, $attribute)
public function clientValidateAttribute($object, $attribute, $view)
{
if ($this->enableIDN) {
$view->registerAssetBundle('punycode');
}

if (strpos($this->pattern, '{schemes}') !== false) {
$pattern = str_replace('{schemes}', '(' . implode('|', $this->validSchemes) . ')', $this->pattern);
} else {
Expand Down
4 changes: 3 additions & 1 deletion yii/validators/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,11 +211,13 @@ public function validateValue($value)
*
* @param \yii\base\Model $object the data object being validated
* @param string $attribute the name of the attribute to be validated.
* @param \yii\base\View $view the view object that is going to be used to render views or view files
* containing a model form with this validator applied.
* @return string the client-side validation script. Null if the validator does not support
* client-side validation.
* @see \yii\web\ActiveForm::enableClientValidation
*/
public function clientValidateAttribute($object, $attribute)
public function clientValidateAttribute($object, $attribute, $view)
{
return null;
}
Expand Down
11 changes: 1 addition & 10 deletions yii/widgets/ActiveField.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
use yii\helpers\Html;
use yii\base\Model;
use yii\web\JsExpression;
use yii\validators\EmailValidator;
use yii\validators\UrlValidator;

/**
* @author Qiang Xue <[email protected]>
Expand Down Expand Up @@ -123,13 +121,6 @@ public function begin()
}
$options['class'] = implode(' ', $class);

foreach ($this->model->getActiveValidators($attribute) as $validator) {
if (($validator instanceof EmailValidator || $validator instanceof UrlValidator) && $validator->enableIDN) {
$this->form->view->registerAssetBundle('punycode');
break;
}
}

return Html::beginTag($this->tag, $options);
}

Expand All @@ -147,7 +138,7 @@ protected function getClientOptions()
$validators = array();
foreach ($this->model->getActiveValidators($attribute) as $validator) {
/** @var \yii\validators\Validator $validator */
$js = $validator->clientValidateAttribute($this->model, $attribute);
$js = $validator->clientValidateAttribute($this->model, $attribute, $this->form->getView());
if ($validator->enableClientValidation && $js != '') {
$validators[] = $js;
}
Expand Down

0 comments on commit f7b8595

Please sign in to comment.