adamwathan / bootforms

Rapid form generation with Bootstrap 3 and Laravel.
MIT License
417 stars 103 forks source link

[Feature] Required field #7

Closed msieprawski closed 10 years ago

msieprawski commented 10 years ago

Hi,

Is there any option to mark field as required? I see this like new method on form element, like: BootForm::text('Name', 'name')->required() And then field will turn red with additional * or something :)

adamwathan commented 10 years ago

Hey man, not a bad idea, I don't think I would want to make it default behavior though because Bootstrap doesn't have any built-in standard for defining required asterisks and styling them red.

I think what I will do though is make it easy to target the label itself when customizing elements, as right now everything delegates to the form element itself. Gonna think about the best way to add support for what you want easily without making it default, as it's something I would probably use most of the time as well!

Thanks for the suggestion, I'll try and come up with something in the next few days.

msieprawski-squiz commented 10 years ago

Hi. Thank you for response. I will look forward for response.

Best regards!

moura137 commented 10 years ago

I have the same need. The way I prefer is to use a style so as follows.

<style type="text/css">
label.required:after {
  content: "*";
  color:#f00;
}
</style>

To customize the label, I thought I'd change the class AdamWathan\BootForms\Elements\GroupWrapper

class GroupWrapper
{
...
    public function __call($method, $parameters)
    {
        if (preg_match("/^Label_([A-Za-z0-9]+)$/", $method, $result)) {
            $method = $result[1];
            call_user_func_array(array($this->formGroup->label(), $method), $parameters);
            return $this;
        }

        call_user_func_array(array($this->formGroup->control(), $method), $parameters);
        return $this;
    }
...
}

Usage

BootForm::text('First Name', 'first_name')->Label_AddClass('required');

What do you think? I make a pull request

adamwathan commented 10 years ago

Hey man sorry for the slow response! Can you think of other methods you would want to call on the label besides just adding a class? I think that's probably the only one I would need to support, any really crazy customization is probably just better off creating the markup in the template manually as eventually you'll just be fighting with BootForms more than it is helping you.

I'll add a method like labelClass() or something similar and we can play with it for a while and decide if that'll just be enough on it's own or if there are other cases worth accounting for. Want to try and keep it simple.

nCrazed commented 10 years ago

I think that ->required() should instead add the required to field's <input> element which allows targeting it directly with :required pseudo-selector and instructs HTML5 compliant browsers to prevent form submission until the field has a value.

adamwathan commented 10 years ago

That is actually how it works already, so you could use pseudo-elements to style it for sure.

clemblanco commented 7 years ago

Sorry I don't really agree. ->required() will give you the opportunity to target the element not the label using CSS pseudo-elements.

Or am I missing something?

See PR #132.