I have a project where I need to do form validation. If the fields are not correctly entered the formErrors object will get a string variable with the correct error text. So in the case of email the error String will be put in formErrors.email. This String can be printed out in the p element that shows the error even the visibility of this p element can be shown or hidden depending on the state of formErrors.email like v-if="formErrors.email".
But when I try to give the input element a red border color using :class="{formErrors.email : text-red-primary}" the linter throws an error Parsing error: Unexpected token .. But how do i enable this class binding with a variable inside the formErrors object.
<input
type="text"
name="email"
id="email"
v-model="email"
placeholder="Email address"
class="w-full px-4 py-3 border rounded-lg text-black-primary focus:outline-none text-sm"
:class="{formErrors.email : text-red-primary}"
/>
<p v-if="formErrors.email" class="text-red-primary text-xs mt-1 ml-1">{{formErrors.email}}</p>
</div>
Your class binding is currently backwards: the key should be the class name, and the value should be truthy/falsy (e.g., a non-empty string would be truthy). Also, the key (text-red-primary) needs to be quoted because it contains non-alphanumeric characters (-):
<!-- ❌ -->
<input :class="{ formErrors.email : text-red-primary }">
<!-- ✅ -->
<input :class="{ 'text-red-primary' : formErrors.email }">
You can create computed property say isEmailInValid which return true/false
and use it in template like this
:class="{'text-red-primary': isEmailInValid}"
dynamic class gets applied when condition is true only.
You can checkout vue docs for more info https://v2.vuejs.org/v2/guide/class-and-style.html
Related
I have this Formik Form with Yup validation :
<Form>
<div className="mt-3">
<label className="font-semibold">Nom</label>
<Field className={`mt-2 rounded-md w-full py-2 px-3 ${errors.lastname ? 'border-2 border-red-500 error-form error-form' : 'border'}`}
name="lastname"
type="text"
placeholder="Votre nom"
/>
{errors.lastname &&
<div className="text-red-500 text-sm">{errors.lastname}</div>}
[...]
</div>
I disable the verification before submit (because I didn't want the visitor to be alarmed on every input even before he filled it :
validateOnChange={false}
validateOnBlur={false}
Everything works fine. But now I want to set a green border on the input (when I click on Submit button) to the Fields that are OK for Yup.
I already do it with red borders when error.
I tried className={`mt-2 rounded-md w-full py-2 px-3 ${errors.message ? 'border-2 border-red-500 error-form' : 'border'} ${!errors.lastname ? 'border-2 border-green-500' : ''}`} but this set all my fields in green border even before the submit.
Any idea ?
Yes, it will be applied before you even submit the form as you're just checking if it has error or not. Obviously in the beginning, since there are no errors, all borders will be painted green.
Apply the same code conditionally on submit button click and it will work.
Or to make things more clean, you can dynamically add a class like form_submitted on submit click and then use the following CSS
.form_submitted input {
border-color: green;
}
.form_submitted input.error {
border-color: red;
}
I am using formvalidation.io and Tailwind. Whilst there are plugins for many of the major frameworks, there does not appear to be one for Tailwind.
My issue is trying to get the error messages to appear below the form input to which they relate. There are five inputs, all along the same lines :
<div class="relative w-full mb-3">
<label class="block uppercase text-thatblue text-xs font-bold mb-2" for="password">Password</label>
<input type="password" name="password" id="password" class="border-0 px-3 py-3 placeholder-thatblue-lightest text-thatblue bg-white rounded text-sm shadow focus:outline-none focus:ring w-full ease-linear transition-all duration-150" placeholder="Password">
</div>
If I put in an empty div with an ID of "messages" at the bottom of the form input I can get the error messages to display in that div using :
message: new FormValidation.plugins.Message({
clazz: 'text-xs text-red-500',
container: '#messages'
},
}),
...but that's not a great visual layout.
If, however, I put in an empty div with class of messages under each of the inputs
<div class="messages"></div>
And then change the container targeted by formvalidation.io to be
container: '.messages'
...then error messages for all five inputs appear against the first input, which is even less ideal.
The documentation suggests that I can target closest siblings using :
message: new FormValidation.plugins.Message({
clazz: 'text-xs text-red-500',
container: function (field, ele) {
return FormValidation.utils.closest(ele, '.messages').nextElementSibling;
},
}),
But this does nothing.
What am I missing, Obi Wan? You're my only hope.
When I was reading the documentation in Material Design Lite's official page, no class name is mentioned for the fixed label with a textbox. In case of textarea they have a solution. But same code like the following one is creating only placeholder instead of a label for input type = "text".
<div class="mdl-textfield mdl-js-textfield">
<input class="mdl-textfield__input" type="text" id="sample5">
<label class="mdl-textfield__label" for="sample5">Text lines...</label>
</div>
I haven't seen this documented anywhere but it was annoying me so I delved into the SCSS to see what I could do. No changes to CSS are required. I managed to solve it by doing the following:
Add the mdl-textfield--floating-label has-placeholder classes to the outer <div> element.
Add a placeholder attribute to the <input> element, it can contain a value or remain empty; either way it will still work.
This will force the label to float above the input, instead of acting as a placeholder.
<div class="mdl-textfield mdl-js-textfield mdl-textfield--floating-label has-placeholder">
<input class="mdl-textfield__input" type="text" id="sample5" placeholder="">
<label class="mdl-textfield__label" for="sample5">Text lines...</label>
</div>
I would like to get a typed value from a ReactStrap Input component, via the onChange function.
The aim is to use a text input only but be able to get a specific typed value (String or Number in my example below).
<Input valueAs="String" name="input1" type="text" onChange={this.onChange}></Input>
<Input valueAs="Number" name="input2" type="text" onChange={this.onChange}></Input>
You can see the expected type is defined by the valueAs attribute.
I expect to get an event in this.onChange with:
a value as a String for Input 1
a value as a Number for Input 2
What is the best way to do this with React / Reactstrap?
Input component of reactstrap does not have a property called valueAs. To get value in a format you need, you can do:
<Input name="input1" type="text" onChange={(e) => this.onChange(`${e.target.value}`)}/>
{/* to make it integer you can add unary plus sign like so: */}
{/* this.onChange(+e.target.value) */}
I am doing testing using selenium ide.
My objective is to verify the following
1) Max and Min length property of text boxes.
2) Verify the texts of the labels
My html code is as below:
<div class="control-group">
<label class="control-label" for="input01">Email</label>
<div class="controls">
<input name="data[Salon][username]" class="span4" id="username" placeholder="Username or Email" type="text"/>
</div>
</div>
<div class="control-group">
<label class="control-label" for="input01">Password</label>
<div class="controls">
<input name="data[Salon][password]" class="span4" id="password" placeholder="Required" type="password"/>
</div>
But in the above I am facing following problems:
a) I have problem in accessing the labels for assertText or assertElementPresent since they are having same class name.
b) I don't know how verify the Max and Min length property of the Text boxes.
And please note that when I am trying to use
document.getElementsByClassName("control-label")
I am getting the following error:
[error] locator not found: document.getElementsByClassName("control-lable"), error = TypeError: e.scrollIntoView is not a function
You can access first label via:
css=div[class='control-group'] label[class='control-label']:contains('Email')
You can access second lavel via:
css=div[class='control-group'] label[class='control-label']:contains('Password')
Use these with command assertElementPresent, "contains" in element locator allows you to verify text in it.
Also you can use xpath:
//div[#class='control-group']//label[#class='control-label'][text()='Email']
//div[#class='control-group']//label[#class='control-label'][text()='Password']
Usually maxlength property is set as attribute of the input, but i can't see it in your html code.. But you can try:
storeAttribute (Selenium IDE's command) and as target you can use xpath:
/div[#class='control-group']//label[#class='control-label'][text()='Email']/#maxlength save it to some var (eg set to the value field smthg like attLength) and then echo this var like: Selenium IDE's command echo and as put to the target field ${attLength}