Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Since I'm using Bootstrap 3, I use a directive: (see <a href="http://plnkr.co/edit/0ED8x3?p=preview" rel="nofollow">plunkr</a>)</p> <pre><code> var ValidSubmit = ['$parse', function ($parse) { return { compile: function compile(tElement, tAttrs, transclude) { return { post: function postLink(scope, element, iAttrs, controller) { var form = element.controller('form'); form.$submitted = false; var fn = $parse(iAttrs.validSubmit); element.on('submit', function(event) { scope.$apply(function() { element.addClass('ng-submitted'); form.$submitted = true; if(form.$valid) { fn(scope, {$event:event}); } }); }); scope.$watch(function() { return form.$valid}, function(isValid) { if(form.$submitted == false) return; if(isValid) { element.removeClass('has-error').addClass('has-success'); } else { element.removeClass('has-success'); element.addClass('has-error'); } }); } } } } }] app.directive('validSubmit', ValidSubmit); </code></pre> <p>and then in my HTML:</p> <pre><code>&lt;form class="form-horizontal" role="form" name="form" novalidate valid-submit="connect()"&gt; &lt;div class="form-group"&gt; &lt;div class="input-group col col-sm-11 col-sm-offset-1"&gt; &lt;span class="input-group-addon input-large"&gt;&lt;i class="glyphicon glyphicon-envelope"&gt;&lt;/i&gt;&lt;/span&gt; &lt;input class="input-large form-control" type="email" id="email" placeholder="Email" name="email" ng-model="email" required="required"&gt; &lt;/div&gt; &lt;p class="col-sm-offset-3 help-block error" ng-show="form.$submitted &amp;&amp; form.email.$error.required"&gt;please enter your email&lt;/p&gt; &lt;p class="col-sm-offset-3 help-block error" ng-show="form.$submitted &amp;&amp; form.email.$error.email"&gt;please enter a valid email&lt;/p&gt; &lt;/div&gt; &lt;/form&gt; </code></pre> <p><strong>UPDATED</strong></p> <p>In my latest project, I use <strong><em>Ionic</em></strong> so I have the following, which automatically puts <code>.valid</code> or <code>.invalid</code> on the <code>input-item</code>'s:</p> <pre><code>.directive('input', ['$timeout', function ($timeout) { function findParent(element, selector) { selector = selector || 'item'; var parent = element.parent(); while (parent &amp;&amp; parent.length) { parent = angular.element(parent); if (parent.hasClass(selector)) { break; } parent = parent &amp;&amp; parent.parent &amp;&amp; parent.parent(); } return parent; } return { restrict: 'E', require: ['?^ngModel', '^form'], priority: 1, link: function (scope, element, attrs, ctrls) { var ngModelCtrl = ctrls[0]; var form = ctrls[1]; if (!ngModelCtrl || form.$name !== 'form' || attrs.type === 'radio' || attrs.type === 'checkbox') { return; } function setValidClass() { var parent = findParent(element); if (parent &amp;&amp; parent.toggleClass) { parent.addClass('validated'); parent.toggleClass('valid', ngModelCtrl.$valid &amp;&amp; (ngModelCtrl.$dirty || form.$submitted)); parent.toggleClass('invalid', ngModelCtrl.$invalid &amp;&amp; (ngModelCtrl.$dirty || form.$submitted)); $timeout(angular.noop); } } scope.$watch(function () { return form.$submitted; }, function (b, a) { setValidClass(); }); var before = void 0; var update = function () { before = element.val().trim(); ngModelCtrl.$setViewValue(before); ngModelCtrl.$render(); setValidClass(); }; element .on('focus', function (e) { if (ngModelCtrl.$pristine) { element.removeClass('$blurred'); } }) .on('blur', function (e) { if (ngModelCtrl.$dirty) { setValidClass(); element.addClass('$blurred'); } }).on('change', function (e) { if (form.$submitted || element.hasClass('$blurred')) { setValidClass(); } }).on('paste', function (e) { if (form.$submitted || element.hasClass('$blurred')) { setValidClass(); } }) ; } }; }]) </code></pre> <p>and then in the HTML:</p> <pre><code> &lt;form name='form' novalidate="novalidate" ng-submit="auth.signin(form, vm)"&gt; &lt;label class="item item-input item-floating-label"&gt; &lt;span class="input-label"&gt;Email&lt;/span&gt; &lt;input type="email" placeholder="Email" ng-model="vm.email" autofocus="true" required &gt; &lt;/label&gt; &lt;button ng-if="!posting" type="submit" class="item button-block item-balanced item-icon-right call-to-action"&gt;Login&lt;i class="icon ion-chevron-right"&gt;&lt;/i&gt; &lt;/button&gt; </code></pre> <p>and in the controller:</p> <pre><code> self.signin = function (form, data) { if (!form.$valid) return; Authentication.emailLogin(data) //... </code></pre> <p>so, now, in the CSS, you can do stuff like:</p> <pre><code>.item.valid::before{ float: right; font-family: "Ionicons"; font-style: normal; font-weight: normal; font-variant: normal; text-transform: none; text-rendering: auto; line-height: 1; -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale; color: #66cc33; margin-right: 8px; font-size: 24px; content: "\f122"; } .item.invalid::before{ float: right; font-family: "Ionicons"; font-style: normal; font-weight: normal; font-variant: normal; text-transform: none; text-rendering: auto; line-height: 1; -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale; color: #ef4e3a; margin-right: 8px; font-size: 24px; content: "\f12a"; /* border-left: solid 2px #ef4e3a !important; border-right: solid 2px #ef4e3a !important; */ } </code></pre> <p><strong>MUCH SIMPLER!</strong></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload