How to uppercase login fields in auth component - cakephp - cakephp

I want to convert username and password fields that I'm using in my auth component to upper case, before the login action. I tried to set
text-transform:uppercase;
in my CSS, for inputs, and it worked. But the uppercased information do not go to the database. The strings will only be uppercased if I write them with capslock. Could someone give me an example (for newbies)?
Thanks!
EDIT:
Here is my login action:
function login() {
if (!empty($this->data) && $this->Auth->user()) {
$this->User->id = $this->Auth->user('id');
$this->User->saveField('last_login', date('Y-m-d H:i:s'));
$this->redirect($this->Auth->redirect());
}
}

you can uppercase in server-side php as demonstrated by #thecodeparadox, or you can do so with client-side javascript or if you're using jQuery, add it to your events.
html/javascript:
<form>
<input type="text" name="username" onchange="this.value = this.value.toUpperCase();" />
<input type="password" name="password" onchange="this.value = this.value.toUpperCase();" />
</form>​
demo: http://jsfiddle.net/85HEy/2/
or html/jQuery:
<form>
<input type="text" name="username" />
<input type="password" name="password" />
</form>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.js"></script>
<script>
$(document).ready(function() {
$("input[name='username'], input[name='password']").change(function() {
$(this).val($(this).val().toUpperCase());
});
});
</script>​
demo: http://jsfiddle.net/T9XbP/

Related

Creating User model? form.is_valid always returning false

Trying to add new User (using Django default User model).
#api/views.py
#csrf_exempt
#api_view(['GET', 'POST', ])
def signup(request):
print(request.POST)
if request.method == 'POST':
form = UserCreationForm(data=request.POST)
if form.is_valid():
form.save()
return Response('created new user')
else:
return Response('did not')
Here is the form. I am not using raw html, it is a React component
<form className="register-form" noValidate action="api/register/"
method="post" autoComplete="off">
<input type="text" name="username"></input>
<input type="password" name="password"></input>
<input type="submit" value="Submit"></input>
</form>
clicking submit successfully sends a post request to api/register, which in urls.py points it to views.signup.
form.is_valid is always evaluating to false, so the User isn't getting created. As far as I can tell, the only required fields to create the User are username and password. I have also tried removing the label "data" in UserCreationForm(data=request.POST). This doesn't work either. Where am I going wrong?
Solved by printing to console and checking errors in signup function
print(request.POST)
print(form.errors)
print(form.non_field_errors)
User model actually requires username, password1, and password 2. Changed form to
<form className="register-form" noValidate action="api/register/"
method="post" autoComplete="off">
<input type="text" name="username"></input>
<input type="password" name="password1"></input>
<input type="password" name="password2"></input>
<input type="submit" value="Submit"></input>
</form>
which solved the issue

Submit login with AngularJS

I want to avoid using <form> so instead of
<form action="./login" method="post">
<input type="text" placeholder="Username" name="username">
<input type="password" placeholder="Password" name="password">
<input type="submit" value="Login">
</form>
I want to use something like
<input type="text" placeholder="Nombre de usuario" data-ng-model="username">
<input type="password" placeholder="Contraseña" data-ng-model="password">
<a data-ng-click="doLogin()">Login</a>
And in controller:
$scope.doLogin=function(){
var url = '/login?username=' + $scope.username + '&password=' + $scope.password;
$http.post(url).then(function(msg){
console.log(msg);
});
}
The request is sent to Spring framework and work perfectly when I use form,but when I use Angular to perform same action it gives me an error POST http://localhost:8080/login?username=admin&password=admin 404 (Not Found)
What changes I have to do? I would like to have the perfectly the same functionality of submit, as on wrong credentials Spring framework redirects me on another view.
I think that you should not delete the form but change it to a pure angularjs form with a ngSubmit attribute (be sure to delete the action attribute).
Like this :
<form method="post" ng-submit="doLogin()">
<input type="text" placeholder="Username" name="username">
<input type="password" placeholder="Password" name="password">
<input type="submit" value="Login">
</form>
Then, in your js controller, your http call is a post call with parameters passed like a get call (param1=value1&param2=...), change your call to pass login parameters in post.
$scope.doLogin=function(){
var url = '/login';
$http.post(url, {username: $scope.username, password: $scope.password}).then(function(msg){
console.log(msg);
});
}
This way, your angularjs form will behave the same way than your previous code.

how do you keep meteor from clearing form on update

I have a meteor / angular app with my index.ng.html page looking like
<div>
{{1+4}}
<form id = "myForm" name="myForm" >
<input type="text" name = "username"/>
<input type="text" name = "password"/>
<input type="submit" value="Submit"/>
</form>
</div>
When I update the file by changing 1+4 to 1+6, any pre-filled information I have in my form is lost when the page "reloads".
How can I keep from loosing data when the page reloads?
Store the data in a Session object. This should survive a hot code reload but not a manual page refresh.
Template.xxx.events({
'change input': function(e,tmpl) {
var fieldName = tmpl.$(e.currentTarget).attr("name");
Session.set('field' + fieldName, e.currentTarget.value);
}
});
Template.xxx.helpers({
valueForField = function(fieldName) {
return Session.get("field" + fieldName);
}
});
Then you can use this in your html:
<form id = "myForm" name="myForm" >
<input type="text" value="{{valueForField 'username'}}" name = "username"/>
<input type="text" value="{{valueForField 'password'}}" name = "password"/>
<input type="submit" value="Submit"/>
</form>
It's obviously a bit tedious. Meteor used to do this natively using a 'preserve-inputs' package, but it looks to be deprecated.

