admin管理员组

文章数量:1327972

I have form generated with HTML helper:

<div class="row">
  <div class='form-group col-lg-6 col-sm-6'>
    <?= HTML::label('Name:','PropertyContactsNew',['class' => 'control-label']); ?>
    <?= HTML::textInput("PropertyContactsNew[{$slug_other_contacts}][name][]", '', ['maxlength' => 50, 'class'=>'form-control']) ?>
  </div>
  <div class='form-group col-lg-6 col-sm-6'>
    <?= HTML::label('Company:','PropertyContactsNew',['class' => 'control-label']); ?>
    <?= HTML::textInput("PropertyContactsNew[{$slug_other_contacts}][pany][]", '', ['maxlength' => 100, 'class'=>'form-control']) ?>
  </div>
</div>

Is there a way mark form fields required and prevent form from submitting in case the it is empty, just like the ActiveForm does (I can not use ActiveForm for some reasons) or should I use js library to do this?

I have form generated with HTML helper:

<div class="row">
  <div class='form-group col-lg-6 col-sm-6'>
    <?= HTML::label('Name:','PropertyContactsNew',['class' => 'control-label']); ?>
    <?= HTML::textInput("PropertyContactsNew[{$slug_other_contacts}][name][]", '', ['maxlength' => 50, 'class'=>'form-control']) ?>
  </div>
  <div class='form-group col-lg-6 col-sm-6'>
    <?= HTML::label('Company:','PropertyContactsNew',['class' => 'control-label']); ?>
    <?= HTML::textInput("PropertyContactsNew[{$slug_other_contacts}][pany][]", '', ['maxlength' => 100, 'class'=>'form-control']) ?>
  </div>
</div>

Is there a way mark form fields required and prevent form from submitting in case the it is empty, just like the ActiveForm does (I can not use ActiveForm for some reasons) or should I use js library to do this?

Share Improve this question asked Nov 27, 2015 at 15:45 Avag SargsyanAvag Sargsyan 2,5234 gold badges32 silver badges45 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3

Someone might need

<?= HTML::label('Name:','PropertyContactsNew',['class' => 'control-label','required'=>true]); ?>

I think that you could create a FormModel that extends Model class, so you can handle every validation you need.

Red required

Only this code is working for me. Div with class required

<div class="form-group required">
     <?= Html::label('Pressed digit', 'pressed_digit', ['class' => 'control-label']) ?>
</div>

本文标签: javascriptYii2 HTML helper input make requiredStack Overflow