Lightning – How to use force:inputField?

I’ve seen that since Spring’15, there are new standard components available in Lightning.

I’ve been looking for a component that would be a sort of apex:inputField, but in Lightning. The force:inputField component seems to be what I want, however I was not able to make it work.

The component is available inside the Documentation App (/auradocs/reference.app), but there is nothing about it inside the Spring’15 Release Notes so I wonder if this component is fully available or not.

I wrote a really basic test to display an Account Name. I’ve set the attribute type to Account, using type="Account" in my aura:attribute tag but that is not working. Maybe am I missing something about the correct way to set it ?
Here is my code :

LIGTHNING COMPONENT

<aura:component controller="[YOURNAMESPACE].AccountController" implements="force:appHostable">
    <aura:attribute name="account" type="Account"/>
    <aura:handler name="init" value="{!this}" action="{!c.doInit}" />
    <force:inputField value="{!v.account.Name}" />
</aura:component>

COMPONENT CONTROLLER

({
    doInit: function(component, evt, helper) {
        var action = component.get("c.getAccount");
        action.setCallback(this, function(a) {
                component.set("v.account", a.getReturnValue());
        });
        $A.enqueueAction(action);
    },
})

APEX CONTROLLER

public class AccountController {
    @AuraEnabled
    public static Account getAccount() {
        return [select Id, Name from Account Limit 1];
    }
}

However, using <ui:inputText value="{!v.account.Name}" /> instead of <force:inputField value="{!v.account.Name}" /> is working.

Did someone manage to use it ?

Answer

This is a current shortcoming of those components.

As explained to me by Doug Chasman, force:inputField and force:outputField have always been provided the metadata definition of what the sObject was by their containing component (for instance force:recordEdit). At some point they will be able to do so on their own, but for now, we need to help them.

The way to do this is to provide a default value object to the Account attribute, like this:

<aura:attribute name="account" type="Account" default="{ sobjectType: 'Account' }"/>

I just tested this out, and it works for me it works to get rid of the error.

Attribution
Source : Link , Question Author : Fabien Taillon , Answer Author : pchittum

Leave a Comment