forms

Angular 2 requiring one of two items in a formgroup

风格不统一 提交于 2020-02-02 11:38:50
问题 I am trying to validate a reactive form that has three fields. Username is always a required field, but I only want the user to have to enter either a phone number or an email in addition to username, and I cannot figure out how to do so. I've tried nested form groups, and custom validators, I also tried the solution here, but no luck. Here is my current html form: <div class="container"><br> <form [formGroup]="signupForm" (ngSubmit)="onSubmit()"> <div class="form-group"> <label for="username

Javascript - form select element open url in new window and submit form

廉价感情. 提交于 2020-02-02 11:38:17
问题 UPDATED - please read further details below original question I have a select form element with various urls, that I want to open in a new window when selected - to do this I have the following code in the element's onchange event: window.open(this.options[this.selectedIndex].value,'_blank'); This works fine. But I also want to submit the form when changing this select element value - I've tried various things, but I can't seem to get it to work. I have jquery, so if it's easier to achieve

Angular 2 requiring one of two items in a formgroup

微笑、不失礼 提交于 2020-02-02 11:36:09
问题 I am trying to validate a reactive form that has three fields. Username is always a required field, but I only want the user to have to enter either a phone number or an email in addition to username, and I cannot figure out how to do so. I've tried nested form groups, and custom validators, I also tried the solution here, but no luck. Here is my current html form: <div class="container"><br> <form [formGroup]="signupForm" (ngSubmit)="onSubmit()"> <div class="form-group"> <label for="username

Placeholder for form_widget choices on Twig

佐手、 提交于 2020-02-02 11:27:24
问题 Using PHP on controller side, i can add a placeholder to my field using the createFormBuilder(): $form = $this->createFormBuilder($company) ->add('name', 'text') ->add('cities', 'entity', array( 'class' => 'NexBaseBundle:City', 'property' => 'name', 'placeholder' => 'Choose an option', )) ->getForm(); I am looking to add a placeholder using Twig, for simple fields i can use attr like this : {{ form_widget(form.name, {'attr': {'placeholder': 'My placeholder'} }) }} I have tried with this but

Override Meta in ModelForm

拟墨画扇 提交于 2020-02-02 08:10:46
问题 If I have: class MCQuestionForm(forms.ModelForm): class Meta: model = models.MultipleChoiceQuestion fields = ('prompt',) Can I override class Meta to change model to some other model? For instance: class Meta: model = models.EssayQuestion EDIT: I had to add that I need to make this override at runtime, the model class will come from the result in views' logic 回答1: Do you mean at runtime? Yes, you can. Here is a simple way to do it: def get_question_form(conditional_model): class

AngularJS - get label text for field

送分小仙女□ 提交于 2020-02-02 02:33:10
问题 Question I was wondering what the AngularJS "best practice" way of getting a label for a field is. With jQuery you just query using a "label for" query then extract the text. While it's possible to do it this way with AngularJS, something just doesn't feel right about it. Assume you have something like this in your HTML: <form name="MyForm" ng-controller="Ctrl"> <label for="MyField">My spoon is too big:</label> <input type="text" size="40" id="MyField" ng-model="myField" /> <br /><br /> You

Django : How to exclude form field if the user is staff?

走远了吗. 提交于 2020-02-02 02:04:04
问题 How to exclude form fields if the user is not staff ? I tried this but didn't work , giving an error : global name 'user' is not defined class PostForm(ModelForm): class Meta: model = Photo exclude = ['author','featured','published'] def __init__(self, *args, **kwargs): published = kwargs.pop('published', None) super(PostForm, self).__init__(*args, **kwargs) if not user.is_staff: del self.fields['published'] view.py def addpost(request): if request.method == 'POST': form = PostForm(request

Get the chosen values from FilteredSelectMultiple widget in Django

天涯浪子 提交于 2020-02-01 09:39:28
问题 In Django admin , does anyone know how can i get the chosen values from FilteredSelectMultiple widget in, when the form is saved? class ControllerForm(forms.ModelForm): terminal = forms.ModelMultipleChoiceField(queryset=[]) def __init__(self, *args, **kwargs): super(ControllerForm, self).__init__(*args, **kwargs) self.fields['terminal'].widget = widgets.FilteredSelectMultiple('terminals', False) self.fields['terminal'].help_text = "Select the terminals which are to be added to the group."

Get the chosen values from FilteredSelectMultiple widget in Django

浪尽此生 提交于 2020-02-01 09:39:14
问题 In Django admin , does anyone know how can i get the chosen values from FilteredSelectMultiple widget in, when the form is saved? class ControllerForm(forms.ModelForm): terminal = forms.ModelMultipleChoiceField(queryset=[]) def __init__(self, *args, **kwargs): super(ControllerForm, self).__init__(*args, **kwargs) self.fields['terminal'].widget = widgets.FilteredSelectMultiple('terminals', False) self.fields['terminal'].help_text = "Select the terminals which are to be added to the group."

How do I use an <input>'s pattern attribute to require at least one usable character?

好久不见. 提交于 2020-02-01 08:58:50
问题 I've got an <input> searchbox, and I want to add pattern to guide the user to submitting more than just whitespace. The required attribute doesn't let them submit an empty string, but searching for a couple of spaces goes through and isn't useful. I've tried the following, but input such as " a couple of keywords " isn't valid with them: pattern="\S+" pattern="(!\s*)" pattern="(\s*\S\s*){,}" I know the ^ and $ symbols are implied, and adding them doesn't change anything in the results. How do