Yii check the input array of phone numbers

I am working on several contact forms in Yii 1.1.16. If the user can add multiple phone numbers.

The problem is how to check this with Yii rules() ?

 <div class="form-group"> <?php echo $form->labelEx($model,'contacts', array('class'=>'col-md-3 control-label')); ?> <div class="col-md-9"> <div class="multiple-contact multiple-form-group input-group padding-bottom-10px" data-max="5"> <div class="input-group-btn input-group-select"> <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown"> <span class="concept">Phone</span> <i class="fa fa-caret-down"></i> </button> <ul class="dropdown-menu" role="menu"> <li><a href="#phone">Phone</a></li> <li><a href="#fax">Fax</a></li> <li><a href="#mobile">Mobile</a></li> </ul> <?php echo $form->textField($model,'contacts',array('type'=>'text', 'class'=>'input-group-select-val', 'name'=>'contacts[type][]','value'=>'phone')); ?> </div> <?php echo $form->textField($model,'contacts',array('size'=>60,'maxlength'=>255, 'name'=>'contacts[value][]','class'=>'form-control')); ?> <?php echo $form->error($model,'contacts'); ?> <span class="input-group-btn"> <button type="button" class="btn btn-success btn-add"><i class="fa fa-plus"></i></button> </span> </div> </div> </div> 

I tried using this but it doesn’t work

 public function rules() { return array( array('contacts[value][]', 'required'), array('contacts[value][]', 'integerOnly'=>true), array('contacts[value][]','type','type'=>'array','allowEmpty'=>false) ); } 

Here is a Fiddle example on how jQuery works. I want it to be able to check with 'enableAjaxValidation'=>true, Also, when more fields are added, it duplicates the id input. and no ajax post made onblur/onfocus

+5
source share
4 answers

Use custom validation.

Declare a special validator in your rules and define the required validation in the verification method.

 public function rules() { return array( array('contacts', validateContacts), ); } public function validateContacts($attribute,$params) { if (length($this->contacts) == 0) { $this->addError($attribute, 'You must add at least one contact!'); } foreach($this->contacts as $contact) { // ... } } 

In the controller, assign an array of contacts in the Model field and call the model validation method. If there are any errors, they will be displayed line by line.

 <?php echo $form->error($model,'contacts'); ?> 

in view.

The controller contains code for invoking verification.

 $contactModel = new Contact; // assign the array of contacts to the model $contactModel->contacts = $POST['myForm]['contacts'] $contactsModel->validate(); $this->render('myform', contactModel); 

If you want validation to be done through Ajax, you need to specify this when creating your form:

 $form=$this->beginWidget('CActiveForm', array( 'id'=>'top-websites-cr-form', 'enableAjaxValidation'=>true, 'clientOptions' => array( 'validateOnSubmit'=>true, 'validateOnChange'=>true), )); 

In this case, your controller can check the ajax forms.

 if(isset($_POST['ajax']) && $_POST['ajax']==='branch-form') { echo CActiveForm::validate($model); Yii::app()->end(); } 

links: http://www.yiiframework.com/wiki/168/create-your-own-validation-rule/

+4
source

You must make it a separate model with your own validation. Then, in your controller, you must independently check the main models and related models.

Here is a good guide for such an installation: http://www.yiiframework.com/wiki/384/creating-and-updating-model-and-its-related-models-in-one-form-inc-image/

0
source

Let's consider that you have a model called ContactNo and it looks like

  class ContactNo extends CFormModel { public $contact; public function rules() { return array( // your rules array('contact', 'required'), array('contact','length','min'=>2) ); } /** * Declares attribute labels. */ public function attributeLabels() { return array( 'contact'=>'Contact No', ); } } 

Controller as SiteController and actionIndex action name

Then your controller should look something like this.

 public function actionIndex() { // set how many contact fields you want here $contactCount = 3; $models = array(); if(isset($_POST['ContactNo'])) { $successModels = 0; foreach($_POST['ContactNo'] as $key=>$value) { $model = new ContactNo; $model->attributes = $value; if($model->validate()) // this validates your model $successModels++; // it tells how many contact No.s have been validated $models[$key]=$model; } // if all the contact nos are validated, then perform your task here if($successModels === $contactCount) { // save your models echo 'models saved'; Yii::app()->end(); } } else { for($index = 0;$index < $contactCount; $index++) $models[] = new ContactNo; } $params = array(); $params['contactCount']=$contactCount; $params['models']= $models; $this->render('index',$params); } 

Now let's see. Obviously, the view is index.php , and it will be something like

 // Include all the initial part required for activeforms <?php echo $form->errorSummary($models); ?> <?php foreach ($models as $index=>$model): ?> <div class="row"> <?php echo $form->labelEx($model,"[{$index}]contact"); ?> <?php echo $form->textField($model,"[{$index}]contact",array('size'=>60,'maxlength'=>128)); ?> <?php echo $form->error($model,"[{$index}]contact"); ?> </div> <?php endforeach; ?> // Include the submit button 

Hope this helps you or can give you an idea, at least to achieve your goal.

0
source

In my opinion, for a better check on phonenumbers, you should use the php libphonenumber php library, and for it there is an extension for the yii structure http://www.yiiframework.com/extension/libphonenumber/

base use:

 Yii::setPathOfAlias('libphonenumber',Yii::getPathOfAlias('application.vendors.libphonenumber')); $phonenumber=new libphonenumber\LibPhone($your_phone_number); $phonenumber->validate(); 

for more information on the use and capabilities of the php libphonenumber library you can find here: https://github.com/davideme/libphonenumber-for-PHP

0
source

Source: https://habr.com/ru/post/1211912/


All Articles