Clearing all inputs and restoring .ng-pristine on click

It's still the first day of me using AngularJS after inheriting the project from a fellow developer. I was wondering, I have a login/registration form on my interface that is hidden once the items/form is submitted. Now once the user has submitted is there a correct or proper way to clear the form of it's entries and restore the .ng-pristine class on the items. The reason for this is should the user choose to log out and then login again (or another user wishes to register) the form is populated and has the validation css applied to it already. I don't want this, I would want everything to be empty and no CSS applied.
I can do this in JavaScript (obviously) however with AngularJS being a different approach I was wondering if I should approach this issue another way rather than loop through the form items and append a class to each item whilst clearing it's value.
This an example of one of my forms
<form name="signupForm" novalidate ng-submit="register(user)">
<div><label for="email">email:</label><input type="email" id="email" name="email" required ng-model="user.email" ng-minlength=4></div>
<div><label for="userName">Username:</label><input type="text" name="userName" id="userName" ng-model="user.userName" required ng-pattern="/^[A-Za-z0-9 \-_.]*$/" ng-minlength=4></div>
<div><label for="firstName">Vorname:</label><input type="text" name="firstName" id="firstName" ng-model="user.firstName" required ng-pattern="/^[A-Za-z \-_.]*$/" ng-minlength=3></div>
<div><label for="lastName">Nachname:</label><input type="text" name="lastName" id="lastName" ng-model="user.lastName" required ng-pattern="/^[A-Za-z \-_.]*$/" ng-minlength=4></div>
<div><label for="password1">Passwort:</label><input type="password" name="password1" id="password1" ng-model="user.password1" required ng-minlength=4></div>
<div><label for="password2">Passwort wiederholen:</label><input type="password" name="password2" id="password2" ng-model="user.password2" valid-password2 ng-minlength=4 pw-check="password1"></div>
... and so on
Many thanks
The form will appear in the correct scope under its name, i.e. $scope.signupForm. Additionally the object populating the form is $scope.user. In your controller, do:
$scope.user = {}; // or new User() if it is your case
$scope.signupForm.$setPristine();
In case $scope.signupForm is undefined, put a controller directly on the form, and place the code above (and anything else applicable) inside this new controller:
<form name="signupForm" novalidate ng-submit="register(user)"
ng-controller="NewCtrl">
(This may happen due to scope nesting under your original controller.)
Just refer to this post :
Reset form to pristine state (AngularJS 1.0.x)
In the main question you got reference to issues and pull request on AngularJS. In resume you have to use $setPristine() method to your form.
Hope it helps !
var app = angular.module('App', []);
app.controller('formController', function($scope, $document){
$scope.Clear = function(){
angular.forEach(angular.element($document[0].querySelectorAll('input[type=text], input[type=email], input[type=password]')), function (elem, index) {
elem.value = '';
/*
Just extra do something
elem.parent().parent().removeClass('has-error has-success');
elem.parent().parent().children().find('span').removeClass('glyphicon-exclamation-sign glyphicon-ok');
*/
});
$scope.myForm.$setPristine();
}
});
<!DOCTYPE html>
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.5/angular.min.js"></script>
</head>
<body ng-app="App">
<div ng-controller="formController">
<form name="myForm">
<input type="text" name="FirstName" ng-model="FN"/> <br>
<input type="text" name="LastName"/>
<br>
<input type="text" name="UserName"/>
<br>
<input type="password" name="Password"/>
</form>
<button ng-click="Clear()">Clear</button>
</div>
</body>
</html>
Here is my Example, it worked for me i am using angularjs 1.6

View is not displayed correctly

I am learning Cake now and am following IBM's tutorial. I have completed the Blog tutorial successfully from the cake site.
My problem is that the Register view does not show up. Instead of showing the form, the register() action is immediately performed and the condition checking whether the form's parameters are empty always fails so I always get to the failed to register user message.
The ctp file looks like this (it's not using the helpers):
<form action="/users/register" method="post">
<p>Please fill out the form below to register an account.</p>
<label>Username:</label><input name="username" size="40" />
<label>Password:</label><input type="password" name="password" size="40" />
<label>Email Address:</label><input name="email" size="40" maxlength="255" />
<label>First Name:</label><input name="first_name" size="40" />
<label>Last Name:</label><input name="last_name" size="40" />
<input type="submit" value="register" />
</form>
And the register() action is:
function register() {
if (!empty($this->params['form']))
{
if ($this->User->save($this->params['form']))
{
$this->flash('Your registration infomration was accepted.', '/users/register');
}
}
else
{
$this->flash('There was a problem with your registration', '/users/register');
}
}
Try:
function register() {
if (!empty($this->data)) {
$this->User->create();
if ($this->User->save($this->data)) {
$this->flash('Your registration infomration was accepted.', '/users/register');
} else {
$this->flash('There was a problem with your registration', '/users/register');
}
}
}
The big difference being using $this->data instead of $this->params[form] and adding $this->User->create();. Also the else statement should be with if(user is saved) flash saved else flash not saved.

Resources